POJ 2492 A Bug's Life

本博客探讨了如何使用并查集算法解决科学家Hopper关于虫子两性行为一致性的验证问题,通过分析虫子间的交互数据,判断是否存在违反两性行为规则的情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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--)
            {
           

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值