Skip to content

Commit 6a4fdba

Browse files
committed
add dsu
1 parent 2641999 commit 6a4fdba

File tree

6 files changed

+88
-9
lines changed

6 files changed

+88
-9
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#pragma once
2+
struct dsu {
3+
vi e;
4+
dsu(int n): e(n, -1) {}
5+
int size(int x) { return -e[go(x)]; }
6+
int go(int x) { return e[x] < 0 ? x : e[x] = go(e[x]); }
7+
bool join(int a, int b) {
8+
if ((a = go(a)) == (b = go(b))) return 0;
9+
if (e[a] > e[b]) swap(a, b);
10+
return e[a] += e[b], e[b] = a, 1;
11+
}
12+
};

library/graphs/mst.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#pragma once
2-
#include "../../kactl/content/data-structures/UnionFind.h"
2+
#include "../data_structures/dsu/dsu.hpp"
33
//! @code
44
//! auto [mst_cost, ids] = mst(w_eds, n);
55
//! @endcode
@@ -14,7 +14,7 @@ pair<ll, vi> mst(const vector<array<int, 3>>& w_eds,
1414
ranges::sort(order, [&](int i, int j) {
1515
return w_eds[i][2] < w_eds[j][2];
1616
});
17-
UF uf(n);
17+
dsu uf(n);
1818
vi ids;
1919
ll cost = 0;
2020
for (int it : order) {
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#define PROBLEM "https://judge.yosupo.jp/problem/unionfind"
2+
#include "../template.hpp"
3+
#include "../../../library/data_structures/dsu/dsu.hpp"
4+
int main() {
5+
cin.tie(0)->sync_with_stdio(0);
6+
int n, q;
7+
cin >> n >> q;
8+
dsu uf(n);
9+
while (q--) {
10+
int type, u, v;
11+
cin >> type >> u >> v;
12+
if (type == 0) uf.join(u, v);
13+
else cout << (uf.go(u) == uf.go(v)) << '\n';
14+
}
15+
return 0;
16+
}

tests/library_checker_aizu_tests/graphs/bcc_callback_lib_checker_two_cc.test.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"https://judge.yosupo.jp/problem/two_edge_connected_components"
33
#include "../template.hpp"
44
#include "../../../library/graphs/bcc_callback.hpp"
5-
#include "../../../kactl/content/data-structures/UnionFind.h"
5+
#include "../../../library/data_structures/dsu/dsu.hpp"
66
int main() {
77
cin.tie(0)->sync_with_stdio(0);
88
int n, m;
@@ -14,7 +14,7 @@ int main() {
1414
adj[u].push_back(v);
1515
adj[v].push_back(u);
1616
}
17-
UF uf(n);
17+
dsu uf(n);
1818
vector<bool> seen(n);
1919
bcc_callback(adj, [&](const vi& nodes) {
2020
int count_edges = 0;
@@ -31,7 +31,7 @@ int main() {
3131
for (int v : nodes) uf.join(v, nodes[0]);
3232
});
3333
vector<vi> two_edge_ccs(n);
34-
rep(i, 0, n) two_edge_ccs[uf.find(i)].push_back(i);
34+
rep(i, 0, n) two_edge_ccs[uf.go(i)].push_back(i);
3535
int cnt_ccs = 0;
3636
rep(i, 0, n) cnt_ccs += (!empty(two_edge_ccs[i]));
3737
cout << cnt_ccs << '\n';

tests/library_checker_aizu_tests/graphs/offline_incremental_scc.test.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"https://judge.yosupo.jp/problem/incremental_scc"
33
#include "../template.hpp"
44
#include "../../../library/graphs/strongly_connected_components/offline_incremental_scc.hpp"
5-
#include "../../../kactl/content/data-structures/UnionFind.h"
5+
#include "../../../library/data_structures/dsu/dsu.hpp"
66
#include "../../../library/math/mod_int.hpp"
77
int main() {
88
cin.tie(0)->sync_with_stdio(0);
@@ -20,13 +20,13 @@ int main() {
2020
iota(all(order), 0);
2121
ranges::sort(all(order), {},
2222
[&](int i) { return joins[i]; });
23-
UF uf(n);
23+
dsu uf(n);
2424
mint sum = 0;
2525
for (int t = 0, it = 0; t < m; t++) {
2626
while (it < m && joins[order[it]] <= t) {
2727
auto [u, v] = eds[order[it]];
28-
u = uf.find(u);
29-
v = uf.find(v);
28+
u = uf.go(u);
29+
v = uf.go(v);
3030
if (uf.e[u] > uf.e[v]) swap(u, v);
3131
if (u != v) {
3232
sum = sum + xs[u] * xs[v];
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#define PROBLEM \
2+
"https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP1_1_A"
3+
#include "../template.hpp"
4+
#include "../../../library/contest/random.hpp"
5+
#include "../../../library/data_structures/dsu/dsu.hpp"
6+
int main() {
7+
cin.tie(0)->sync_with_stdio(0);
8+
for (int n = 0; n <= 100; n++) {
9+
vector<vector<int>> adj(n);
10+
dsu uf(n);
11+
for (int q = 0; q < n; q++) {
12+
int u = rnd(0, n - 1);
13+
int v = rnd(0, n - 1);
14+
bool joined = uf.join(u, v);
15+
{
16+
vector<bool> vis(n);
17+
auto dfs = [&](auto&& self, int v) -> void {
18+
for (int u : adj[v]) {
19+
if (!vis[u]) {
20+
vis[u] = 1;
21+
self(self, u);
22+
}
23+
}
24+
};
25+
vis[u] = 1;
26+
dfs(dfs, u);
27+
assert(vis[v] != joined);
28+
}
29+
adj[u].push_back(v);
30+
adj[v].push_back(u);
31+
for (int i = 0; i < n; i++) {
32+
vector<bool> vis(n);
33+
int cnt_nodes = 0;
34+
auto dfs = [&](auto&& self, int v) -> void {
35+
cnt_nodes++;
36+
for (int u : adj[v]) {
37+
if (!vis[u]) {
38+
vis[u] = 1;
39+
self(self, u);
40+
}
41+
}
42+
};
43+
vis[i] = 1;
44+
dfs(dfs, i);
45+
assert(cnt_nodes == uf.size(i));
46+
}
47+
}
48+
}
49+
cout << "Hello World\n";
50+
return 0;
51+
}

0 commit comments

Comments
 (0)