跳到正文

题解

A Bug's Life

wanghaoran000约 2 分钟
原文
文章信息与版本

文章信息

发布
2026年7月20日
收录
2026年7月20日
更新
2026年11月14日
篇幅
约 2 分钟

版本

  1. v1当前

题意
有一个研究,理论为昆虫之间只存在异性交配,给出多组互动记录,判断这些记录是否支持该理论。
思路
对于每次互动,规定它们必须各自属于两个集合中,如果发现已标记的两个属于同性的昆虫发生互动,标记可疑。最后检查是否违规即可。
代码

C++
#include<bits/stdc++.h>
using namespace std;
const int maxn=2e5+5;
const int maxm=1e6+5;
int n,m;
int fa[maxn*2+maxm];
int cnt;
int find(int x){
	if(fa[x]==x){
		return x;
	}else{
		return fa[x]=find(fa[x]);
	}
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	int T;
	cin>>T;
	int P=1;
	int o=0;
	while(T--){
		o=0;
		cnt=0;
		cin>>n>>m;
		for(int i=1;i<=2*n;i++){
			fa[i]=i;
		}
		int x,y;
		for(int i=0;i<m;i++){
			cin>>x>>y;
			if(find(x)==find(y)){
				o=1;
			}
			fa[find(x+n)]=find(y);
			fa[find(y+n)]=find(x);
		}
		if(o==0){
			cout<<"Scenario #"<<P<<":"<<'\n'<<"No suspicious bugs found!"<<'\n'<<'\n';
			P++;
		}else{
			cout<<"Scenario #"<<P<<":"<<'\n'<<"Suspicious bugs found!"<<'\n'<<'\n';
			P++;
		}
	}
	return 0;
} 
原文