A Bug's Life
Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 15009 Accepted Submission(s): 4880
Problem 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.
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!HintHuge input,scanf is recommended.
Source
题意 :给出n个点m条边,每条边有两个点表示它们两个发生了性行为,试问这些点之间是否有同性性行为的;
思路:
poj1182 食物链;
代码:
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int N=2005;
int pre[N*2],Rank[N*2];
void init()
{
for(int i=0;i<N*2;i++)
pre[i]=i,Rank[i]=0;
}
int Find(int x)
{
if(pre[x]==x)
return x;
else
return pre[x]=Find(pre[x]);
}
void unite(int x,int y)
{
x=Find(x);
y=Find(y);
if(x==y) return ;
if(Rank[x]<Rank[y])
pre[x]=y;
else
{
pre[y]=x;
if(Rank[x]==Rank[y]) Rank[x]++;
}
}
bool same(int x,int y)
{
return Find(x)==Find(y);
}
int main()
{
int T,n,m;
scanf("%d",&T);
int cas=0,flag=0;
int k=T;
while(T--)
{
init();
cas++;
flag=0;
int x,y;
scanf("%d%d",&n,&m);
for(int i=0;i<m;i++)
{
scanf("%d%d",&x,&y);
if(same(x,y)||same(x+N,y+N))
flag=1;
else
{
unite(x,y+N);
unite(x+N,y);
}
}
// if(T<k-1)
// printf("\n");
printf("Scenario #%d:\n",cas);
if(flag)
printf("Suspicious bugs found!\n");
else
printf("No suspicious bugs found!\n");
// if(T!=0)
printf("\n");
}
// printf("haha");
return 0;
}
格式是每个输出后都有一个空行;