zoukankan      html  css  js  c++  java
  • POJ 2492 A Bug's Life

    题意:给出n个虫子的m种关系,这种关系表示两个虫子性别不同,问有没有产生歧义。

    解法:并查集+向量偏移。跟POJ1173几乎一样。

    代码:

    #include<stdio.h>
    #include<iostream>
    #include<algorithm>
    #include<string>
    #include<string.h>
    #include<math.h>
    #include<limits.h>
    #include<time.h>
    #include<stdlib.h>
    #include<map>
    #include<queue>
    #include<set>
    #include<stack>
    #include<vector>
    #define LL long long
    
    using namespace std;
    
    int father[100005], delta[100005];
    void init()
    {
        memset(delta, 0, sizeof delta);
        for(int i = 0; i < 100005; i++)
            father[i] = i;
    }
    int Find(int a)
    {
        if(father[a] != a)
        {
            int tmp = Find(father[a]);
            delta[a] = (delta[a] + delta[father[a]]) % 2;
            father[a] = tmp;
        }
        return father[a];
    }
    int main()
    {
        int T;
        scanf("%d", &T);
        int cse = 1;
        while(T--)
        {
            init();
            int n, m;
            scanf("%d%d", &n, &m);
            bool ans = 0;
            while(m--)
            {
                int x, y;
                scanf("%d%d", &x, &y);
                int c = Find(x), d = Find(y);
                if(c != d)
                {
                    father[c] = d;
                    delta[c] = (delta[y] - delta[x] + 1) % 2;
                    continue;
                }
                if(delta[x] == delta[y]) ans = 1;
            }
            printf("Scenario #%d:
    ", cse++);
            if(ans) puts("Suspicious bugs found!
    ");
            else puts("No suspicious bugs found!
    ");
        }
        return 0;
    }
    

      

  • 相关阅读:
    20210420
    20210419
    2021041601
    20210416
    20210415
    20210414
    20210413
    20210412
    20210409
    20210405
  • 原文地址:https://www.cnblogs.com/Apro/p/4808346.html
Copyright © 2011-2022 走看看