zoukankan      html  css  js  c++  java
  • kuangbin_UnionFind J (POJ 2492)

    加权并查集mod2模板 本体的难点是bug的释义(笑)

    #include <iostream>
    #include <string>
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <queue>
    #include <map>
    #include <set>
    #include <algorithm>
    using namespace std;
    
    int father[5005],sum[5005];
    int find(int x)
    {
        if(father[x] != x) {
            int tmp = father[x];
            father[x] = find(tmp);
            sum[x] = (sum[x] + sum[tmp]) % 2;
        }
        return father[x];
    }
    void merge(int x,int y)
    {
        int tx = find(x);
        int ty = find(y);
        //printf("x = %d y = %d
    tx = %d ty = %d
    ", x, y, tx, ty);
        if(tx == ty) return;
        father[tx] = ty;
        //printf("father(1) = %d
    ",father[1]);
        sum[tx] = (sum[x] - sum[y] + 1) % 2;
        return;
    }
    int main()
    {
        int t;
        scanf("%d",&t);
        for(int kase = 1; kase <= t; kase++)
        {
            int n, m;
            bool flag = true;
            scanf("%d%d", &n, &m);
            //Initiate
            for(int i = 0; i <= n; i++)
            {
                father[i] = i;
                sum[i] = 0;
            }
            while(m--)
            {
                int a, b;
                scanf("%d%d", &a, &b);
                if(find(a) == find(b))  
                {
                    if(sum[a] != (sum[b] + 1) % 2){
                        //printf("a = %d b = %d
    ", a, b);
                        flag = false;  
                    }
                }  
                else  
                    merge(a, b);  
            }
            printf("Scenario #%d:
    ", kase);
            if(flag) printf("No suspicious bugs found!
    
    ");
            else printf("Suspicious bugs found!
    
    ");
        }
        return 0;
    }
  • 相关阅读:
    JDK、J2EE、J2SE、J2ME的区别
    消息队列
    Unity3D 导入aar注意事项
    汇编小结
    构造函数语意学--笔记
    androidStudio 改包名
    新手用车
    北京临牌办理与续办
    h5+
    apache.http.MalformedChunkCodingException: Chunked stream ended unexpectedly
  • 原文地址:https://www.cnblogs.com/quasar/p/5060162.html
Copyright © 2011-2022 走看看