Files
python/TangDou/AcWing_TiGao/T4/239_ExtendDomain_STL.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

54 lines
1.3 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
const int N = 40010, B = N / 2;
// 简化版本的食物链
int n, m;
// 无序离散化
unordered_map<int, int> S;
int get(int x) {
if (S[x] == 0) S[x] = ++n;
return S[x];
}
// 并查集
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;
int res = m;
for (int i = 1; i <= m; i++) {
int a, b;
scanf("%d %d", &a, &b);
char t[100];
scanf("%s", t);
a = get(a - 1), b = get(b); // 计算出新的在并查集中的号
if (t[0] == 'e') { // 偶数个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;
}