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.
PS:种类并查集的入门题吧,下面给出两种AC代码,大家可以参考第一种是将不同性的分成两拨,第二个是记录该点与根节点的关系,同性还是不同性。
1.
#include <iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<map>
#include<queue>
#include<set>
#include<cmath>
#include<stack>
#include<string>
const int maxn=1e5+100;
const int mod=1e9+7;
const int inf=1e9;
#define me(a,b) memset(a,b,sizeof(a))
typedef long long ll;
using namespace std;
int a[maxn];
int find(int x)
{
if(a[x]==x)
return x;
return a[x]=find(a[x]);
}
void un(int x,int y)
{
int x1=find(x),y1=find(y);
if(x1!=y1)
a[y1]=x1;
}
int main()
{
int t,tt=0;cin>>t;
while(t--)
{
int m,n;
scanf("%d%d",&n,&m);
for(int i=1;i<=2*n+10;i++)
a[i]=i;
int flog=0;
while(m--)
{
int x,y;scanf("%d%d",&x,&y);
int x1=find(x),y1=find(y);
if(x1!=y1)
un(y,x+n),un(x,y+n);
else
flog=1;
}
cout<<"Scenario #"<<++tt<<":"<<endl;
if(flog)
cout<<"Suspicious bugs found!"<<endl;
else
cout<<"No suspicious bugs found!"<<endl;
cout<<endl;
}
return 0;
}
2.
#include <iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<string>
#include<map>
#include<cmath>
#include<vector>
const int maxn=2e3+5;
const int mod=1e9+7;
#define me(a,b) memset(a,b,sizeof(a))
typedef long long ll;
using namespace std;
int vis[maxn],root[maxn],flog;//vis[x]存储x与其父亲节点的关系
int find(int x)
{
if(root[x]==x)
return x;
int x1=find(root[x]);
vis[x]=(vis[x]+vis[root[x]])%2;
root[x]=x1;
return root[x];
}
void un(int x,int y)
{
int x1=find(x),y1=find(y);
if(x1==y1&&vis[x]==vis[y])//vis得出x, y的关系为同性,又由题意得出输入的x, y是异性, 矛盾
flog=1;
else
{
vis[x1]=(vis[x]+vis[y]+1)%2;
root[x1]=y1;
}
}
int main()
{
int t,Case=1;cin>>t;
while(t--)
{
int m,n;cin>>n>>m;
for(int i=1;i<=n;i++)
root[i]=i;
me(vis,0),flog=0;
for(int i=0;i<m;i++)
{
int x,y;scanf("%d%d",&x,&y);
if(flog) continue ;
un(x,y);
}
cout<<"Scenario #"<<Case++<<":"<<endl;
if(flog)
cout<<"Suspicious bugs found!"<<endl;
else
cout<<"No suspicious bugs found!"<<endl;
cout<<endl;
}
return 0;
}