zoukankan      html  css  js  c++  java
  • (并查集 建立关系)Parity game -- POJ -1733

    链接:

    http://poj.org/problem?id=1733

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82830#problem/H

    代码:

    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <cstdlib>
    #include <algorithm>
    #include <vector>
    #include <queue>
    #include <map>
    using namespace std;
    
    #define N 110
    #define oo 0xfffffff
    
    map<int, int>f; ///f[i]=x, i是int类型的,x也是int类型的
    map<int, int>r;
    int n, m;
    
    int Find(int x)
    {
        if(!f[x])
            return x;
        int k=f[x];
        f[x]=Find(f[x]);
        r[x]=(r[x]+r[k])%2;
    
        return f[x];
    }
    
    int main()
    {
        while(scanf("%d", &n)!=EOF)
        {
            scanf("%d", &m);
            int i, a, b, fa, fb, ans=0, flag=0;
            char s[N];
    
            for(i=1; i<=m; i++)
            {
                int num=0;
                scanf("%d%d%s", &a, &b, s);
                a--;
    
                if(strcmp(s, "odd")==0)
                    num=1;
    
                fa=Find(a), fb=Find(b);
                if((a<0 || a>=n) || (fa==fb && (r[a]+num)%2!=r[b]))
                    flag=1;
                if(flag==1)
                    continue;
                if(fa!=fb)
                {
                    f[fa]=fb;
                    r[fa]=(r[b]-(r[a]+num)%2+2)%2;
                }
                ans++;
            }
            printf("%d
    ", ans);
        }
        return 0;
    }
    勿忘初心
  • 相关阅读:
    bzoj2555-SubString
    离散对数
    xsy1436-括号游戏
    bzoj1143-祭祀
    连通分量
    bzoj3864-hdu4899-Hero meet devil
    bzoj3238-差异
    poj1065-Wooden Sticks
    多项式除法
    JavaScript 对象
  • 原文地址:https://www.cnblogs.com/YY56/p/4735734.html
Copyright © 2011-2022 走看看