题意:上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走。但是她设计迷宫的思路不一样,首先她认为所有的通道都应该是双向连通的,就是说如果有一个通道连通了房间A和B,那么既可以通过它从房间A走到房间B,也可以通过它从房间B走到房间A,为了提高难度,小希希望任意两个房间有且仅有一条路径可以相通(除非走了回头路)。小希现在把她的设计图给你,让你帮忙判断她的设计图是否符合她的设计思路。比如下面的例子,前两个是符合条件的,但是最后一个却有两种方法从5到达8。
思路:
并查集判环,顶点数=边数-1即可, 注意无顶点和边时是满足条件的,输出Yes。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
using namespace std;
#define inf 0x3f3f3f3f
#define ll long long
const int maxn=100005;
const double eps=1e-8;
const double PI = acos(-1.0);
ll gcd(ll a,ll b)
{
return b==0?a:gcd(b,a%b);
}
int pre[maxn];
void init()
{
for(int i=1;i<=maxn;i++)
{
pre[i]=i;
}
}
int find(int x)
{
if(x==pre[x])
return x;
else
{
int root=find(pre[x]);
return pre[x]=root;
}
}
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
int a,b;
while(cin>>a>>b)
{
set<int> s;
if(!a&&!b)
{
cout<<"Yes"<<endl;
continue;
}
init();
int flag=0,cnt=1;
s.insert(a),s.insert(b);
if(a==-1&&b==-1) break;
int fa=find(a),fb=find(b);
if(fa!=fb)
pre[fa]=fb;
else
flag=1;
for(;;)
{
int u,v;
cin>>u>>v;
if(!u&&!v) break;
cnt++;
s.insert(u),s.insert(v);
int fu=find(u),fv=find(v);
if(fu!=fv) pre[fu]=fv;
else
flag=1;
}
if(s.size()!=cnt+1) flag=1;
if(flag) cout<<"No"<<endl;
else cout<<"Yes"<<endl;
}
return 0;
}