1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
|
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <cmath> #include <queue> #include <set> #include <vector> using namespace std;
const int INF = ~0u>>1; typedef pair <int,int> P; #define MID(x,y) ((x+y)>>1) #define iabs(x) ((x)>0?(x):-(x)) #define REP(i,a,b) for(int i=(a);i<(b);i++) #define FOR(i,a,b) for(int i=(a);i<=(b);i++) #define pb push_back #define mp make_pair #define print() cout<<"--------"<<endl #define maxn 22222 int pa[maxn],d[maxn]; int l[maxn],r[maxn],x[maxn],c[maxn];
int find(int x){ if (pa[x] == x) return x; int ret = find(pa[x]); d[x] ^= d[pa[x]]; pa[x] = ret; return ret; }
bool Union(int L,int R,int c){ int l = find(L); int r = find(R); if (l == r){ return ((d[L] ^ d[R]) == c); }else { if (l > r){ pa[l] = r; d[l] = d[L] ^ c ^ d[R]; }else { pa[r] = l; d[r] = d[R] ^ c ^ d[L]; } } return 1; }
int bin(int l,int r,int va){ int mid = MID(l,r); while (l <= r){ mid = MID(l,r); if (x[mid] == va) return mid; else if (x[mid] < va) l = mid + 1; else r = mid - 1; } return -1; }
int main(){ char s[15];int n,m; scanf("%d%d", &n, &m); int cnt = 0; for (int i = 0; i < m; i ++){ scanf("%d%d%s", &l[i], &r[i], s); x[cnt ++] = l[i] - 1; x[cnt ++] = r[i]; if (s[0] == 'o') c[i] = 1; else c[i] = 0; } sort(x, x + cnt); cnt = unique(x, x + cnt) - x; for (int i = 0; i <= maxn; i ++){ pa[i] = i; d[i] = 0; } for (int i = 0; i < m; i ++){ int L = bin(0, cnt - 1, l[i] - 1) + 1; int R = bin(0, cnt - 1, r[i]) + 1;
if (Union(L,R,c[i]) == 0){ printf("%dn", i); return 0; } } printf("%dn", m); return 0; }
|