hdu 3926 Hand in Hand 同构图

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3926

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?"
 
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.
 
Output
For each test case: output the case number as shown and "YES" if the two graph are isomorphism or "NO" otherwise.
题意描述:判断两个图是否是同构图,同构图的定义:假设G=(V,E)和G1=(V1,E1)是两个图,如果存在一个双射m:V→V1,使得对所有的x,y∈V均有xy∈E等价 于m(x)m(y)∈E1,则称G和G1是同构的,这样的一个映射m称之为一个同构,如果G=G1,则称他为一个自同构。
算法分析:由于是判断是否同构,我们就只需要判断一个集合里的节点数目是否相等和是否都是一个环就可以了。
 1 #include<iostream>

 2 #include<cstdio>

 3 #include<cstring>

 4 #include<cstdlib>

 5 #include<cmath>

 6 #include<algorithm>

 7 #define inf 0x7fffffff

 8 using namespace std;

 9 const int maxn=10000+10;

10 

11 int n,m,n2,m2;

12 int father[maxn],d[maxn],isCircle[maxn];

13 struct node

14 {

15     int num,isCircle;

16     friend bool operator < (node a,node b)

17     {

18         if (a.num!=b.num) return a.num>b.num;

19         return a.isCircle>b.isCircle;

20     }

21 }an[maxn],bn[maxn];

22 

23 int findset(int x)

24 {

25     if (x==father[x]) return x;

26     return father[x]=findset(father[x]);

27 }

28 void Union(int x,int y)

29 {

30     x=findset(x) ;y=findset(y) ;

31     if (x==y) {isCircle[x]=1;return;}

32     if (d[x]>d[y])

33     {

34         father[y]=x;

35         d[x] += d[y];

36     }

37     else

38     {

39         father[x]=y;

40         d[y] += d[x];

41     }

42 }

43 

44 int main()

45 {

46     int t,ncase=1;

47     scanf("%d",&t);

48     while (t--)

49     {

50         scanf("%d%d",&n,&m);

51         memset(isCircle,0,sizeof(isCircle));

52         for (int i=1 ;i<=n ;i++) father[i]=i,d[i]=1;

53         int u,v;

54         for (int i=0 ;i<m ;i++)

55         {

56             scanf("%d%d",&u,&v);

57             Union(u,v);

58         }

59         int cnt=0,cnt2=0;

60         for (int i=1 ;i<=n ;i++) if (father[i]==i)

61         {

62             an[cnt].num=d[i] ;an[cnt].isCircle=isCircle[i];

63             cnt ++ ;

64         }

65         sort(an,an+cnt);

66 

67         scanf("%d%d",&n2,&m2);

68         memset(isCircle,0,sizeof(isCircle));

69         for (int i=1 ;i<=n2 ;i++) father[i]=i,d[i]=1;

70         for (int i=0 ;i<m2 ;i++)

71         {

72             scanf("%d%d",&u,&v);

73             Union(u,v);

74         }

75         for (int i=1 ;i<=n2 ;i++) if (father[i]==i)

76         {

77             bn[cnt2].num=d[i] ;bn[cnt2].isCircle=isCircle[i];

78             cnt2++;

79         }

80         sort(bn,bn+cnt2);

81 

82         printf("Case #%d: ",ncase++);

83         if (n!=n2 || m!=m2 || cnt!=cnt2) {printf("NO\n");continue; }

84         int flag=0;

85         for (int i=0 ;i<cnt ;i++)

86         {

87             if (an[i].num != bn[i].num) {flag=1;break; }

88             if (an[i].isCircle != bn[i].isCircle) {flag=1;break; }

89         }

90         if (flag) printf("NO\n");

91         else printf("YES\n");

92     }

93     return 0;

94 }

 

你可能感兴趣的:(HDU)