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.

74 lines
1.7 KiB

2 years ago
#include <bits/stdc++.h>
using namespace std;
const int N = 40010, B = N / 2;
// 结构体记录原始输入
struct Node {
int x, y, e;
} g[N];
int n, m;
// 离散化静态数组+二分查找新位置
int b[N], bl;
int get(int x) {
return lower_bound(b, b + bl, x) - b;
}
// 并查集
int p[N];
int find(int x) {
if (p[x] != x) p[x] = find(p[x]);
return p[x];
}
int main() {
scanf("%d %d", &n, &m);
n = 0;
for (int i = 1; i < N; i++) p[i] = i;
for (int i = 1; i <= m; i++) {
int x, y;
char t[100];
scanf("%d %d %s", &x, &y, t);
g[i].x = x, g[i].y = y;
if (t[0] == 'e')
g[i].e = 0;
else
g[i].e = 1;
// 记录下来
b[bl++] = x, b[bl++] = y;
}
// 离散化去重
sort(b, b + 2 * m);
bl = unique(b, b + 2 * m) - b;
int res = m;
for (int i = 1; i <= m; i++) {
int a = g[i].x, b = g[i].y, e = g[i].e;
a = get(a - 1), b = get(b); // 计算出新的在并查集中的号
if (e == 0) { // 偶数个1
if (find(a + B) == find(b)) { // 如果奇偶性不同因为b与a+B相同
res = i - 1;
break;
}
// join两个奇偶相同的集合
p[find(a)] = find(b);
p[find(a + B)] = find(b + B);
} else { // 奇数个1
if (find(a) == find(b)) {
res = i - 1;
break;
}
// join两个奇偶不相同的集合
p[find(a + B)] = find(b);
p[find(a)] = find(b + B);
}
}
printf("%d\n", res);
return 0;
}