并查集杭电1272小希的迷宫
生活随笔
收集整理的這篇文章主要介紹了
并查集杭电1272小希的迷宫
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
| New~ 歡迎參加——計算機學院大學生程序設計競賽(新生為主) |
小希的迷宮Time Limit: 2000/1000 MS (Java/Others)????Memory Limit: 65536/32768 K (Java/Others) Input 輸入包含多組數據,每組數據是一個以0 0結尾的整數對列表,表示了一條通道連接的兩個房間的編號。房間的編號至少為1,且不超過100000。每兩組數據之間有一個空行。 整個文件以兩個-1結尾。
Sample Input 6 8 5 3 5 2 6 4 5 6 0 08 1 7 3 6 2 8 9 7 5 7 4 7 8 7 6 0 03 8 6 8 6 4 5 3 5 6 5 2 0 0-1 -1
using namespace std; int set[100001]; int rel[100001]; int find(int a) { ??? if(a==set[a]) return a; ??? else return set[a]=find(set[a]); } int main() { ??? int x,y; ??? while(cin>>x>>y) ??? { ??????? for(int i=1;i<=100001;i++) ??????? { ??????????? set[i]=i; ??????????? rel[i]=0; ??????? } ??????? if(x==0 && y==0) ??????? { ??????????? cout<<"Yes"<<endl; ??????????? continue; ??????? } ??????? int k=1; ??????? if(x==-1&&y==-1)break; ??????? else ??????? { ??????????? while(x!=0&&y!=0) ??????????? { ??????????????? rel[x]=1;rel[y]=1; ??????????????? int fx=find(x),fy=find(y); ??????????????? if(fx==fy)k=0; ??????????????? else set[fy]=fx; ??????????????? cin>>x>>y; ??????????? } ??????????? if(k==0) ??????????????? cout<<"No"<<endl; ??????????? else ??????????? { ??????????????? int t=0; ??????????????? for(int i=1;i<=100001;i++)???? //此步即判斷多個集合是否聯通 ??????????????? { ??????????????????? if(rel[i]&&set[i]==i) t++; ??????????????? } ??????????????? if(t==1) cout<<"Yes"<<endl; ??????????????? else cout<<"No"<<endl; ??????????? } ??????? } ??? } ??? return 0; } |
總結
以上是生活随笔為你收集整理的并查集杭电1272小希的迷宫的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: A cycle was detected
- 下一篇: 杭电2031进制转换