总时间限制:
10000ms
内存限制:
65536kB
描述
Hopper 博士正在研究一种罕见种类的虫子的性行为。他假定虫子只表现为两种性别,并且虫子只与异性进行交互。在他的实验中,不同的虫子个体和虫子的交互行为很容易区分开来,因为这些虫子的背上都被标记有一些标号。
现在给定一系列的虫子的交互,现在让你判断实验的结果是否验证了他的关于没有同性恋的虫子的假设或者是否存在一些虫子之间的交互证明假设是错的。
输入
输入的第一行包含实验的组数。每组实验数据第一行是虫子的个数(至少1个,最多2000个) 和交互的次数 (最多1000000次) ,以空格间隔. 在下面的几行中,每次交互通过给出交互的两个虫子的标号来表示,标号之间以空格间隔。已知虫子从1开始连续编号。
输出
每组测试数据的输出为2行,第一行包含 "Scenario #i:", 其中 i 是实验数据组数的标号,从1开始,第二行为 "No suspicious bugs found!" 如果实验结果和博士的假设相符,或 "Suspicious bugs found!" 如果Hopper的博士的假设是错误的
样例输入
2 3 3 1 2 2 3 1 3 4 2 1 2 3 4
样例输出
Scenario #1: Suspicious bugs found! Scenario #2: No suspicious bugs found!
提示
数据量比较大,建议使用scanf
带权查并集,且只有两种权。每个权代代表父结点与子节点之间相对关系即可。
用0代表同性,1代表异性
和食物链实在是太像了,甚至连变量名都没改。
食物链:动物王国_Mephisto1484的博客-CSDN博客
实现:
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include<stdio.h>
using namespace std;
struct group {
int index;//index 代表父结点
int weight;//weight 代表与父结点的关系,相同为0,异性为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) {//寻找,在此过程中完成路径压缩
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) { //在同一棵树中
if (root_a.weight == root_b.weight){//性别相同
cout << "Suspicious bugs found!" << endl<<endl;
flag = 0;
}
}
else {//不在一棵树中
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;
}
}