# POJ1733 Parity game

## 题目链接

POJ1733 Parity game

## 题目概述

1 ≤ N ≤ 1 0 9 , 1 ≤ M ≤ 5 ⋅ 1 0 5 1\leq N \leq 10^9, 1\leq M \leq 5\cdot10^5

## 代码

/*
* @Author: Shuo Yang
* @Date: 2020-08-02 15:22:26
* @LastEditors: Shuo Yang
* @LastEditTime: 2020-08-03 15:59:23
* @FilePath: /Code/POJ/1733.cpp
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N = 1e6+5;
int buf[N];
int s[N];
int d[N];
// int cnt[N];
bool flag = true;
int ans = 0;
int cnt = 0;

int find_set(int x){
if(s[x] == x)
return x;
int root = find_set(s[x]);
d[x] ^= d[s[x]];
s[x] = root;
return s[x];
}

void union_set(int x, int y, int value){
int a = find_set(x);
int b = find_set(y);
if(a == b){
if((d[x] ^ d[y]) != value){
flag = false;
}
return;
}
s[a] = b;
d[a] = d[x]^d[y]^value;
}

struct node{
int left, right,nums;
};

node nodes[N];

int main(int argc, char** args){
for(int i = 0; i < N; ++i)
s[i] = i;
ios::sync_with_stdio(false);
cout.tie(0);
cin.tie(0);
int n,m;
cin>>n>>m;
for(int i = 0; i < m; ++i){
int a,b,temp;
string str;
cin>>a>>b>>str;
if( str == "even" ){
temp = 0;
}else{
temp = 1;
}
nodes[i]={a-1,b,temp};
buf[cnt++] = a-1;
buf[cnt++] = b;
}
sort(buf, buf+cnt);
cnt = unique(buf, buf+cnt)-buf;
for(int i = 0; i < m; ++i){
int x =  nodes[i].left;
int y = nodes[i].right;
int value = nodes[i].nums;
x = lower_bound(buf, buf+cnt, x)-buf;
y = lower_bound(buf, buf+cnt, y)-buf;
union_set(x, y, value);
if( !flag ){
cout<<i<<endl;
break;
}
}
if( flag )
cout<<m<<endl;
return 0;
}

04-25 127
06-04 151

01-31 2459
04-05 742
07-19 243
03-02 275