Topic link: Point me
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.
Title:
Judging whether homosexual behavior exists;
Train of thought:
To collect or group and collect with weight.
Let's start with simple grouping and collecting. We need to add two points into each group separately and merge them. Then we can judge whether there is conflict or not.
We need to use weights to maintain whether the two insects are of the same sex. Here we use 0 and 1 to represent the two sexes.
Weighted and code collection;
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<iostream>
using namespace std;
int f[2000+10];
int ran[2000+10];
bool flag = false;
int getf(int x){
if(x == f[x]) return x;
int t = getf(f[x]);
ran[x] = (ran[x] + ran[f[x]]) & 1;
f[x] = t;
return t;
}
void Merge(int x,int y){
int t1 = getf(x);
int t2 = getf(y);
if(t1 != t2){
f[t2] = t1;
ran[t2] = (ran[x] + ran[y] + 1) & 1;Because more than one side.,So you need to add 1 ,
return ;
}
if(ran[x] == ran[y])
flag = true;
}
int main(){
int t;
scanf("%d", &t);
int kase = 0;
while(t--){
int n, m;
scanf("%d %d", &n, &m);
for(int i = 1; i <= n;++i)
f[i] = i,ran[i] = 0;
flag = false;
while(m--){
int x, y;
scanf("%d %d", &x, &y);
if(flag)
continue;
Merge(x,y);
}printf("Scenario #%d:\n",++kase);
if(flag)
printf("Suspicious bugs found!\n");
else printf("No suspicious bugs found!\n");
printf("\n");
}
return 0;
}
Grouping and lookup code:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<iostream>
using namespace std;
int f[2000 * 2+10];
int ran[2000+10];
bool flag = false;
int getf(int x){
if(x == f[x]) return x;
return f[x] = getf(f[x]);
}
void Merge(int x,int y){
int t1 = getf(x);
int t2 = getf(y);
if(t1 != t2)
f[t2] = t1;
}
int main(){
int t;
scanf("%d", &t);
int kase = 0;
while(t--){
int n, m;
scanf("%d %d", &n, &m);
for(int i = 1; i <= n + n;++i)
f[i] = i;
flag = false;
while(m--){
int x, y;
scanf("%d %d", &x, &y);
if(getf(x) == getf(y) || getf(x + n) == getf(y + n))
flag =true;//Judging whether homosexuality already exists
if(flag)
continue;
Merge(x,y + n);
Merge(x + n, y);//Join and check the centralization separately
}printf("Scenario #%d:\n",++kase);
if(flag)
printf("Suspicious bugs found!\n");
else printf("No suspicious bugs found!\n");
printf("\n");
}
return 0;
}