【HDOJ】1829 A Bug's Life

并查集变型。题意就是x与y是互斥的,下列是否数据是否可保证x-y是否均为互斥。

 1 #include <cstdio>
 2 #include <cstring>
 3 
 4 #define MAXN 2005
 5 
 6 int pre[MAXN];
 7 int opp[MAXN];
 8 int n;
 9 bool flag;
10 
11 int find(int x) {
12     return pre[x]==x ? x:pre[x]=find(pre[x]);
13 }
14 
15 void merge(int x, int y) {
16     if (!flag) return ;
17     x = find(x);
18     y = find(y);
19     if (x == y) {
20         flag = false;
21         return;
22     }
23     if (opp[x]) pre[opp[x]] = y;
24     if (opp[y]) pre[opp[y]] = x;
25     opp[x] = y;
26     opp[y] = x;
27 }
28 
29 int main() {
30     int case_n, m;
31     int i, j, x, y;
32 
33     scanf("%d", &case_n);
34 
35     for (j=1; j<=case_n; ++j) {
36         scanf("%d %d", &n, &m);
37         for (i=1; i<=n; ++i)
38             pre[i] = i;
39         memset(opp, 0, sizeof(opp));
40         flag = true;
41         while (m--) {
42             scanf("%d %d", &x, &y);
43             merge(x, y);
44         }
45         printf("Scenario #%d:\n", j);
46         if (flag)
47             printf("No suspicious bugs found!\n\n");
48         else
49             printf("Suspicious bugs found!\n\n");
50     }
51 
52     return 0;
53 }

 

posted on 2014-06-11 12:00  Bombe  阅读(232)  评论(0编辑  收藏  举报

导航