他说每个测试输出两行,其实是三行吧,否则PE.
并查集的简单应用,类似于”发现它,抓住它”.
描述
Hopper 博士正在研究一种罕见种类的虫子的性行为。他假定虫子只表现为两种性别,并且虫子只与异性进行交互。在他的实验中,不同的虫子个体和虫子的交互行为很容易区分开来,因为这些虫子的背上都被标记有一些标号。
现在给定一系列的虫子的交互,现在让你判断实验的结果是否验证了他的关于没有同性恋的虫子的假设或者是否存在一些虫子之间的交互证明假设是错的。
输入
输入的第一行包含实验的组数。每组实验数据第一行是虫子的个数(至少1个,最多2000个) 和交互的次数 (最多1000000次) ,以空格间隔. 在下面的几行中,每次交互通过给出交互的两个虫子的标号来表示,标号之间以空格间隔。已知虫子从1开始连续编号。
输出
每组测试数据的输出为2行,第一行包含 "Scenario #i:", 其中 i 是实验数据组数的标号,从1开始,第二行为 "No suspicious bugs found!" 如果实验结果和博士的假设相符,或 "Suspicious bugs found!" 如果Hopper的博士的假设是错误的
样例输入
2
3 3
1 2
2 3
1 3
4 2
1 2
3 4
样例输出
Scenario #1:
Suspicious bugs found!
Scenario #2:
No suspicious bugs found!
#include <iostream>
#include <stdio.h>
using namespace std;
int _case, bug, flag, action, a, b;
int father[2100], r[2100];
//r表示和父节点的关系,0表示同性
int root(int x)
{
int temp;
if(x == father[x])
return father[x];
//路径压缩
temp = root(father[x]);
r[x] = (r[x]+r[father[x]]) % 2;//路径压缩时,修改节点和祖先的关系
father[x] = temp;
return father[x];
}
void Union(int x,int y)
{
int fx = root(x);
int fy = root(y);
if(fx != fy)
{
/*if(father[x] == fx )
cout<<"hhhhhhhh"<<endl;
else
cout<<"aaaaaaaaa"<<endl;*/
father[fx] = fy;
//归并后修改关系
if(r[y] == 0)
r[fx] = 1 - r[x];
else
r[fx] = r[x];
}
else //已经有相同祖先
{
if(r[x] == r[y])
flag = 1;//他们是同性
}
}
int main()
{
scanf("%d",&_case);
for(int t=1;t<=_case;t++)
{
flag = 0;
printf("Scenario #%d:\n",t);
scanf("%d%d",&bug,&action);
for(int i=1;i<=bug;i++)
{
father[i] = i;
r[i] = 0;
}
while(action--)
{
scanf("%d%d",&a,&b);//虫子编号
if(flag == 0)
Union(a,b);
}
if(flag == 1)
printf("Suspicious bugs found!\n");
else
printf("No suspicious bugs found!\n");
printf("\n");
}
return 0;
}