POJ 2492 A Bug's Life

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.


【题目分析】
带偏移量的并查集。维护一个数组d来表示和跟的关系(能否进行基因交流)。然后就是类似于并查集的合并与查询。


【代码】

#include <cstdio>
#include <cstring>
int f[2001],d[2001];
int gf(int k)
{
    int t=f[k];
    if (t==k) return k;
    f[k]=gf(t);
    d[k]=(d[k]+d[t])%2;
    return f[k];
}
void un(int a,int b)
{
    int x=gf(a),y=gf(b);
    if (x==y) return;
    f[x]=y;
    d[x]=((d[a]-d[b])^1);
}
int main()
{
    int T,flag=1,kase=0;
    scanf("%d",&T);
    while (T--)
    {
        flag=1;
        int n,m;
        scanf("%d%d",&n,&m);
        for (int i=1;i<=n;++i) f[i]=i,d[i]=0;
        for (int i=1;i<=m;++i)
        {
            int a,b;
            scanf("%d%d",&a,&b);
            if (gf(a)==gf(b))
              {if (d[a]!=(d[b]^1)) flag=0;}
            else un(a,b);
        }
        if(flag)printf("Scenario #%d:\nNo suspicious bugs found!\n\n",++kase);
        else printf("Scenario #%d:\nSuspicious bugs found!\n\n",++kase);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值