您的位置:首页 > 其它

并查集 HDU1272

2015-08-10 18:27 387 查看
#include <iostream>
#include <map>
#include <cstring>
#include <cstdio>

using namespace std;

int father[100010];
int num[100010];

int main()
{
int a,b;
while(true)
{
int flag=1;
int time=1;
for(int i=1;i<=100000;i++)
father[i]=i;
for(int i=1;i<=100000;i++)
num[i]=1;
while(cin>>a>>b)
{
if(a==0&&b==0)
break;
if(a==-1&&b==-1)
goto F1;
time++;
int fa=a;
int fb=b;
while(fa!=father[fa])
{
fa=father[fa];
}
while(fb!=father[fb])
{
fb=father[fb];
}
int t1=a;
while(t1!=fa)
{
int tmp=father[t1];
father[t1]=fa;
t1=tmp;
}
int t2=b;
while(t2!=fb)
{
int tmp=father[t2];
father[t2]=fb;
t2=tmp;
}
if(fa==fb)
flag=0;
else
{
father[fb]=fa;
num[fa]+=num[fb];
num[fb]=0;
}
}
if(flag)
{
flag=0;
for(int i=1;i<=100000;i++)
{
if(num[i]==time)
{
flag=1;
}
}
}
if(flag)
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}
F1:
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: