易爆物(X-Plosives )基础并查集

 1 #include <iostream>
 2 #include <algorithm>
 3 using namespace std;
 4 const int maxn = 100000 + 10;
 5 int fa[maxn];
 6 
 7 int Find(int x){
 8     if (x == fa[x])
 9         return x;
10     else
11         return fa[x] = Find(fa[x]);
12 }
13 
14 int main(){
15     int a, b;
16     while (cin >> a){
17         
18         //init
19         for (int i = 0; i < maxn; i++)
20             fa[i] = i;
21         int ans = 0;
22         while (a != -1){
23             cin >> b;
24             a = Find(a);
25             b = Find(b);
26             if (a == b)
27                 ans++;
28             else
29                 fa[b] = a;
30             cin >> a;
31         }
32         cout << ans << endl;
33     }
34     //system("pause");
35     return 0;
36 }

 

posted @ 2017-11-10 18:18  ouyang_wsgwz  阅读(198)  评论(0编辑  收藏  举报