- [NOI2001] 食物链
1
- @ 2026-7-20 11:50:54
#include<bits/stdc++.h>
using namespace std;
#define ll long long
struct DSU {
vector<int> f;
DSU(int n) {
f.assign(n + 1, 0);
for (int i = 1; i <= n; i++) f[i] = i;
}
int rt(int u) {
if (u == f[u]) return u;
return f[u] = rt(f[u]);
}
int same(int u, int v) {
return rt(u) == rt(v);
}
void merge(int u, int v) {
f[rt(u)] = rt(v);
}
};
void solve() {
int n, ans = 0, q;
cin >> n >> q;
DSU dsu(3 * n);
while (q--) {
int o, x, y;
cin >> o >> x >> y;
if (max(x, y) > n) {
ans++; continue;
}
if (o == 1) {
// same
if (dsu.same(x, y + n) || dsu.same(x + n, y)) {
ans++; continue;
}
dsu.merge(x, y);
dsu.merge(x + n, y + n);
dsu.merge(x + 2 * n, y + 2 * n);
} else {
// eat
if (x == y) {
ans++; continue;
}
if (dsu.same(x, y) || dsu.same(x + n, y)) {
ans++; continue;
}
dsu.merge(x, y + n);
dsu.merge(x + n, y + n * 2);
dsu.merge(x + 2 * n, y);
}
}
cout << ans << '\n';
}
signed main() {
int t = 1;
// cin >> t;
while (t--) solve();
}
/*
g++ -std=c++20 1.cpp -o 1 && 1 < in.txt > out.txt
*/
0 条评论
目前还没有评论...
信息
- ID
- 486
- 时间
- ms
- 内存
- MiB
- 难度
- 3
- 标签
- 递交数
- 68
- 已通过
- 38
- 上传者