hdu 1829 并查集*4

810639asia2562219AAccepted232 KB343 msC++1243 B2012-11-11 20:36:43

这里两个点事分属两个集合,sex[i]保存与i性别不同的节点。

View Code
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <iostream>
using namespace std;
#define MAXN 1111111
int father[MAXN],n,m,sex[MAXN];
bool flag=true;
int find(int x)
{
    int i,j,r=x;
    while(father[r]!=r)
     r=father[r];
     i=x;
     while(i!=r)
     {
         j=father[i];
         father[i]=r;
         i=j;
     }
     return r;
}

void Union(int x,int y)
{
    x=find(x);
    y=find(y);
    if(x!=y)
    father[x]=y;
}
int main()
{
    int t,cases=1,i,j,x,y;
    scanf("%d",&t);
    while(t--)
    {
    scanf("%d%d",&n,&m);
    flag=true;
    for(i=0;i<=n;i++)
    {
        father[i]=i;
        sex[i]=0;
    }
        for(i=1;i<=m;i++)
        {
            scanf("%d%d",&x,&y);
            if(find(x)==find(y))
            flag=false;
            else
            {
                if(sex[x]==0)
                sex[x]=y;
                else Union(sex[x],y);

                if(sex[y]==0)
                sex[y]=x;
                else Union(sex[y],x);
            }
        }
        printf("Scenario #%d:\n",cases++);
        if(!flag)
        printf("Suspicious bugs found!\n");
        else
        printf("No suspicious bugs found!\n");
        printf("\n");

    }
    return 0;
}

11.11 依旧光棍……

转载于:https://www.cnblogs.com/TO-Asia/archive/2012/11/11/2765411.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值