HDU 1272 小希的迷宮 基礎並查集★
阿新 • • 發佈:2018-11-01
https://blog.csdn.net/FJUT_ACM_cjt152/article/details/53018059
程式碼如下:
#include<bits\stdc++.h> using namespace std; int Father[1000]; int fg = 1; int FindFather(int x) { return Father[x] < 0 ? x : FindFather(Father[x]); } void Umerge(int x, int y) { int fx = FindFather(x);//fx,返回-1,或者1-的正數 int fy = FindFather(y); if (fx != fy) { Father[fy] += Father[fx];//father[X]<0時,存的是孩子的個數,大於0是上個節點數。 Father[fx] = fy; } else { if (Father[x] > 0 && Father[y] > 0)//在一個father下,還要連結 { fg = 0; } } } int main() { memset(Father, -1, sizeof(Father)); int u, v; while (cin>>u>>v) { if (u == 0 && v == 0) break; Umerge(u, v); } fg ? printf("yes\n") : printf("no"); }