A Bug's Life
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.
Source
TUD Programming Contest 2005, Darmstadt, Germany
|
搞基的虫子。。。k组测试数据。每组测试数据有n个虫子和m组关系。。。每组关系是两只虫子在少儿不宜。。。自然是异性的。但是奇型种会搞基。最后让判断有没有搞基的虫子。
自己写了个向量偏移。。。结果WA了两天。还是看题解明白的。
http://www.cnblogs.com/liushang0419/archive/2011/04/26/2029835.html
#include <cstdio>
int set[5010],offset[5010];
int n,m,k,i,j,x,y;
bool flag;
int FindSet(int x);
void Union(int x, int y);
int main()
{
scanf("%d",&k);
for (int i = 1; i <= k; ++i){
scanf("%d%d", &n, &m);
flag = true;
for (j=1;j<=n;++j){
set[j] = j;
offset[j] = 0;
}
for (j = 1; j <= m; ++j){
scanf("%d%d", &x, &y);
if (flag)
if (FindSet(x) != FindSet(y))
Union(x, y);
else
if (offset[x] == offset[y]) flag=0;
}
if (flag)
printf("Scenario #%d:\nNo suspicious bugs found!\n", i);
else
printf("Scenario #%d:\nSuspicious bugs found!\n", i);
if (i != k) printf("\n");
}
return 0;
}
int FindSet(int x){
if (x != set[x]){
int temp=set[x];
set[x] = FindSet(set[x]);
offset[x]^=offset[temp];
}
return set[x];
}
void Union(int x, int y){
int fx = FindSet(x);
int fy = FindSet(y);
set[fy] = fx;
offset[fy] = offset[x]^offset[y]^1;
return;
}
kdwycz的网站: http://kdwycz.com/
kdwyz的刷题空间:http://blog.csdn.net/kdwycz