hdu 3826 Hand in Hand 同构图★
来源:互联网 发布:席德梅尔 知乎 编辑:程序博客网 时间:2024/05/19 23:57
Hand in Hand
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 122768/62768 K (Java/Others)Total Submission(s): 2402 Accepted Submission(s): 821
Problem Description
In order to get rid of Conan, Kaitou KID disguises himself as a teacher in the kindergarten. He knows kids love games and works out a new game called "hand in hand".
Initially kids run on the playground randomly. When Kid says "stop", kids catch others' hands immediately. One hand can catch any other hand randomly. It's weird to have more than two hands get together so one hand grabs at most one other hand. After kids stop moving they form a graph.
Everybody takes a look at the graph and repeat the above steps again to form another graph. Now Kid has a question for his kids: "Are the two graph isomorphism?"
Initially kids run on the playground randomly. When Kid says "stop", kids catch others' hands immediately. One hand can catch any other hand randomly. It's weird to have more than two hands get together so one hand grabs at most one other hand. After kids stop moving they form a graph.
Everybody takes a look at the graph and repeat the above steps again to form another graph. Now Kid has a question for his kids: "Are the two graph isomorphism?"
Input
The first line contains a single positive integer T( T <= 100 ), indicating the number of datasets.
There are two graphs in each case, for each graph:
first line contains N( 1 <= N <= 10^4 ) and M indicating the number of kids and connections.
the next M lines each have two integers u and v indicating kid u and v are "hand in hand".
You can assume each kid only has two hands.
There are two graphs in each case, for each graph:
first line contains N( 1 <= N <= 10^4 ) and M indicating the number of kids and connections.
the next M lines each have two integers u and v indicating kid u and v are "hand in hand".
You can assume each kid only has two hands.
Output
For each test case: output the case number as shown and "YES" if the two graph are isomorphism or "NO" otherwise.
Sample Input
23 21 22 33 23 22 13 31 22 33 13 11 2
Sample Output
Case #1: YESCase #2: NO题目挺简单。。细节太多。。同构图 :1。点和边的个数相等。。2.点的入度相同3.环相同,连通分量相同#include<stdio.h>#include<string.h>#include<algorithm>#define MAXN 100003using namespace std;struct Graph{ int son; bool ring;} a[100003],b[100003];int pre1[100003],pre2[100003];void init(){ for(int i=0; i<100002; i++) { pre1[i]=i; pre2[i]=i; a[i].son=1; b[i].son=1; a[i].ring=false; b[i].ring=false; }}int cmp(Graph a,Graph b){ if(a.son==b.son) return a.ring<b.ring; return a.son<b.son;}int Find(int x, int pre[]) //查找根结点+路径压缩{ return x == pre[x] ? x : pre[x]=Find(pre[x], pre);}void Merge(int x,int y,int pre[],Graph tmp[]){ int X=Find(x,pre); int Y=Find(y,pre); if(X==Y) tmp[X].ring=true; else { if(tmp[X].son>=tmp[Y].son) pre[Y]=X,tmp[X].son+=tmp[Y].son; else pre[X]=Y,tmp[Y].son+=tmp[X].son; }}bool judge(int num,Graph a[],Graph b[]){ sort(a+1,a+num+1,cmp); sort(b+1,b+num+1,cmp); for(int i=1;i<=num;i++) { if(a[i].son!=b[i].son||a[i].ring!=b[i].ring) return false; } return true;}int main(){ int iCase=0,T; scanf("%d",&T); while(T--) { int flag=true; init(); int num1,match1,num2,match2; scanf("%d%d",&num1,&match1); for(int i=1;i<=match1;i++) { int x,y; scanf("%d%d",&x,&y); Merge(x,y,pre1,a); } scanf("%d%d",&num2,&match2); if(match1!=match2) flag=false; for(int i=1;i<=match2;i++) { int x,y; scanf("%d%d",&x,&y); if(flag) Merge(x,y,pre2,b); } if(flag) { flag=judge(num2,a,b); if(flag) printf("Case #%d: YES\n", ++iCase); else printf("Case #%d: NO\n", ++iCase); } else printf("Case #%d: NO\n", ++iCase); }}
阅读全文
0 0
- hdu 3826 Hand in Hand 同构图★
- hdu 3926 Hand in Hand (同构图)
- Hdu-3926 Hand in Hand(同构图)
- 【HDU】3926 Hand in Hand 同构图
- HDU 3926 Hand in Hand 同构图
- HDU 3926 Hand in Hand (同构图)
- hdu 3926 hand in hand 同构图
- HDU 3926 Hand in Hand 同构图★
- hdu3926 Hand in Hand 同构图★
- HDU 3926 Hand in Hand(同构图)
- hdu 3926 Hand in Hand(同构图)
- 简单同构图 hdoj3926 Hand in Hand
- Hand in Hand 并查集 同构图
- hdoj3926-Hand in Hand(同构图的判断)
- hdu 3926 Hand in Hand
- Hdu-3926 Hand in Hand
- Hdu-3926 Hand in Hand
- hdu 3926 Hand in Hand
- 数组的动态初始化
- 学习记录(4)
- 从零开始打jar包
- 一键分享
- Mybatis逆向工程
- hdu 3826 Hand in Hand 同构图★
- 见过最好的神经网络CNN解释
- 斐波切纳 数列 java
- c语言初步经典题5----求两个正整数的正差值
- POJ 3264 Balanced Lineup(线段树区间查询)
- 数组的静态初始化
- 2.常用控件:TextView
- RxJava Essentials 中文翻译版
- Python基础三