1 条题解
-
0
使用带扩展域的并查集,将每个节点 拆分为 和 两个域,最后统计 的祖先个数即可。
#include <iostream> #include <bitset> using namespace std; bitset<4005> v; int fa[2005]; int n, m, ans; int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); } void unite(int x, int y) { fa[find(x)] = find(y); } int main() { ios::sync_with_stdio(false); cin.tie(nullptr), cout.tie(nullptr); cin >> n >> m; for (int i = 1; i <= n * 2; ++i) fa[i] = i; while (m--) { char opt; int p, q; cin >> opt >> p >> q; int pf = p, pe = p + n, qf = q, qe = q + n; if (opt == 'F') unite(pf, qf); else unite(pe, qf), unite(pf, qe); } for (int i = 1; i <= n; ++i) { int root = find(i); if (!v[root]) v[root] = true, ++ans; } cout << ans << endl; return 0; }
- 1
信息
- ID
- 857
- 时间
- 1000ms
- 内存
- 125MiB
- 难度
- 4
- 标签
- 递交数
- 17
- 已通过
- 4
- 上传者