小希的迷宫
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 50066 Accepted Submission(s):
15594
整个文件以两个-1结尾。
#include<bits/stdc++.h>
using namespace std;
const int N=100000+5;
int f[N];
bool vis[N];
int getf(int v){return f[v]==v?v:f[v]=getf(f[v]);}
void calc()
{
for(int i=1;i<=100000;++i) f[i]=i;
memset(vis,0,sizeof(vis));
}
int main()
{
int i,j,k,a,b;
while(cin>>a>>b){bool ok=1;
if(a==-1&&b==-1) break;
else if(a==0&&b==0) {puts("Yes");continue;}
calc();
f[b]=a;
vis[a]=vis[b]=1;
while(cin>>a>>b){
if(a==0&&b==0) break;
vis[a]=vis[b]=1;
int ra=getf(a),rb=getf(b);
if(ra==rb) ok=0;
f[rb]=ra;
}int s=0;
for(i=1;i<=100000;++i){
if(vis[i]&&getf(i)==i) s++;
if(s>1) {ok=0;break;}
}
ok?puts("Yes"):puts("No");
}
return 0;
}