POJ 2492/hdu 1829 A Bug's Life【带权并查集】

A Bug's Life
Time Limit: 10000MS Memory Limit: 65536K
Total Submissions: 33135 Accepted: 10859

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.


以前做这个题的时候用的是二分图染色法,今天试一试用并查集做。



题目大意:

有n只虫子,m个关系,关系表示x,y是异性恋爱关系,如果有同性恋,输出 found,否则输出no found、



思路:尽管给出的m个关系是指代两个虫子不属于一个性别,但是我们依旧能用一个并查集来做这个问题,将有关系的两个虫子放在一个并查集中,并且对其设置边权,当

a-b之间有直接关系表示这两个虫子不属于一个性别,那么对其设置边权为1.辣么我们有这样的结论:如果a到b的边权值为偶数的时候表示两者同性别,如果a到b的边权值为奇数的时候表示不同性别。


在连接的时候有这样的关系:



1+sumb==suma+x

x=1+sumb-suma。

这个时候我们就能够通过已知权值求未知权值了,思路已经很清晰,最后上AC代码:

#include<stdio.h>
#include<string.h>
using namespace std;
int f[2500];
int sum[2500];
int find(int x)
{
    if(x!=f[x])
    {
        int pre=f[x];//pre是x的一个父节点。
        f[x]=find(f[x]);//递归找祖先。
        sum[x]+=sum[pre];//路径压缩部分。
    }
    return f[x];
}
int main()
{
    int t;
    int kase=0;
    scanf("%d",&t);
    while(t--)
    {
        int n,m;
        int flag=0;
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
        {
            f[i]=i;
            sum[i]=0;
        }
        while(m--)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            int xx=find(x);
            int yy=find(y);
            if(xx!=yy)
            {
                sum[yy]=1-sum[y]+sum[x];
                f[yy]=xx;
            }
            else
            {
                if((sum[y]-sum[x])%2==0)//如果距离为偶数
                {
                    flag=1;//标记上有同性恋
                }
            }
        }
        printf("Scenario #%d:\n",++kase);
        if(flag==1)
        {
            printf("Suspicious bugs found!\n");
        }
        else
        {
            printf("No suspicious bugs found!\n");
        }
        printf("\n");
    }
}







  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值