标签:
6 8 5 3 5 2 6 4 5 6 0 0 8 1 7 3 6 2 8 9 7 5 7 4 7 8 7 6 0 0 3 8 6 8 6 4 5 3 5 6 5 2 0 0 -1 -1
Yes Yes No
最近在做bin神挂的题,按顺序一点点来吧
#include <iostream> #include <stdio.h> #include <string> #include <cstring> #include <algorithm> #include <cmath> #define N 111129 using namespace std; int fa[N]; int vis[N]; int findfa(int x) { if(fa[x]!=x) fa[x]=findfa(fa[x]); return fa[x]; } void uniontwo(int x,int y) { int xx=findfa(x); int yy=findfa(y); if(xx<yy) fa[yy]=xx; else fa[xx]=yy; } int main() { int a,b; while(~scanf("%d %d",&a,&b)) { if(a==-1 && b==-1) break; int min=100019; int max=-109; int flag=0; if(a==0 && b==0) { cout<<"Yes\n"; continue; } for(int i=0;i<=111119;i++) { fa[i]=i; vis[i]=0; } while(a||b) { if(a<min) min=a; if(b<min) min=b; if(a>max) max=a; if(b>max) max=b; vis[a]=1;vis[b]=1; int xx=findfa(a); int yy=findfa(b); if(xx==yy)//判一下有不有环 flag=1; else uniontwo(a,b); scanf("%d %d",&a,&b); } int num=0; if(flag==1) cout<<"No"<<endl; else { for(int i=min;i<=max;i++)//范围内出现过的数,判一下是不是只有一个连通块 { if(vis[i] && fa[i]==i) num++; } if(num==1) cout<<"Yes"<<endl; else cout<<"No"<<endl; } } return 0; }
标签:
原文地址:http://blog.csdn.net/wust_zjx/article/details/46342329