F - Hand in Hand HDU - 3926(解题报告)

        

3 2
1 2

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. 
Sample Input 2
2 3
3 2
3 2
2 1

3 3
1 2
2 3
3 1
3 1
1 2

            初看这道题,很容易就能够清楚这是一道要用到并查集的题。并查集详细的说明我觉得这篇文章很好。

             http://m.blog.csdn.net/article/details?id=7724401 

            以上是链接,题的分析就是也就是要考虑是否同构,同时也要考虑是否成环,或者是链状。

            用并查集因为要确定结点,所以我们就可以通过判断每个小朋友所对应的结点数是否一致。然后判断环或者链,如果都一样那么就符合题意。接下来是我的代码。

#include <stdio.h>
#include <algorithm>
using namespace std;
#define MAX 10005
int pre1[MAX],pre2[MAX];
int n1,n2;

//建造结构体来判断每个小盆友对应的结点数以及最后是否成环
struct dia{
int spo,cycle;
};
dia gra1[MAX],gra2[MAX];
/*int max(int x,int y)
{
if(x>y)
{
return x;
}
return y;
}
int min(int x,int y)
{
if(x<y)
{
return x;

return y;
}*/

//并查集需要的其中一个函数,查找。
int Find(int x,int pre[])
{
int r = x;
while(r!=pre[r])
{
r = pre[r];
}
int i = x,j;
while(pre[i]!=r)
{
j = pre[i];
pre[i] = r;
i = j;
}
return r;

//并查集所必需的另一个函数,合并。为了符合题意,做了是否成环的分类处理。
int mix(int x,int y,int pre[],dia gra[])
{
int fx = Find(x,pre),fy = Find(y,pre);
if(fx==fy)
{
gra[fx].cycle = 1;
}
else
{
/*pre[min(fx,fy)] += pre[min(fx,fy)];
gra[min(fx,fy)].spo += gra[max(fx,fy)].spo;*/
if(gra[fx].spo>=gra[fy].spo)
{
pre[fy] = pre[fx];
gra[fx].spo += gra[fy].spo;

else
{
pre[fx] = pre[fy];
gra[fy].spo += gra[fx].spo;
}
}
}

//sort排序用的函数
int cm(dia a,dia b)
{
if(a.spo<b.spo)
{
return true;
}
if(a.spo==b.spo&&a.cycle<b.cycle)
{
return true;
}
return false;
}

//判断最后是否同构的函数。
int judge(int n,dia a[],dia b[])
{
int i;
sort(a+1,a+n+1,cm);
sort(b+1,b+n+1,cm);
for(i=1;i<=n;++i)
{
if(a[i].spo!=b[i].spo||a[i].cycle!=b[i].cycle)
{
return 0;
}
}
return 1;
}
int main()
{
int T,N,ncase;
scanf("%d",&T);
ncase = 1;
int i,hand1,hand2,c1,c2,nc1,nc2,flag;
while(T--)
{
flag = 1;
scanf("%d%d",&n1,&nc1);
for(i=1;i<MAX;++i)
{
pre1[i] = i;
pre2[i] = i;
gra1[i].spo = 1;
gra2[i].spo = 1;
gra1[i].cycle = 0;
gra2[i].cycle = 0;
}//对数组进行初始化。
for(i=1;i<=nc1;++i)
{
scanf("%d%d",&hand1,&hand2);
mix(hand1,hand2,pre1,gra1);
}
scanf("%d%d",&n2,&nc2);
if(nc1!=nc2)
{
flag = 0;
}//从这里就先进行判断,如果小盆友们两两链接的个数不同,那么也就不用对其进行处理了。
for(i=1;i<=nc2;++i)
{
scanf("%d%d",&hand1,&hand2);
if(!flag)
{
continue;
}
else
{
mix(hand1,hand2,pre2,gra2);
}
}
flag = judge(n2,gra1,gra2);
if(flag)
{
printf("Case #%d: YES\n",ncase++);
}
else
{
printf("Case #%d: NO\n",ncase++);
}
}
return 0;
}

以上借鉴他人的,所以有雷同。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值