A Bug’s Life

Time Limit: 10000MS Memory Limit: 65536K
Total Submissions: 49727 Accepted: 16056

Description

Background
Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they feature two different genders and that they only interact with bugs of the opposite gender. In his experiment, individual bugs and their interactions were easy to identify, because numbers were printed on their backs.
Problem
Given a list of bug interactions, decide whether the experiment supports his assumption of two genders with no homosexual bugs or if it contains some bug interactions that falsify it.
Input

The first line of the input contains the number of scenarios. Each scenario starts with one line giving the number of bugs (at least one, and up to 2000) and the number of interactions (up to 1000000) separated by a single space. In the following lines, each interaction is given in the form of two distinct bug numbers separated by a single space. Bugs are numbered consecutively starting from one.
Output

The output for every scenario is a line containing “Scenario #i:”, where i is the number of the scenario starting at 1, followed by one line saying either “No suspicious bugs found!” if the experiment is consistent with his assumption about the bugs’ sexual behavior, or “Suspicious bugs found!” if Professor Hopper’s assumption is definitely 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!
Hint

Huge input,scanf is recommended.

题目大意:有个科学家要研究昆虫性行为,给出n只昆虫,和m条交互信息表,如果发现交互信息表有同性交互输出found,否则输出not fonund。
思路:
这个有可以用二分图染色法解答:把每只昆虫看作一个顶点,他们之间交互信息就是边,然后bfs搜索。
例如:
1 2
2 3
1设置黑色,则2白色。
2白色,则3黑色,没有同性行为,not found。
例如:
1 2
2 3
1 3
1黑色,则2白色。
2白色,则3黑色。
1 3 同色,所以又同性行为,found。
代码:

#include<iostream>
#include<queue>
#include<vector>
#include<cstring>
using namespace std;
const int maxn=1e3+10;
int t,color[maxn];//color 0尚未染色 1红色 2黑色 
vector<int >g[maxn*3];//存邻接表 
int n,m;

void nin(){
	memset(color,0,sizeof(color));
	for(int i=1;i<=n;i++)
	g[i].clear();
}

bool bfs(int x){
	queue<int >st;
	st.push(x);
	if(color[x]==0){//当前点尚未染色 
		color[x]=1;//染色 
	}
	while(!st.empty()){
		int u=st.front();st.pop();
		for(int i=0;i<g[u].size();i++){
			int v=g[u][i];
			if(color[v]){//如果有颜色 
					if(color[v]==color[u]){
				    return 0;
			}
			}else{//没有颜色,给他上色!!!!!! 
				color[v]=3-color[u];//染对立色 
				st.push(v); 
			}
		}
	}
	return 1; 
}
int main(){
	scanf("%d",&t);
	int a,b;
	for(int py=1;py<=t;py++){
		scanf("%d%d",&n,&m);
		nin();
		for(int i=1;i<=m;i++){
			scanf("%d%d",&a,&b);
			g[a].push_back(b);//vector建立邻接表 
			g[b].push_back(a); 
		}
		bool flag=0;
		for(int i=1;i<=n;i++){
			if(bfs(i)==0){
				flag=1;
				break;
			}
		}
		printf("Scenario #%d:\n",py);
		if(flag){
			printf("Suspicious bugs found!\n\n");
		}else{
			printf("No suspicious bugs found!\n\n");
		}
	}
}

并查集代码:

#include<iostream>
using namespace std;

const int maxn=2e3+10;
int test,n,m,a,b;
int f[maxn],r[maxn];
int find(int x){
	if(x!=f[x]){
		int t=f[x];
		f[x]=find(f[x]);
		r[x]^=r[t];
	}
	return f[x];
}
bool ufs(int a,int b){
	int x=find(a);
	int y=find(b);
	if(x!=y){
		f[x]=y;
		r[x]=(r[a]+r[b]+1)%2;
		return true;
	}else{
		return false;
	}
}
int main(){
	scanf("%d",&test);
	for(int t=1;t<=test;t++){
		bool flag=false;
		scanf("%d%d",&n,&m);
		for(int i=1;i<=n;i++){
			r[i]=0;
			f[i]=i;
		}
		for(int i=1;i<=m;i++){
			scanf("%d%d",&a,&b);
			if(!ufs(a,b)&&r[a]==r[b])
			flag=true;
		}
		cout<<"Scenario #"<<t<<":"<<endl;
		if(flag){
			cout<<"Suspicious bugs found!"<<endl;
		}else{
			cout<<"No suspicious bugs found!"<<endl;
		} 
		cout<<endl;
	}
}