并查集入门题,小希的迷宫,hdu1272,注意在union时记录一下link和判换,有两两条路的就NO了。就是录入数据有点坑爹,初始化要做好,注意只有两个0,0 是也是yes。
ac代码:
/*
title:hdu1272
description:并查集入门题,
author: averyboy
time:
version:
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
using namespace std;
int father[100010];
int mark[100010];
int llink=0,num;
bool circle;
void init()
{
num=0;
for(int i=0;i<100010;i++)
{
father[i]=i;
}
return ;
}
int getfather(int i)
{
if(father[i]!=i)
{
father[i]=getfather(father[i]);
}
return father[i];
}
void unio(int x,int y)
{
int xfather=getfather(x);
int yfather=getfather(y);
//cout<
if(xfather!=yfather)
{
// cout<<"yes"<
father[xfather]=yfather;
llink++;
}
else
{
circle=true;
}
return ;
}
int main()
{
int m,n;
memset(mark,0,sizeof(mark));
init();
while(true)
{
scanf("%d%d",&m,&n);
if(m==0&&n==0)
{
init();
for(int i=1;i<=100010;i++)
{
if(mark[i]==1)
num++;
}
// cout<
if(num==0)
{
cout<<"Yes"<else if(llink==num-1&&circle==false)
{
cout<<"Yes"<else
cout<<"No"<0 ;
memset(mark,0,sizeof(mark));
circle=false;
}
else if(m==-1&&n==-1)
break;
else
{
mark[m]=1;
mark[n]=1;
unio(m,n);
}
}
return 0;
}