Total time limit:
10000ms
Memory limit:
65536kB
describe
Dr. Hopper is studying the sexual behavior of a rare species of insects. He assumes that insects are only two sexes, and that insects interact only with the opposite sex. In his experiment, the interaction behavior of different insect individuals and insects was easy to distinguish, because the backs of these insects were marked with some labels.
Now, given a series of insect interactions, let you judge whether the results of the experiment verify his hypothesis that there are no homosexual insects, or whether there are some insect interactions to prove that the hypothesis is wrong.
input
The first line entered contains the number of groups for the experiment. The first row of each group of experimental data is the number of insects (at least 1, up to 2000) and the number of interactions (up to 1000000), which are separated by spaces. In the following rows, each interaction is represented by the labels of the two interactive insects, which are separated by spaces. It is known that insects are numbered consecutively from 1.
output
The output of each group of test data is 2 lines. The first line contains "Scenario #i:", where I is the label of the number of experimental data groups. Starting from 1, the second line is "no supplementary bugs found!" if the experimental results are consistent with the doctor's hypothesis, or "supplementary bugs found!" if the doctor's hypothesis is 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!
Tips
The amount of data is large, so it is recommended to use scanf
There are only two kinds of weights. Each weight generation represents the relative relationship between the parent node and the child node.
Use 0 for the same sex and 1 for the opposite sex
It is so similar to the food chain that even the variable name has not been changed.
Food chain: Animal Kingdom _mephisto1484blog - CSDN blog
realization:
#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include<stdio.h> using namespace std; struct group { int index;//index represents the parent node int weight;//weight represents the relationship with the parent node. The same is 0 and the opposite is 1 group() { index = 0; weight = 0; } group(int a, int b) :index(a), weight(b) {}; }; group *animals; bool exist(int num) { if (animals[num].index)return 1; else return 0; } group find(int num) {//Find and complete path compression in the process if (animals[num].index != num) { group tmp=find(animals[num].index); animals[num].index = tmp.index; animals[num].weight = (animals[num].weight + tmp.weight) % 2; } return animals[num]; } int main(){ int N; cin >> N; int n, k; int a, b; for (int k_ = 1; k_ <= N; k_++) { cout << "Scenario #" << k_ << ":" << endl; scanf("%d%d", &n, &k); animals = new group[n + 10]; bool flag = 1; while(k--){ scanf("%d%d", &a, &b); if (!flag)continue; if (exist(a)) { if (exist(b)) { group root_a = find(a), root_b = find(b); //cout <<" "<< root_a.index << " "<<root_a.weight<<" " << root_b.index <<" "<<root_b.weight<< endl; if (root_a.index == root_b.index) { //In the same tree if (root_a.weight == root_b.weight){//Same sex cout << "Suspicious bugs found!" << endl<<endl; flag = 0; } } else {//Not in a tree animals[root_b.index].index = root_a.index; animals[root_b.index].weight = (root_a.weight - root_b.weight +1) % 2; //for (int i = 1; i <= N; i++) cout << animals[i].index << " " << animals[i].weight << endl; } } else { animals[b].index = a; animals[b].weight = 1; } } else { if (exist(b)) { animals[a].index = b; animals[a].weight = 1; } else { animals[a].index = a; animals[b].index = a; animals[b].weight = 1; } } } //for (int i = 1; i <= n; i++) cout << animals[i].index << " " << animals[i].weight << endl; if (flag)cout << "No suspicious bugs found!" << endl<<endl; } }