You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
1.4 KiB

2 years ago
#include <bits/stdc++.h>
using namespace std;
const int N = 20010;
int n, m;
int p[N], d[N];
// 无序离散化
unordered_map<int, int> S;
int get(int x) {
if (S[x] == 0) S[x] = ++n; // x映射为第n个数字
return S[x];
}
// 带边权更新并查集模板
int find(int x) {
if (x == p[x]) return x;
int root = find(p[x]);
d[x] += d[p[x]];
return p[x] = root;
}
int main() {
scanf("%d %d", &n, &m);
n = 0; // 序列的长度没有用处我们只关心每个a,b范围内的数字1的个数
for (int i = 1; i < N; i++) p[i] = i; // 初始化并查集
int res = m;
for (int i = 1; i <= m; i++) {
int a, b;
// a~b之间1是奇数个还是偶数个
scanf("%d %d", &a, &b);
char type[100]; // 字符数组
scanf("%s", type);
// 类前缀和
a = get(a - 1), b = get(b);
int t = 0; // 偶数个1
if (type[0] == 'o') t = 1; // 奇数个1
// 并查集
int pa = find(a), pb = find(b);
if (pa == pb) {
if (abs(d[a] - d[b]) % 2 != t) {
res = i - 1; // 最后一条正确的序号
break;
}
} else {
p[pa] = pb;
d[pa] = abs(d[a] - d[b] - t) % 2;
}
}
printf("%d\n", res);
return 0;
}