链接:

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;
}

 

posted on 2015-08-17 09:49  栀蓝  阅读(138)  评论(0编辑  收藏  举报

levels of contents