您的位置:首页 > 其它

hdu 3926 Hand in Hand 同构图

2015-04-05 23:24 190 查看
题目链接: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?"

[align=left]Input[/align]
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.

[align=left]Output[/align]
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,则称他为一个自同构。
算法分析:由于是判断是否同构,我们就只需要判断一个集合里的节点数目是否相等和是否都是一个环就可以了。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#define inf 0x7fffffff
using namespace std;
const int maxn=10000+10;

int n,m,n2,m2;
int father[maxn],d[maxn],isCircle[maxn];
struct node
{
int num,isCircle;
friend bool operator < (node a,node b)
{
if (a.num!=b.num) return a.num>b.num;
return a.isCircle>b.isCircle;
}
}an[maxn],bn[maxn];

int findset(int x)
{
if (x==father[x]) return x;
return father[x]=findset(father[x]);
}
void Union(int x,int y)
{
x=findset(x) ;y=findset(y) ;
if (x==y) {isCircle[x]=1;return;}
if (d[x]>d[y])
{
father[y]=x;
d[x] += d[y];
}
else
{
father[x]=y;
d[y] += d[x];
}
}

int main()
{
int t,ncase=1;
scanf("%d",&t);
while (t--)
{
scanf("%d%d",&n,&m);
memset(isCircle,0,sizeof(isCircle));
for (int i=1 ;i<=n ;i++) father[i]=i,d[i]=1;
int u,v;
for (int i=0 ;i<m ;i++)
{
scanf("%d%d",&u,&v);
Union(u,v);
}
int cnt=0,cnt2=0;
for (int i=1 ;i<=n ;i++) if (father[i]==i)
{
an[cnt].num=d[i] ;an[cnt].isCircle=isCircle[i];
cnt ++ ;
}
sort(an,an+cnt);

scanf("%d%d",&n2,&m2);
memset(isCircle,0,sizeof(isCircle));
for (int i=1 ;i<=n2 ;i++) father[i]=i,d[i]=1;
for (int i=0 ;i<m2 ;i++)
{
scanf("%d%d",&u,&v);
Union(u,v);
}
for (int i=1 ;i<=n2 ;i++) if (father[i]==i)
{
bn[cnt2].num=d[i] ;bn[cnt2].isCircle=isCircle[i];
cnt2++;
}
sort(bn,bn+cnt2);

printf("Case #%d: ",ncase++);
if (n!=n2 || m!=m2 || cnt!=cnt2) {printf("NO\n");continue; }
int flag=0;
for (int i=0 ;i<cnt ;i++)
{
if (an[i].num != bn[i].num) {flag=1;break; }
if (an[i].isCircle != bn[i].isCircle) {flag=1;break; }
}
if (flag) printf("NO\n");
else printf("YES\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: