带权并查集+离散化
/*
s[l,r]为奇数-> s[r]-s[l-1](前缀和)为奇数->s[r]与s[l-1]不同类
若为偶数,则s[r]与s[l-1]为同一类
用带权并查集维护
*/
#include <iostream>
#include <unordered_map>
#define MAXN 20000
using namespace std;
int p[MAXN], d[MAXN];
unordered_map<int, int> mp;
int n, k;
int get(int x){ //离散化,重新编号
if(!mp.count(x)) return mp[x] = ++n;
return mp[x];
}
int find(const int &x){
if(x!=p[x]){
int t = find(p[x]);
d[x] += d[p[x]];
p[x] = t;
}
return p[x];
}
int main(){
cin>>n>>k;
n = 0;
for(int i=0; i<MAXN; i++) p[i] = i;
for(int i=1; i<=k; i++){
int x, y;
string s;
cin >> x >> y >> s;
x--;
x=get(x), y=get(y);
int px = find(x), py = find(y);
int temp = 0;
if(s == "odd") temp = 1;
if(px == py){
if( ((d[x]+d[y])%2+2)%2 !=temp){
cout<<i-1<<endl;
return 0;
}
}
else{
p[px] = py;
d[px] = d[x]+d[y]+temp;
}
}
cout<<k<<endl;
return 0;
}