首页 > 代码库 > 【POJ】2492 A bug's life ——种类并查集

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

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

23 31 22 31 34 21 23 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     else24         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         else81             printf("No suspicious bugs found!\n");82         printf("\n");83     }84     return 0;85 }