【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

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代码:
 
 #include <cstdio>
#include <cstring> const int LEN = ; int uset[LEN];
int opp[LEN]; //代表每个虫子的配对异性
int rank[LEN];
int n; void makeset() //初始化并查集
{
for(int i = ; i <= n; i++)
uset[i] = i;
memset(opp, , sizeof(opp));
memset(rank, , sizeof(rank));
} int findset(int x)
{
if (x == uset[x])
return x;
else
uset[x] = findset(uset[x]);
return uset[x];
} void unionset(int x, int y)
{
if ((x = findset(x)) == (y = findset(y)))
return;
if (rank[x] > rank[y]) //按秩合并
uset[y] = x;
else{
uset[x] = y;
if (rank[x] == rank[y])
++rank[y];
}
} int main()
{
int T;
scanf("%d", &T);
for(int cnt = ; cnt <= T; cnt++){
int m;
scanf("%d %d", &n, &m);
makeset();
int f = ;
for(int i = ; i < m; i++){
int x, y;
scanf("%d %d", &x, &y);
if (f)
continue;
if (findset(x) == findset(y)){ //如果这两个虫子在同一个集合里找到 就是同性恋
f = ;
continue;
}
if (opp[x] == && opp[y] == ){ //代表两个虫子都没被分类过,储存下每个虫子的异性
opp[x] = y;
opp[y] = x;
}
else if (opp[x] == ){ //如果x没被分类过,将它的异性记录为y,并将它加入y的异性所在的集合
opp[x] = y;
unionset(x, opp[y]);
}
else if (opp[y] == ){ //同上
opp[y] = x;
unionset(y, opp[x]);
}
else{ //否者合并x也opp[y]所在的集合以及y与opp[x]所在的集合
unionset(x, opp[y]); unionset(y, opp[x]);
}
}
printf("Scenario #%d:\n", cnt);
if (f)
printf("Suspicious bugs found!\n");
else
printf("No suspicious bugs found!\n");
printf("\n");
}
return ;
}
上一篇:Web大文件分片上传


下一篇:web选择文件夹上传