LA 3644 易爆物

https://vjudge.net/problem/UVALive-3644

简单的并查集题目。

 1 #include<iostream>
 2 using namespace std;
 3 
 4 const int maxn = 100000 + 5;
 5 
 6 int p[maxn];
 7 
 8 int find(int x)
 9 {
10     return p[x] != x ? p[x] = (find(p[x])) : x;
11 }
12 
13 int main()
14 {
15     //freopen("D:\\txt.txt", "r", stdin);
16     int x, y;
17     while (cin >> x && x != -1)
18     {
19         for (int i = 0; i < maxn; i++)
20             p[i] = i;
21         int cnt = 0;
22         while (x != -1)
23         {
24             cin >> y;
25             x = find(x);
26             y = find(y);
27             if (x == y)   cnt++;
28             else p[x] = y;
29             cin >> x;
30         }
31         cout << cnt << endl;
32     }
33 }

 

posted @ 2017-03-11 14:40  Kayden_Cheung  阅读(122)  评论(0编辑  收藏  举报
//目录