hdu 1272 小希的迷宫

题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=1272

小希的迷宫

Description

上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走。但是她设计迷宫的思路不一样,首先她认为所有的通道都应该是双向连通的,就是说如果有一个通道连通了房间A和B,那么既可以通过它从房间A走到房间B,也可以通过它从房间B走到房间A,为了提高难度,小希希望任意两个房间有且仅有一条路径可以相通(除非走了回头路)。小希现在把她的设计图给你,让你帮忙判断她的设计图是否符合她的设计思路。比如下面的例子,前两个是符合条件的,但是最后一个却有两种方法从5到达8。 

Input

输入包含多组数据,每组数据是一个以0 0结尾的整数对列表,表示了一条通道连接的两个房间的编号。房间的编号至少为1,且不超过100000。每两组数据之间有一个空行。 
整个文件以两个-1结尾。

Output

对于输入的每一组数据,输出仅包括一行。如果该迷宫符合小希的思路,那么输出"Yes",否则输出"No"。

Sample Input

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

Sample Output

Yes

Yes

No

杭电第100题纪念O(∩_∩)O~

 1 #include<algorithm>
 2 #include<iostream>
 3 #include<cstdlib>
 4 #include<cstring>
 5 #include<cstdio>
 6 #include<vector>
 7 #include<map>
 8 #include<set>
 9 using std::cin;
10 using std::cout;
11 using std::endl;
12 using std::find;
13 using std::sort;
14 using std::set;
15 using std::map;
16 using std::pair;
17 using std::vector;
18 using std::multiset;
19 using std::multimap;
20 #define pb(e) push_back(e)
21 #define sz(c) (int)(c).size()
22 #define mp(a, b) make_pair(a, b)
23 #define all(c) (c).begin(), (c).end()
24 #define iter(c) decltype((c).begin())
25 #define cls(arr,val) memset(arr,val,sizeof(arr))
26 #define cpresent(c, e) (find(all(c), (e)) != (c).end())
27 #define rep(i, n) for (int i = 0; i < (int)(n); i++)
28 #define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
29 const int N = 100010;
30 typedef unsigned long long ull;
31 struct UnionFind {
32     bool vis[N];
33     int par[N], rank[N];
34     inline void init(int n) {
35         rep(i, n) {
36             par[i] = i;
37             vis[i] = false, rank[i] = 0;
38         }
39     }
40     inline int find(int x) {
41         while (x != par[x]) {
42             x = par[x] = par[par[x]];
43         }
44         return x;
45     }
46     inline bool unite(int x, int y) {
47         x = find(x), y = find(y);
48         if (x == y) return false;
49         if (rank[x] < rank[y]) {
50             par[x] = y;
51         } else {
52             par[y] = x;
53             if (rank[x] == rank[y]) rank[x]++;
54         }
55         return true;
56     }
57     inline void work(int a, int b, bool &f) {
58         vis[a] = vis[b] = true;
59         if (!unite(a, b)) f = false;
60     }
61     inline void judge() {
62         int cnt = 0;
63         rep(i, N) {
64             // 题目给出的并不一定是只有一个集合,它可能给出多个集合,彼此并不联通 
65             if (vis[i] && par[i] == i) cnt++; 
66         }
67         puts(1 == cnt ? "Yes" : "No");
68     }
69 }go;
70 int main() {
71 #ifdef LOCAL
72     freopen("in.txt", "r", stdin);
73     freopen("out.txt", "w+", stdout);
74 #endif
75     int a, b;
76     while (~scanf("%d %d", &a, &b) && a + b != -2) {
77         if (!a && !b){ printf("Yes
"); continue; }
78         bool f = true;
79         go.init(N);
80         while (a || b) {
81             go.work(a, b, f);
82             scanf("%d %d", &a, &b);
83         }
84         if (!f) { puts("No"); continue; }
85         go.judge();
86     }
87     return 0;
88 }
View Code
By: GadyPu 博客地址:http://www.cnblogs.com/GadyPu/ 转载请说明
原文地址:https://www.cnblogs.com/GadyPu/p/4617132.html