【POJ 1733】 Parity Game

【题目链接】

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

【算法】

            并查集

【代码】

             

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cerrno>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <limits>
#include <list>
#include <map>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <utility>
#include <vector>
#include <cwchar>
#include <cwctype>
#include <stack>
#include <limits.h>
using namespace std;

struct Query
{    
        int l,r,op; 
} q[100010];

int k,n,i,x,y,len;
int l[100010],r[100010],fa[100010],d[100010],a[100010];
char s[100010][10];

inline int get_root(int x)
{
        if (fa[x] == x) return x;
        int f = get_root(fa[x]);
        d[x] ^= d[fa[x]];
        return fa[x] = f;        
}

int main() 
{
        
        scanf("%d%d",&k,&n);
        for (i = 1; i <= n; i++)
        {
                scanf("%d%d%s",&l[i],&r[i],&s[i]);
                a[++len] = l[i] - 1;
                a[++len] = r[i];        
        }
        sort(a+1,a+len+1);
        len = unique(a+1,a+len+1) - a - 1;
        for (i = 1; i <= n; i++)
        {
                q[i].l = lower_bound(a+1,a+len+1,l[i]-1) - a;
                q[i].r = lower_bound(a+1,a+len+1,r[i]) - a;
                q[i].op = strcmp(s[i],"odd") == 0 ? 1 : 0;    
        }
        for (i = 1; i <= 2 * n; i++) fa[i] = i;
        for (i = 1; i <= n; i++)
        {
                x = get_root(q[i].l);
                y = get_root(q[i].r);
                if (x == y)
                {
                        if ((d[q[i].l] ^ d[q[i].r]) != q[i].op)
                        {
                                printf("%d
",i-1);
                                exit(0);
                        }
                  }    else
                 {
                         fa[x] = y;
                         d[x] = d[q[i].l] ^ d[q[i].r] ^ q[i].op;
                 }    
        }
        printf("%d
",n);
        
        return 0;
    
}