码迷,mamicode.com
首页 > 其他好文 > 详细

LA 3644 易爆物

时间:2017-03-11 15:56:34      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:并查集   max   http   stdin   cin   class   const   live   div   

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 }

 

LA 3644 易爆物

标签:并查集   max   http   stdin   cin   class   const   live   div   

原文地址:http://www.cnblogs.com/zyb993963526/p/6534907.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!