/* Project: Ural Author: Ai.Freedom Title: Parity Date: 20020214 Time: 1809 Tested Problem: Ural 1003 Status: 1/2 Remark: 终于在情人节AC.. 并查集, 这里用到的方法很巧妙. */ #include #include using namespace std; const int maxn = 65521+1000, bigprime = 65521; int data[maxn], f[maxn], n, m; bool r[maxn]; unsigned int hash(unsigned int num) { unsigned int d; d = num % bigprime; while (data[d] != -1 && data[d] != num) d = (d + 1) % bigprime; data[d] = num; return d; } void pathcomp(unsigned int x) { unsigned int s, t; bool a, b; a = false; s = x; while (f[s] != s) {a ^= r[s], s = f[s];} while (x != s) { b = a ^ r[x], r[x] = a, a = b; t = x, x = f[x], f[t] = s; } } void init() { memset(data, -1, sizeof(data)), memset(r, false, sizeof(r)); for (int i=0; i> n; } void calc() { int i; unsigned int a, b, x, y; string str; bool t; for (i=0; i> a >> b >> str; a--; x = hash(a), y = hash(b); t = (str == "odd"); pathcomp(x), pathcomp(y); if (f[x] != f[y]) { r[f[x]] = (t ^ r[x] ^ r[y]); f[f[x]] = f[y]; } else { if ((r[x] ^ r[y]) != t) { cout << i << endl; for (int j=i+1; j> a >> b >> str; return; } } } cout << n << endl; } int main() { #ifndef ONLINE_JUDGE freopen("1003.in", "rt", stdin); freopen("1003.out", "wt", stdout); #endif cin >> n; while (n != -1) { init(); calc(); cin >> n; } return 0; }