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

【POJ】2492 A bug's life ——种类并查集

时间:2014-08-05 03:04:08      阅读:242      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   color   os   strong   io   for   

A Bug‘s Life

Time Limit: 10000MS   Memory Limit: 65536K
Total Submissions: 28211   Accepted: 9177

Description

Background 
Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they feature two different genders and that they only interact with bugs of the opposite gender. In his experiment, individual bugs and their interactions were easy to identify, because numbers were printed on their backs. 
Problem 
Given a list of bug interactions, decide whether the experiment supports his assumption of two genders with no homosexual bugs or if it contains some bug interactions that falsify it.

Input

The first line of the input contains the number of scenarios. Each scenario starts with one line giving the number of bugs (at least one, and up to 2000) and the number of interactions (up to 1000000) separated by a single space. In the following lines, each interaction is given in the form of two distinct bug numbers separated by a single space. Bugs are numbered consecutively starting from one.

Output

The output for every scenario is a line containing "Scenario #i:", where i is the number of the scenario starting at 1, followed by one line saying either "No suspicious bugs found!" if the experiment is consistent with his assumption about the bugs‘ sexual behavior, or "Suspicious bugs found!" if Professor Hopper‘s assumption is definitely wrong.

Sample Input

2
3 3
1 2
2 3
1 3
4 2
1 2
3 4

Sample Output

Scenario #1:
Suspicious bugs found!

Scenario #2:
No suspicious bugs found!

Hint

Huge input,scanf is recommended.
 
题解:种类并查集,假设之前输入的两个放在一起的虫子不是同性恋,那么他们就是互为异性,分别将他们加入两个不同的集合,每次输入新的一组虫子时,先查询他们在不在同一集合,若在,则为同性恋,否则分别将两个虫子所在的集合与其已存过的或者根节点的异性所在的集合合并,直到最后。
 
AC代码:
 
 1 #include <cstdio>
 2 #include <cstring>
 3 
 4 const int LEN = 2020;
 5 
 6 int uset[LEN];
 7 int opp[LEN]; //代表每个虫子的配对异性
 8 int rank[LEN];
 9 int n;
10 
11 void makeset()  //初始化并查集
12 {
13     for(int i = 0; i <= n; i++)
14         uset[i] = i;
15     memset(opp, 0, sizeof(opp));
16     memset(rank, 0, sizeof(rank));
17 }
18 
19 int findset(int x)
20 {
21     if (x == uset[x])
22         return x;
23     else
24         uset[x] = findset(uset[x]);
25     return uset[x];
26 }
27 
28 void unionset(int x, int y)
29 {
30     if ((x = findset(x)) == (y = findset(y)))
31         return;
32     if (rank[x] > rank[y])  //按秩合并
33         uset[y] = x;
34     else{
35         uset[x] = y;
36         if (rank[x] == rank[y])
37             ++rank[y];
38     }
39 }
40 
41 int main()
42 {
43     int T;
44     scanf("%d", &T);
45     for(int cnt = 1; cnt <= T; cnt++){
46         int m;
47         scanf("%d %d", &n, &m);
48         makeset();
49         int f = 0;
50         for(int i = 0; i < m; i++){
51             int x, y;
52             scanf("%d %d", &x, &y);
53             if (f)
54                 continue;
55             if (findset(x) == findset(y)){  //如果这两个虫子在同一个集合里找到 就是同性恋
56                 f = 1;
57                 continue;
58             }
59             if (opp[x] == 0 && opp[y] == 0){  //代表两个虫子都没被分类过,储存下每个虫子的异性
60                 opp[x] = y;
61                 opp[y] = x;
62             }
63             else if (opp[x] == 0){ //如果x没被分类过,将它的异性记录为y,并将它加入y的异性所在的集合
64                 opp[x] = y;
65                 unionset(x, opp[y]);
66             }
67             else if (opp[y] == 0){ //同上
68                 opp[y] = x;
69                 unionset(y, opp[x]);
70             }
71             else{  //否者合并x也opp[y]所在的集合以及y与opp[x]所在的集合
72                 unionset(x, opp[y]);
73 
74                 unionset(y, opp[x]);
75             }
76         }
77         printf("Scenario #%d:\n", cnt);
78         if (f)
79             printf("Suspicious bugs found!\n");
80         else
81             printf("No suspicious bugs found!\n");
82         printf("\n");
83     }
84     return 0;
85 }

 

【POJ】2492 A bug's life ——种类并查集,布布扣,bubuko.com

【POJ】2492 A bug's life ——种类并查集

标签:des   style   blog   color   os   strong   io   for   

原文地址:http://www.cnblogs.com/kevince/p/3891379.html

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