poj 1733 Parity game(离散化+并查集)

题解思路:

与poj2492 - A Bug's Life相同;

两类,一个odd 一个even, 要开一个区间a-- or b++

比2492相比多了一个离散化;

没有相矛盾的情况 输出m

odd even赋值反了 wa了半天..

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<set>
#include<map>
#include<stack>

#define mem(a,b) memset(a,b,sizeof(a))
#define ll long long

const int maxn=1e6+10;

using namespace std;

struct node{
    int u,v;
    char s[5];
}e[maxn];

int arr[maxn],f[maxn],sum[maxn],tot=0;

int root(int x)
{
    if(f[x]==x) return x;
    int rt=root(f[x]);
    sum[x]=(sum[x]+sum[f[x]])%2;
    return f[x]=rt;
}

void init()
{
    mem(e,0);
    mem(arr,0);
    mem(sum,0);
    tot=0;
}

int main(){
    int n,m,ans;
    while(~scanf("%d%d",&n,&m))
    {
        ans=m;
        init();
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d%s",&e[i].u,&e[i].v,e[i].s);
            arr[++tot]=e[i].u;
            arr[++tot]=++e[i].v;
        }
        sort(arr+1,arr+1+tot);
        tot=unique(arr+1,arr+tot+1)-arr-1;
       // cout<<tot<<endl;
        for(int i=0;i<=tot;i++) f[i]=i;
        for(int i=1;i<=m;i++)
        {
            int a,b,x,y,v;
            a=lower_bound(arr+1,arr+tot+1,e[i].u)-arr;
            b=lower_bound(arr+1,arr+tot+1,e[i].v)-arr;
            //cout<<a<<" "<<b<<endl;
            e[i].s[0]=='o'?v=1:v=0;
            x=root(a);
            y=root(b);
            if(x!=y)
            {
                f[x]=y;
                sum[x]=(v+sum[b]-sum[a]+2)%2;
            }
            else
            {
                if((sum[a]-sum[b]+2)%2!=v)
                {
                    ans=i-1;break;
                }
            }
        }
        printf("%d
",ans);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/minun/p/10473767.html