http://acm.hdu.edu.cn/showproblem.php?pid=1272
上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走。但是她设计迷宫的思路不一样,首先她认为所有的通道都应该是双向连通的,就是说如果有一个通道连通了房间A和B,那么既可以通过它从房间A走到房间B,也可以通过它从房间B走到房间A,为了提高难度,小希希望任意两个房间有且仅有一条路径可以相通(除非走了回头路)。小希现在把她的设计图给你,让你帮忙判断她的设计图是否符合她的设计思路。比如下面的例子,前两个是符合条件的,但是最后一个却有两种方法从5到达8。 |
#include<bits/stdc++.h>
#include<set>
#include<cstring>
using namespace std;
const int maxn=100000+5;
//并查集
int fa[maxn];
int find(int x)
{
return fa[x]==-1?x:fa[x]=find(fa[x]);
}
bool bind(int u,int v)
{
int fu=find(u);
int fv=find(v);
if(fu!=fv)
{
fa[fu]=fv;
return true;
}
return false;
}
int main()
{
int u,v;
while(cin>>u>>v&&u>=0){
if(u==0&&v==0){
cout<<"Yes"<<endl;
continue;
}
memset(fa,-1,sizeof(fa));
set<int>s;
bool ok=true;
do{
if(!bind(u,v))ok=false;
s.insert(u);
s.insert(v);
}while(cin>>u>>v&&u);
if(ok){
int cnt=0;
for(set<int>::iterator it=s.begin();it!=s.end();it++){
if(*it==find(*it))++cnt;
}
if(cnt>1)ok=false;
}
cout<<(ok?"Yes":"No")<<endl;
}
return 0;
}