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 }
时间: 2024-10-08 20:04:41

hdu 3926 Hand in Hand 同构图的相关文章

HDU 3926 图的同构

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3926 题意:给定2个顶点度最大为2的无向图.问你这2个无向图是否同构. 思路: 1.最大度为2.说明这个图可能有多个连通分量,每个连通分量要么是环,要么是链.2.然后遍历每个连通分量,记录该连通分量的结点个数,以及该连通分量是环还是链.3.将第一个图按照结点个数排序(若子结点个数相同,则对链先排序)4.将第二个图按照步骤三排序5.比较排序后,2个图是否每个元素都相等.若相等,则相似. 关于求链通分量

hdu 3926 hands in hands

https://vjudge.net/problem/HDU-3926 题意:有n个小朋友,他们之间手拉手,但是一只手只能拉一只手或者不拉,现在给出两个图,表示拉手关系,问这两个图是否同构.思路:一开始被同构难住了,后来思考发现,每一个联通分量只能是一条链或者一个简单的环,这样就比较好判断了.利用并查集统计每一个连通分量中的点,然后判断类型,判断类型的时候用度数是否为1来判断是否为链,然后将每一个连通分量先根据大小,再根据类型进行排序,最后把两个图进行一个比较即可. 1 #include <st

图论 500题——主要为hdu/poj/zoj

转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并查集======================================[HDU]1213   How Many Tables   基础并查集★1272   小希的迷宫   基础并查集★1325&&poj1308  Is It A Tree?   基础并查集★1856   More i

hdu图论题目分类

=============================以下是最小生成树+并查集====================================== [HDU] 1213 How Many Tables 基础并查集★ 1272 小希的迷宫 基础并查集★ 1325&&poj1308 Is It A Tree? 基础并查集★ 1856 More is better 基础并查集★ 1102 Constructing Roads 基础最小生成树★ 1232 畅通工程 基础并查集★ 123

题单二:图论500

http://wenku.baidu.com/link?url=gETLFsWcgddEDRZ334EJOS7qCTab94qw5cor8Es0LINVaGMSgc9nIV-utRIDh--2UwRLvsvJ5tXFjbdpzbjygEdpGehim1i5BfzYgYWxJmu ==========  以下是最小生成树+并查集=========================[HDU]1213         How Many Tables        基础并查集★1272         小

图论五百题!

生死看淡不服就淦,这才是人生! =============================以下是最小生成树+并查集======================================[HDU]1213 How Many Tables 基础并查集★1272 小希的迷宫 基础并查集★1325&&poj1308 Is It A Tree? 基础并查集★1856 More is better 基础并查集★1102 Constructing Roads 基础最小生成树★1232 畅通工程 基

并查集&amp;MST

[HDU] 1198 Farm Irrigation 基础最小生成树★ 1598 find the most comfortable road 枚举+最小生成树★★ 1811 Rank of Tetris 并查集+拓扑排序★★ 3926 Hand in Hand 同构图★ 3938 Portal 离线+并查集★★ 2489     Minimal Ratio Tree dfs枚举组合情况+最小生成树★ 4081     Qin Shi Huang's National Road System 最

图论精炼500题

忘了从哪转的了... =============================以下是最小生成树+并查集====================================== [HDU] 1213               How Many Tables                    基础并查集★ 1272               小希的迷宫                     基础并查集★ 1325&&poj1308    Is It A Tree?       

图论500题

=============================以下是最小生成树+并查集====================================== [HDU] 1213   How Many Tables   基础并查集★ 1272   小希的迷宫   基础并查集★ 1325&&poj1308  Is It A Tree?   基础并查集★ 1856   More is better   基础并查集★ 1102   Constructing Roads  基础最小生成树★ 12