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;
}
本文解析了一个名为ABug'sLife的编程竞赛题目,该题要求根据虫子间的交互判断是否存在同性恋行为,挑战选手的数据结构和算法能力。通过使用并查集算法解决此问题,代码中详细展示了如何实现并查集以及判断逻辑。
525

被折叠的 条评论
为什么被折叠?



