POJ2492 A Bug's Life (带权并查集判断种类)

题目链接

http://poj.org/problem?id=2492

题目

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!

题意

给定n个昆虫和m对关系,要求m对关系都是不同性别的。问是否存在逻辑矛盾问题。
注意每个测试数据之间有空行输出。

分析

并查集的本质是森林,每个集合是一棵树,集合的代表元是树根。
给树的边加一个权值,sum[u]表示结点u与u的父结点的距离,这个值模2余1表示u与u的父结点不同性别,模2余0表示同性别。
这样,判断x y是否矛盾时:
如果x和y不在一棵树上,那么可默认其合法并将x所在的树和y所在树合并成一棵树。
如果x和y在一棵树上,那么可以根据x与根的距离、y与根的距离去进行判断。由于并查集路径压缩特性,进行一次find()操作后,x的父结点就变成了根,此时sum[x]就是x与根的距离。即判断sum[x]+sum[y]==1与否。

AC代码

//766ms 0.2MB
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn=2e3+100;
int par[maxn];
int sum[maxn];//带权并查集的核心,
//sum[i]表示结点i到其父结点的距离,模2为1表示和父节点相异,模2为0表示与父结点相同
int n,m;
void init()
{
    for(int i=0;i<=n;i++) par[i]=i;
    memset(sum,0,sizeof(sum));//初始化每个结点与自己有相同性别
}
int find(int x)
{
    if(par[x]!=x)
    {
        //路径压缩并更新sum值
        int tmp=par[x];
        par[x]=find(par[x]);
        sum[x]+=sum[tmp];
        sum[x]%=2;
    }
    return par[x];//路径压缩后,par[x]表示x的根结点,sum[x]表示x与根结点的性别差异
}
void unit(int x,int y)
{
    int fx=find(x),fy=find(y);
    par[fx]=fy;
    sum[fx]=(sum[y]-sum[x]+1)%2;
}
bool same(int x,int y)
{
    return find(x)==find(y);
}
int main()
{
    int T,kase=0;
    scanf("%d",&T);
    while(T--)
    {
        if(kase) putchar(10);
        scanf("%d%d",&n,&m);
        init();
        bool flag=true;
        while(m--)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            if(!same(x,y)) unit(x,y);
            else
            {
                if(sum[x]+sum[y]!=1) flag=false;
            }
        }
        printf("Scenario #%d:\n",++kase);
        printf("%s\n",!flag?"Suspicious bugs found!":"No suspicious bugs found!");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值