POJ-2492-A Bug's Life [并查集]


题目传送门


题意:
每行两个数字,表示两个虫子相互喜欢,只有异性才能相互喜欢,所以求里面有没有基佬或者拉拉= =。
思路:
种类并查集,只有两种状态,父节点与自身性别相同,父节点与自身性别不同。
更新并判断。

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <queue>
using namespace std;
int M,N;
struct node{
    int fa;
    int p;
}F[3000];
void init()
{
    for (int i = 0; i <= 3000; i++)
    {
        F[i].fa = i;
        F[i].p = 0;
    }
    return ;
}
int find(int x)
{
    if (x==F[x].fa) return x;
    int t = F[x].fa;
    F[x].fa = find(F[x].fa);
    F[x].p ^= F[t].p;
    return F[x].fa;
}
int main(void)
{
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);

    int T;
    scanf("%d", &T);
    for (int t = 1; t <= T; t++)
    {
        init();
        scanf("%d %d", &N, &M);
        int flag = 0;
        while (M--)
        {
            int x, y;
            scanf("%d %d", &x, &y);
            int a = find(x);
            int b = find(y);
            if (a!=b)
            {
                F[a].fa = b;
                F[a].p = (F[x].p-F[y].p+1)%2;
            }
            else
            {
                if (F[x].p == F[y].p)
                    flag = 1;
            }
        }
        printf("Scenario #%d:\n",t);
        printf("%s\n\n", flag==1?"Suspicious bugs found!":"No suspicious bugs found!");
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值