题目大意
给出昆虫编号,看昆虫能否交配,如果出现同性交配或者自我交配的情况,则出现BUG。
思路分析
显然是一道关系并查集可以过的小水题,自从做了食物链感觉所有并查集都是水题。。。
并查集基础
tips
- 一定要scanf
- 两个test case之间要有额外的换行符
以后再用cin我是猪
#include<iostream>
#include<vector>
#include<algorithm>
#include<cmath>
#include<string>
#include<string.h>
#include<map>
#include<cstdio>
using namespace std;
#define ll int
#define MAX 2005
ll kind[MAX * 2];
ll find(ll a) {
if (a == kind[a]) return a;
else return kind[a] = find(kind[a]);
}
void unite(ll a, ll b) {
kind[find(b)] = kind[find(a)];
}
int main() {
ll N; cin >> N;
for (int i = 1; i <= N; i++) {
ll n, m, a, b, sign = 1; scanf("%d%d", &n, &m);
for (int i = 0; i <= n * 2; i++)kind[i] = i;
for (int j = 0; j < m; j++) {
scanf("%d%d", &a, &b);
if (sign) {
// a与b是不同性别 1-n:性别1 n+1-2n:性别2
if (find(a) == find(b)) sign = 0;
else {
unite(a, n + b); unite(a + n, b);
}
}
}
printf("Scenario #%d:\n", i);
if (sign)cout << "No suspicious bugs found!" << endl << endl;
else cout << "Suspicious bugs found!" << endl << endl;
}
}