5 2 1 2 2 3 1 3 5 2 1 2 3 4 1 4 5 2 1 2 3 4 2 5
same not sure not sure
///用并查集的方法,用于解决根据已知条件,判断所问的是否相关
#include<stdio.h>
#include<string.h>
#define N 100001
int bin[N];
int find(int x)
{
int r,i,j;
r=x;
while(r!=bin[r])
{
r=bin[r];
}
///压缩路径
i=x;
while(i!=r)
{
j=bin[i];
bin[i]=r;
i=j;
}
return r;
}
void merge(int x,int y)
{
int fx,fy;
fx=find(x);
fy=find(y);
if(fx!=fy)
bin[fx]=fy;
}
int main()
{
int n,m,i,a,b,c,d;
while(~scanf("%d%d",&n,&m))
{
for(i=1;i<=n;i++)
{
bin[i]=i;
}
for(i=0;i<=m-1;i++)
{
scanf("%d%d",&a,&b);
merge(a,b);
}
scanf("%d%d",&c,&d);
if(find(c)==find(d))
{
printf("same\n");
}
else
{
printf("not sure\n");
}
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/sh_tomorrow/article/details/47661305