杭电1829 A Bug's Life

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#define MAX 100002

using namespace std;

int father[MAX],rank[MAX];

void init(int n)
{
    for(int i = 1; i <=n; i ++)
        father[i] = i;
    memset(rank,0,sizeof(rank));
}

int find(int x)
{
    if(x!=father[x])
    {
        int temp = father[x];
        father[x] = find(father[x]);
        rank[x] = (rank[x] + rank[temp])%2;
        //此式只的向量指向是  根 -> temp == rank[temp]
        //temp - > x == rank[x]
        //根 --> x   == 根 --> temp --> x;

    }
    return father[x];
}

int Union(int x,int y)
{
    int a = find(x);
    int b = find(y);
    if(a == b) return 1;
    father[a] = b;
    rank[a] = (rank[y]+1-rank[x])%2;   //必须知道向量关系是根指向叶子节点。
    // b -- > y == rank[y], y -->x  == d x --> a == -rank[x]
    //b - >y ->x -> b;

    return 0;
}

int main()
{
    int a,b,m,n,stu,ans;
    scanf("%d",&n);

        for(int i = 1; i <=n; i ++)
        {
            cin>>stu>>m;
            init(stu);
            ans = 0;
            for(int j = 1; j <=m; j++)
            {
                scanf("%d %d",&a,&b);
                if(find(a)==find(b))
                {
                    if(rank[a]==rank[b])
                        ans = 1;
                }
                else
                    Union(a,b);
            }
            if(ans == 1)
                printf("Scenario #%d:\nSuspicious bugs found!\n\n",i);
            else
                printf("Scenario #%d:\nNo suspicious bugs found!\n\n",i);
        }

    return 0;
}
#include <iostream>
#include <cstring>
#include <cstdio>

using namespace std;

const int M = 4005;
int Set[M];
int T, n,m;

void init() {

    for(int i = 1; i <= n*2; i++) {

        Set[i] = i;
    }
}

int Find(int x) {

    return x == Set[x] ? x : Set[x] = Find(Set[x]);
}
void Union(int a, int b) {

    a = Find(a);
    b = Find(b);
    if(a != b) {

        if(a > b)
            Set[a] = b;
        else
            Set[b] = a;
    }
}

int main()
{
    int a, b;
    int flag;
    int q = 1;
    scanf("%d", &T);
    while(T--) {

        scanf("%d%d", &n, &m);
        init();
        flag = 1;
        for(int i = 0; i < m; i++) {

            scanf("%d%d", &a, &b);

                if(Find(a) == Find(b)) {
                    flag = 0;

                } else {

                    Union(a, b + n);
                    Union(a+n, b);
                }

            }

        printf("Scenario #%d:\n", q++);
        if(flag)
            printf("No suspicious bugs found!\n");
        else
            printf("Suspicious bugs found!\n");
            printf("\n");
    }
    return 0;
}






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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值