'commit'
This commit is contained in:
54
TangDou/AcWing_TiGao/T4/239_ExtendDomain_STL.cpp
Normal file
54
TangDou/AcWing_TiGao/T4/239_ExtendDomain_STL.cpp
Normal file
@@ -0,0 +1,54 @@
|
||||
#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;
|
||||
}
|
||||
Reference in New Issue
Block a user