POJ - 2492 -A Bug's Life 【带权并查集讲解】

POJ - 2492 -A Bug’s Life

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!

#include<iostream>
#include<stdio.h>
#include<math.h>
#include<algorithm>

using namespace std;
int f[11111],rank[11111];

int found(int x)
{
    int t;
    if(f[x]!=x)
    {
        t=found(f[x]);
        rank[x]=(rank[x]+rank[f[x]])%2;
        f[x]=t;
    }
    return f[x];
}
int main()
{
    int T;
    int flag,t1,t2;
    int n,m,a,b;
    scanf("%d",&T);
    for(int k=1; k<=T; k++)
    {
        flag=1;
        scanf("%d%d",&n,&m);
        for(int i=1; i<=n; i++)
        {
            f[i]=i;
            rank[i]=0;
        }
        for(int i=1; i<=m; i++)
        {
            scanf("%d%d",&a,&b);
            t1=found(a);
            t2=found(b);
            if(!flag) continue;
            if(t1==t2)
            {
                if(rank[a]==rank[b])
                    flag=0;
            }
            else
            {
                f[t2]=t1;
                rank[t2]=(rank[a]+1-rank[b])%2;
            }
        }
        if(!flag)
        {
            printf("Scenario #%d:\n",k);
            printf("Suspicious bugs found!\n\n");
        }
        else
        {
            printf("Scenario #%d:\n",k);
            printf("No suspicious bugs found!\n\n");
        }
    }
    return 0;
}

讲解思路:
1、

int found(int x)
{
    int t;
    if(f[x]!=x)
    {
        t=found(f[x]);
        rank[x]=(rank[x]+rank[f[x]])%2;//该点到父节点距离+父节点到根节点距离
                  //根据儿子与父亲的关系和父亲与爷爷的关系推出儿子与爷爷的关系 
        f[x]=t;
    }
    return f[x];
}

2、

 rank[t2]=(rank[a]+1-rank[b])%2; 
  //rank[b]代表的是b与根节点的关系,这是因为我们开头的每一次都找了a,b的根节点,在found函数里
  //类似向量的求法  

3、
判断两点是否是同一个根节点
如果是,则判断两点与根结点的关系是否一样,一样则为同性恋,否则不是。
如果不是同一个根结点,合并根结点后,更新b的根结点与a的根结点的关系。

还是看看大神的:
http://blog.csdn.net/stay_accept/article/details/47395935

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值