您的位置:首页 > 其它

hdu 1272 小希迷宫 (并查集)

2014-06-30 17:51 260 查看
/*hdu 1272 (并查集)
注意点的检查(由于这个原因交了很多次)
*/
#include<iostream>
#include<cstdio>
#include<list>
using namespace std;
const int N=100004;
int father
;//并查集存储
void Make_set()
{
for(int i=0;i<N;i++)
{
father[i]=i;
}
}
int Find_set(int x)
{
if(father[x]!=x)
{
father[x]=Find_set(father[x]);
}
return father[x];
}
bool Union(int a,int b)
{
int fx=Find_set(a);
int fy=Find_set(b);
if(fx==fy)
{
return false;
}
else
{
if(fx>fy)
{
father[fx]=fy;
}
else
{
father[fy]=fx;
}
}
return true;
}
int main()
{
int a,b;
while(scanf("%d%d",&a,&b)==2)
{
list<int> sign;//对点的检查
Make_set();
if(a==-1&&b==-1) break;
int flag=true;
if(a==0&&b==0)
{
cout<<"Yes"<<endl;
continue;
}
if(Union(a,b)==false) flag=false;
sign.push_back(a);
sign.push_back(b);
while(scanf("%d%d",&a,&b)==2)
{
if(a==0&&b==0) break;
if(Union(a,b)==false) flag=false;
sign.push_back(a);
sign.push_back(b);
}
sign.unique();
int sum=0;
while(!sign.empty())
{
if(father[sign.front()]==sign.front())
{
sum++;
}
sign.pop_front();
}
if(sum>1)
{
flag=false;
}
if(flag)
{
cout<<"Yes"<<endl;
}
else
{
cout<<"No"<<endl;
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: