hdu 1182 A Bug's Life(简单种类并查集)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1829

题意:就是给你m条关系a与b有性关系,问这些关系中是否有同性恋

这是一道简单的种类并查集,而且也比较简单只要比较给出的两个点是否有相同祖宗如果有那么他们距离祖宗结点的距离是否为偶数

如果是偶数那么他(她)们必然是同性恋。

 

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int M = 2e3 + 10;
int n , m , x , y , f[M] , root[M];
void init() {
    for(int i = 1 ; i <= n ; i++) {
        f[i] = i;
        root[i] = 0;
    }
}
int find(int x) {
    if(x == f[x])
        return x;
    int t = find(f[x]);
    root[x] = (root[f[x]] + root[x]) & 1;
    f[x] = t;
    return t;
}
bool Union(int x , int y) {
    int a = find(x) , b = find(y);
    if(a == b) {
        if(root[x] == root[y])
            return false;
    }
    else {
        f[a] = b;
        root[a] = (root[x] + root[y] + 1) & 1;
    }
    return true;
}
int main() {
    int t , ans = 0;
    scanf("%d" , &t);
    while(t--) {
        bool flag = true;
        scanf("%d%d" , &n , &m);
        init();
        for(int i = 1 ; i <= m ; i++) {
            scanf("%d%d" , &x , &y);
            if(!flag)
                continue;
            flag = Union(x , y);
        }
        printf("Scenario #%d:\n" , ++ans);
        if(!flag) {
            printf("Suspicious bugs found!\n");
        }
        else {
            printf("No suspicious bugs found!\n");
        }
        printf("\n");
    }
    return 0;
}

转载于:https://www.cnblogs.com/TnT2333333/p/6580691.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值