您的位置:首页 > 其它

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

2017-01-16 10:20 253 查看
        

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?" 

InputThe 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. 
OutputFor each test case: output the case number as shown and "YES" if the two graph are isomorphism or "NO" otherwise. 
Sample Input2
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;

}

以上借鉴他人的,所以有雷同。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: