A Bug's Life
Time Limit: 10000MS | Memory Limit: 65536K | |
Total Submissions: 31371 | Accepted: 10284 |
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.
题意:找基佬游戏(汗-_-b)有个hentai科学家研究虫子种群,不断地给出二元组xy,表示x和y是异性交往,但是可能会出现矛盾(找到基佬),比如1与2是异性恋,2与3是异性恋,却又告诉你1和3是异性恋。问种群中存不存在基佬败类;
思路:用带权值的并查集,权值用Rank[]数组记录,0表示两个为同类,1表示两个为异类;
代码如下:
#include< cstdio >
int sex[2005],fa[2005],flag;
int find(int x)
{
int t=x;
while(t!=fa[t])
{
sex[t]=(sex[t]+sex[fa[t]])%2;
t=fa[t];
}
fa[x]=t;
sex[x]=sex[t];
return fa[x];
}
void join(int c,int d)
{
int fc=find(c);
int fd=find(d);
if(fc==fd)//如果在同一个集合里;
{
if(sex[c]==sex[d])//如果权值相同,说明为同性恋;
{
flag=1;
return ;
}
}
fa[fc]=fd;//如果为正常恋爱则并到一个集合里去;
sex[fc]=(-sex[c]+sex[d]+1)%2;//权值相应的改变;
}
int main()
{
int N,c=0;
scanf("%d",&N);
while(N--)
{
int n,m;flag=0;
scanf("%d%d",&n,&m);
while(n--)//初始化;
{
fa[n]=n;
sex[n]=0;
}
while(m--)
{