A Bug's Life
Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 14399 Accepted Submission(s): 4694
Problem 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
这次不是判断是否同个集合了 而是不同集合了 才发现我对并查集一无所知。。。
题意是输入多组相同性别的bug 问是否出现矛盾。。比如:
1 2
2 3
1 3
1 2不同性别 2 3不同性别 那就是1 3性别相同 显然矛盾。。。
大概思路 就。。按照性别来划分并查集的子树吧(感觉很超时)结果786MS过了好像。。
看代码标注吧
#include<iostream>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;
int par[2500];
int vis[2500];//性别标记
int flag;
void init(int x) {
for (int i = 0; i<=x; i++) {
par[i] = i;
vis[i]=0;
}
}
int find(int x) {
if (par[x] == x) return x;
else
return par[x]=find(par[x]);
}
void unite(int x, int y) {
int a = find(x);
int b = find(y);
if (a == b){
return ;
}
else {
par[a] = b;
}
return ;
}
int main() {
int t, m, n,i,a,b;
int o=1;
scanf("%d",&t);
while(t--){
flag=0;
scanf("%d%d",&m,&n);
init(m);
for(i=0;i<n;i++){
scanf("%d%d",&a,&b);
int aa=find(a);
int bb=find(b);
if(aa==bb) flag=1;//如果输入的a,b同个集合 显然矛盾了。
else{
if(vis[aa]==0)
vis[aa]=bb;
//如果自己的异性集合为空 就把自己的异性加上去
else
unite(bb, vis[aa]); //如果不空,把bb这个异性加入并查集中
if(vis[bb]==0)
vis[bb]=aa;
else
unite(aa, vis[bb]);
}//说到底就是变成按种类的分并查集了,分为男性与女性,一旦让输入的两只bug在同一集合就矛盾!!
//并查集其实不容易就算有模板,还是多做几道题提高自己的姿势水平吧!!
}
printf("Scenario #%d:\n",o++);
if (flag) cout << "Suspicious bugs found!" << endl;
else cout << "No suspicious bugs found!" << endl;
cout<<endl;
}
}