A Bug's Life
Time Limit: 10000MS | Memory Limit: 65536K | |
Total Submissions: 28395 | Accepted: 9256 |
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.
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
关于带权向量的问题在poj 1182那篇里有详细的解释,不懂的先去我的博客分类 并查集里找出来看看这类题目就都会做了,这里就不再赘述。
AC代码:
#include<iostream>
#include<stdio.h>
using namespace std;
int f[2010],rank[2010];
int find(int x){
if(x==f[x])
return x;
int tf=f[x];
f[x]=find(f[x]);
rank[x]=(rank[x]+rank[tf])%2;
return f[x];
}
int main(){
int T; scanf("%d",&T);
for(int cas=1;cas<=T;cas++){
int n,m;
int sucess=0;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
f[i]=i;
rank[i]=0; //0表示i的父节点与自己同性,1为异性
}
while(m--){
int x,y;
scanf("%d%d",&x,&y);
int fx=find(x);
int fy=find(y);
if(fx!=fy){
f[fx]=fy;
rank[fx]=(1+rank[y]-rank[x]+2)%2;
}
else{
if(rank[x]==rank[y]){
sucess=1; //出现同性
while(m--){
scanf("%d%d",&x,&y);
}
break;
}
}
}
cout<<"Scenario #"<<cas<<":"<<endl;
if(sucess==1){
cout<<"Suspicious bugs found!"<<endl<<endl;
}
else
cout<<"No suspicious bugs found!"<<endl<<endl;
}
return 0;
}