(并查集 建立关系)Parity game -- POJ -1733

本文详细介绍了如何通过代码解决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\n", ans);
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/YY56/p/4735734.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值