题目:
链接:点击打开链接
题意:
给定虫子的交配关系,确定实验是否支持教授的假设即没有同性恋或者不符合假设。
思路:
是一道基础的并查集题目。存在两个集合异性和同性,给出多组关系,看这两个集合有木有联系,即是否有同性恋。
定义一个数组sex[],sex[i]表示与编号i的性别相反的虫子编号。然后将和i虫子有联系的合并为同一个集合(认为是同性的)。如果findset(u) == findset(v),出现了反常行为。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define MAXN 2020
int n,m;
int root[MAXN];
int sex[MAXN];
int findset(int x)
{
return root[x] == x ? x : root[x] = findset(root[x]);
}
void mergeset(int x,int y)
{
int fx = findset(x);
int fy = findset(y);
if(fx != fy)
{
if(fx > fy)
root[fy] = fx;
else
root[fx] = fy;
}
}
void init()
{
for(int i=0; i<=n; i++)
{
root[i] = i;
sex[i] = 0;
}
}
int main()
{
//freopen("input.txt","r",stdin);
int t;
int kase = 0;
int flag;
int u,v;
cin>>t;
while(t--)
{
flag = 1;
scanf("%d%d",&n,&m);
init();
for(int i=0; i<m; i++)
{
scanf("%d%d",&u,&v);
if(findset(u) == findset(v))
flag = 0;
else
{
if(sex[u] == 0)
sex[u] = v;
else
mergeset(sex[u],v);//合并有行为关系的虫子
if(sex[v] == 0)
sex[v] = u;
else
mergeset(sex[v],u);
}
}
printf("Scenario #%d:\n",++kase);
if(flag)
printf("No suspicious bugs found!\n\n");
else
printf("Suspicious bugs found!\n\n");
}
return 0;
}
---------------------------------------------------------------------------
战斗,毫不退缩;奋斗,永不停歇~~~~~~~~~~~~~~~~