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.
题意:简言之,多组数据,每组先输入一个n和m,分别代表有n个人和m种配对,没配对过的两个人假设都是异性,则
出现过的人可以根据已出现过的人判别性别,如出现同性配对则输出“Suspicious bugs found!”,若未出现同性配
对则输出“No suspicious bugs found!”。
思路:正解是带权的并查集,当然用搜索也能做出来,暂时弱渣只会用带权的并查集(2333。具体思路还请看以下详
细代码。
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.
题意:简言之,多组数据,每组先输入一个n和m,分别代表有n个人和m种配对,没配对过的两个人假设都是异性,则
出现过的人可以根据已出现过的人判别性别,如出现同性配对则输出“Suspicious bugs found!”,若未出现同性配
对则输出“No suspicious bugs found!”。
思路:正解是带权的并查集,当然用搜索也能做出来,暂时弱渣只会用带权的并查集(2333。具体思路还请看以下详
细代码。
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!
<span style="font-size:18px;">#include <cstring>
using namespace std;
int op[40000],f[40000]; //op[]数组用来存储当前人的配对对象,f[]数组用来建立并查集
int dis(int x) //搜寻并查集
{
return f[x]==x?x:f[x]=dis(f[x]);
}
void Merge(int x,int y) //建立并查集
{
int xx=dis(x);
int yy=dis(y);
if(xx!=yy)
f[xx]=yy;
}
int main()
{
int t,n,m,x,y;
scanf("%d",&t);
for(int k=1; k<=t; k++)
{
scanf("%d %d",&n,&m);
memset(op,0,sizeof(op));
for(int i=1; i<=n; i++)
f[i]=i;
bool flag=false;
while(m--)
{
scanf("%d %d",&x,&y);
if(flag)
continue;
if(!op[x]&&!op[y]) //如果都没配对过
{
op[x]=y; //则x的对象是y
op[y]=x; //y的对象是x
}
else if(op[x]&&!op[y]) //如果x已有了对象而y还没有,则说明y与x的对象是同性,进而将x的对象与y建立在一棵树上
{
op[y]=x;
Merge(op[x],y);
}
else if(!op[x]&&op[y]) //道理同上
{
op[x]=y;
Merge(x,op[y]);
}
else if(op[x]&&op[y]) //道理同上
{
Merge(op[x],y);
Merge(x,op[y]);
}
if(dis(x)==dis(y)) //假如两个人在同一棵“树上”,则说明该两个人是同性
flag=true;
}
if(k>1)
printf("\n");
printf("Scenario #%d:\n",k);
if(flag)
printf("Suspicious bugs found!\n");
else
printf("No suspicious bugs found!\n");
}
return 0;
}</span>