把性别相同的虫子放在同一个集合,然后每读入一对虫子号,判断它们在不在同一集合,在则同性别,不在则继续
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int maxn = ;
int fa[maxn];
int Rank[maxn];
int group[maxn]; void init(int x){
fa[x] = x;
Rank[x] = ;
group[x] = ;
} int Find(int x){
if (x == fa[x])
return x;
else
return fa[x] = Find(fa[x]);
} void set_union(int x, int y){
int fx = Find(x);
int fy = Find(y);
if (Rank[fx] > Rank[fy]){
fa[fy] = fx;
}
else{
fa[fx] = fy;
if (Rank[fx] == Rank[fy])
Rank[fy]++;
}
} int main(){
int t;
int cnt = ;
scanf("%d", &t);
while (t--){
cnt++;
int n, m;
scanf("%d %d", &n, &m);
for (int i = ; i <= n; i++){
init(i);
}
bool ans = true;
while (m--){
int x;
int y;
scanf("%d %d", &x, &y);
if (ans == false)
continue;
if (Find(x) == Find(y)){
ans = false;
continue;
}
if (group[x] == )
group[x] = y;
else set_union(group[x], y);
if (group[y] == )
group[y] = x;
else
set_union(group[y], x); }
printf("Scenario #%d:\n", cnt);
if (!ans){
printf("Suspicious bugs found!\n");
}
else
printf("No suspicious bugs found!\n");
if (m != ){
printf("\n");
}
}
//system("pause");
return ;
}