From d124b1fbcec68f8e940d0f2f0f9a97961abd211e Mon Sep 17 00:00:00 2001 From: Luke Videckis Date: Fri, 6 Mar 2026 14:51:23 -0700 Subject: [PATCH 01/22] first draft of dsu weighted --- library/dsu/dsu_weighted.hpp | 27 +++++++++++++ .../dsu/dsu_weighted_lib_checker.test.cpp | 40 +++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 library/dsu/dsu_weighted.hpp create mode 100644 tests/library_checker_aizu_tests/dsu/dsu_weighted_lib_checker.test.cpp diff --git a/library/dsu/dsu_weighted.hpp b/library/dsu/dsu_weighted.hpp new file mode 100644 index 00000000..97333e04 --- /dev/null +++ b/library/dsu/dsu_weighted.hpp @@ -0,0 +1,27 @@ +#pragma once +struct dsu_weighted { + int n; + vi p; + vector d; + dsu_weighted(int n): n(n), p(n, -1), d(n) {} + int f(int u) { + if (p[u] < 0) return u; + int root = f(p[u]); + d[u] += d[p[u]]; + return p[u] = root; + } + int size(int u) { return -p[f(u)]; } + ll diff(int u, int v) { + return f(u) == f(v) ? d[v] - d[u] : 1e18; + } + bool join(int u, int v, ll w) { + w += d[u] - d[v]; + u = f(u), v = f(v); + if (u == v) return 0; + if (p[u] > p[v]) swap(u, v), w = -w; + p[u] += p[v]; + p[v] = u; + d[v] = w; + return 1; + } +}; diff --git a/tests/library_checker_aizu_tests/dsu/dsu_weighted_lib_checker.test.cpp b/tests/library_checker_aizu_tests/dsu/dsu_weighted_lib_checker.test.cpp new file mode 100644 index 00000000..ce6e2a12 --- /dev/null +++ b/tests/library_checker_aizu_tests/dsu/dsu_weighted_lib_checker.test.cpp @@ -0,0 +1,40 @@ +#define PROBLEM \ + "https://judge.yosupo.jp/problem/unionfind_with_potential" +#include "../template.hpp" +#include "../../../library/dsu/dsu_weighted.hpp" +#include "../../../library/dsu/dsu.hpp" +const int mod = 998'244'353; +int main() { + cin.tie(0)->sync_with_stdio(0); + int n, q; + cin >> n >> q; + dsu_weighted dsu_w(n); + DSU dsu(n); + while (q--) { + int type, u, v; + cin >> type >> u >> v; + if (type == 0) { + assert(dsu.size(u) == dsu_w.size(u)); + assert(dsu.size(v) == dsu_w.size(v)); + int w; + cin >> w; + ll curr_diff = dsu_w.diff(u, v); + if (curr_diff == 1e18) { + assert(dsu_w.join(u, v, w)); + cout << 1 << '\n'; + } else + cout << ((curr_diff % mod + mod) % mod == w) + << '\n'; + dsu.join(u, v); + assert(dsu.size(u) == dsu_w.size(u)); + assert(dsu.size(v) == dsu_w.size(v)); + } else { + assert(dsu.size(u) == dsu_w.size(u)); + assert(dsu.size(v) == dsu_w.size(v)); + ll curr_diff = dsu_w.diff(u, v); + if (curr_diff == 1e18) cout << -1 << '\n'; + else cout << (curr_diff % mod + mod) % mod << '\n'; + } + } + return 0; +} From 5395751eb78afa905836fc1dcf2aa019457ef320 Mon Sep 17 00:00:00 2001 From: GitHub Date: Fri, 6 Mar 2026 21:52:46 +0000 Subject: [PATCH 02/22] [auto-verifier] verify commit d124b1fbcec68f8e940d0f2f0f9a97961abd211e --- .verify-helper/timestamps.remote.json | 1 + 1 file changed, 1 insertion(+) diff --git a/.verify-helper/timestamps.remote.json b/.verify-helper/timestamps.remote.json index 2da69947..104cf0bc 100644 --- a/.verify-helper/timestamps.remote.json +++ b/.verify-helper/timestamps.remote.json @@ -44,6 +44,7 @@ "tests/library_checker_aizu_tests/data_structures/simple_tree_walk.test.cpp": "2026-03-01 19:36:27 -0700", "tests/library_checker_aizu_tests/dsu/dsu.test.cpp": "2026-02-27 15:26:53 -0700", "tests/library_checker_aizu_tests/dsu/dsu_bipartite.test.cpp": "2026-02-27 15:26:53 -0700", +"tests/library_checker_aizu_tests/dsu/dsu_weighted_lib_checker.test.cpp": "2026-03-06 14:51:23 -0700", "tests/library_checker_aizu_tests/dsu/kruskal_tree_aizu.test.cpp": "2026-02-27 15:26:53 -0700", "tests/library_checker_aizu_tests/dsu/line_tree_aizu.test.cpp": "2026-02-27 15:26:53 -0700", "tests/library_checker_aizu_tests/dsu/line_tree_lib_checker.test.cpp": "2026-02-27 15:26:53 -0700", From 527432c4ebeb1677cec47cd3f48093faf8e2468f Mon Sep 17 00:00:00 2001 From: Luke Videckis Date: Fri, 6 Mar 2026 15:00:52 -0700 Subject: [PATCH 03/22] golf --- library/dsu/dsu_weighted.hpp | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/library/dsu/dsu_weighted.hpp b/library/dsu/dsu_weighted.hpp index 97333e04..6627610a 100644 --- a/library/dsu/dsu_weighted.hpp +++ b/library/dsu/dsu_weighted.hpp @@ -1,14 +1,12 @@ #pragma once struct dsu_weighted { - int n; vi p; vector d; - dsu_weighted(int n): n(n), p(n, -1), d(n) {} + dsu_weighted(int n): p(n, -1), d(n) {} int f(int u) { if (p[u] < 0) return u; int root = f(p[u]); - d[u] += d[p[u]]; - return p[u] = root; + return d[u] += d[p[u]], p[u] = root; } int size(int u) { return -p[f(u)]; } ll diff(int u, int v) { @@ -16,12 +14,8 @@ struct dsu_weighted { } bool join(int u, int v, ll w) { w += d[u] - d[v]; - u = f(u), v = f(v); - if (u == v) return 0; + if ((u = f(u)) == (v = f(v))) return 0; if (p[u] > p[v]) swap(u, v), w = -w; - p[u] += p[v]; - p[v] = u; - d[v] = w; - return 1; + return p[u] += p[v], p[v] = u, d[v] = w, 1; } }; From 1a8e762b9423e976bb300b03e1b0e22613e5e2f1 Mon Sep 17 00:00:00 2001 From: GitHub Date: Fri, 6 Mar 2026 22:02:08 +0000 Subject: [PATCH 04/22] [auto-verifier] verify commit 527432c4ebeb1677cec47cd3f48093faf8e2468f --- .verify-helper/timestamps.remote.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.verify-helper/timestamps.remote.json b/.verify-helper/timestamps.remote.json index 104cf0bc..18b0a204 100644 --- a/.verify-helper/timestamps.remote.json +++ b/.verify-helper/timestamps.remote.json @@ -44,7 +44,7 @@ "tests/library_checker_aizu_tests/data_structures/simple_tree_walk.test.cpp": "2026-03-01 19:36:27 -0700", "tests/library_checker_aizu_tests/dsu/dsu.test.cpp": "2026-02-27 15:26:53 -0700", "tests/library_checker_aizu_tests/dsu/dsu_bipartite.test.cpp": "2026-02-27 15:26:53 -0700", -"tests/library_checker_aizu_tests/dsu/dsu_weighted_lib_checker.test.cpp": "2026-03-06 14:51:23 -0700", +"tests/library_checker_aizu_tests/dsu/dsu_weighted_lib_checker.test.cpp": "2026-03-06 15:00:52 -0700", "tests/library_checker_aizu_tests/dsu/kruskal_tree_aizu.test.cpp": "2026-02-27 15:26:53 -0700", "tests/library_checker_aizu_tests/dsu/line_tree_aizu.test.cpp": "2026-02-27 15:26:53 -0700", "tests/library_checker_aizu_tests/dsu/line_tree_lib_checker.test.cpp": "2026-02-27 15:26:53 -0700", From f708280dbd511d9a8a2e2463ef8c169189b304cb Mon Sep 17 00:00:00 2001 From: Luke Videckis Date: Fri, 6 Mar 2026 15:16:16 -0700 Subject: [PATCH 05/22] add aizu test now --- library/dsu/dsu_weighted.hpp | 8 +++++ .../dsu/dsu_weighted_aizu.test.cpp | 33 +++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 tests/library_checker_aizu_tests/dsu/dsu_weighted_aizu.test.cpp diff --git a/library/dsu/dsu_weighted.hpp b/library/dsu/dsu_weighted.hpp index 6627610a..d099cc2e 100644 --- a/library/dsu/dsu_weighted.hpp +++ b/library/dsu/dsu_weighted.hpp @@ -1,4 +1,12 @@ #pragma once +//! @code +//! dsu_weighted dsu(n); +//! dsu.join(u, v, w); +//! // we now know a[u] == a[v] + +//! wdflasndoasdinvoasdiinfasd +//! @endcode +//! @time O(n + q * \alpha(n)) +//! @space O(n) struct dsu_weighted { vi p; vector d; diff --git a/tests/library_checker_aizu_tests/dsu/dsu_weighted_aizu.test.cpp b/tests/library_checker_aizu_tests/dsu/dsu_weighted_aizu.test.cpp new file mode 100644 index 00000000..3a91161a --- /dev/null +++ b/tests/library_checker_aizu_tests/dsu/dsu_weighted_aizu.test.cpp @@ -0,0 +1,33 @@ +#define PROBLEM \ + "https://onlinejudge.u-aizu.ac.jp/problems/DSL_1_B" +#include "../template.hpp" +#include "../../../library/dsu/dsu_weighted.hpp" +#include "../../../library/dsu/dsu.hpp" +int main() { + cin.tie(0)->sync_with_stdio(0); + int n, q; + cin >> n >> q; + dsu_weighted dsu_w(n); + DSU dsu(n); + while (q--) { + int type, u, v; + cin >> type >> u >> v; + if (type == 0) { + assert(dsu.size(u) == dsu_w.size(u)); + assert(dsu.size(v) == dsu_w.size(v)); + int w; + cin >> w; + dsu_w.join(u, v, w); + dsu.join(u, v); + assert(dsu.size(u) == dsu_w.size(u)); + assert(dsu.size(v) == dsu_w.size(v)); + } else { + assert(dsu.size(u) == dsu_w.size(u)); + assert(dsu.size(v) == dsu_w.size(v)); + ll curr_diff = dsu_w.diff(u, v); + if (curr_diff == 1e18) cout << "?\n"; + else cout << dsu_w.diff(u, v) << '\n'; + } + } + return 0; +} From 00d952dc16121b94ef24a89960b75388449f0b43 Mon Sep 17 00:00:00 2001 From: GitHub Date: Fri, 6 Mar 2026 22:18:03 +0000 Subject: [PATCH 06/22] [auto-verifier] verify commit f708280dbd511d9a8a2e2463ef8c169189b304cb --- .verify-helper/timestamps.remote.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.verify-helper/timestamps.remote.json b/.verify-helper/timestamps.remote.json index 18b0a204..e811cf1f 100644 --- a/.verify-helper/timestamps.remote.json +++ b/.verify-helper/timestamps.remote.json @@ -44,7 +44,8 @@ "tests/library_checker_aizu_tests/data_structures/simple_tree_walk.test.cpp": "2026-03-01 19:36:27 -0700", "tests/library_checker_aizu_tests/dsu/dsu.test.cpp": "2026-02-27 15:26:53 -0700", "tests/library_checker_aizu_tests/dsu/dsu_bipartite.test.cpp": "2026-02-27 15:26:53 -0700", -"tests/library_checker_aizu_tests/dsu/dsu_weighted_lib_checker.test.cpp": "2026-03-06 15:00:52 -0700", +"tests/library_checker_aizu_tests/dsu/dsu_weighted_aizu.test.cpp": "2026-03-06 15:16:16 -0700", +"tests/library_checker_aizu_tests/dsu/dsu_weighted_lib_checker.test.cpp": "2026-03-06 15:16:16 -0700", "tests/library_checker_aizu_tests/dsu/kruskal_tree_aizu.test.cpp": "2026-02-27 15:26:53 -0700", "tests/library_checker_aizu_tests/dsu/line_tree_aizu.test.cpp": "2026-02-27 15:26:53 -0700", "tests/library_checker_aizu_tests/dsu/line_tree_lib_checker.test.cpp": "2026-02-27 15:26:53 -0700", From 250ce7d6da060d2d26d7a3d74b29002bc736fe45 Mon Sep 17 00:00:00 2001 From: Luke Videckis Date: Fri, 6 Mar 2026 15:23:23 -0700 Subject: [PATCH 07/22] add docs now --- library/dsu/dsu_weighted.hpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/library/dsu/dsu_weighted.hpp b/library/dsu/dsu_weighted.hpp index d099cc2e..345bdc3e 100644 --- a/library/dsu/dsu_weighted.hpp +++ b/library/dsu/dsu_weighted.hpp @@ -2,8 +2,9 @@ //! @code //! dsu_weighted dsu(n); //! dsu.join(u, v, w); -//! // we now know a[u] == a[v] + -//! wdflasndoasdinvoasdiinfasd +//! // we now know a[u] = a[v] + w +//! ll w = dsu.diff(u, v) +//! // satisfies a[u] = a[v] + w based on prior joins //! @endcode //! @time O(n + q * \alpha(n)) //! @space O(n) From ef81d50c45eeaf041540e02dbf440ed991d54b2e Mon Sep 17 00:00:00 2001 From: GitHub Date: Fri, 6 Mar 2026 22:25:06 +0000 Subject: [PATCH 08/22] [auto-verifier] verify commit 82effb2f7831ba1225b7cb47309d882c130757c7 --- .verify-helper/timestamps.remote.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.verify-helper/timestamps.remote.json b/.verify-helper/timestamps.remote.json index e811cf1f..775a04be 100644 --- a/.verify-helper/timestamps.remote.json +++ b/.verify-helper/timestamps.remote.json @@ -44,8 +44,8 @@ "tests/library_checker_aizu_tests/data_structures/simple_tree_walk.test.cpp": "2026-03-01 19:36:27 -0700", "tests/library_checker_aizu_tests/dsu/dsu.test.cpp": "2026-02-27 15:26:53 -0700", "tests/library_checker_aizu_tests/dsu/dsu_bipartite.test.cpp": "2026-02-27 15:26:53 -0700", -"tests/library_checker_aizu_tests/dsu/dsu_weighted_aizu.test.cpp": "2026-03-06 15:16:16 -0700", -"tests/library_checker_aizu_tests/dsu/dsu_weighted_lib_checker.test.cpp": "2026-03-06 15:16:16 -0700", +"tests/library_checker_aizu_tests/dsu/dsu_weighted_aizu.test.cpp": "2026-03-06 15:23:23 -0700", +"tests/library_checker_aizu_tests/dsu/dsu_weighted_lib_checker.test.cpp": "2026-03-06 15:23:23 -0700", "tests/library_checker_aizu_tests/dsu/kruskal_tree_aizu.test.cpp": "2026-02-27 15:26:53 -0700", "tests/library_checker_aizu_tests/dsu/line_tree_aizu.test.cpp": "2026-02-27 15:26:53 -0700", "tests/library_checker_aizu_tests/dsu/line_tree_lib_checker.test.cpp": "2026-02-27 15:26:53 -0700", From 73cafbd5d8f2b138ecf4de8991c62be104489a8d Mon Sep 17 00:00:00 2001 From: Luke Videckis Date: Fri, 6 Mar 2026 15:30:46 -0700 Subject: [PATCH 09/22] do it this way actually --- .../dsu/dsu_weighted_aizu.test.cpp | 6 ++---- .../dsu/dsu_weighted_lib_checker.test.cpp | 6 ++---- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/tests/library_checker_aizu_tests/dsu/dsu_weighted_aizu.test.cpp b/tests/library_checker_aizu_tests/dsu/dsu_weighted_aizu.test.cpp index 3a91161a..5eca0385 100644 --- a/tests/library_checker_aizu_tests/dsu/dsu_weighted_aizu.test.cpp +++ b/tests/library_checker_aizu_tests/dsu/dsu_weighted_aizu.test.cpp @@ -13,8 +13,6 @@ int main() { int type, u, v; cin >> type >> u >> v; if (type == 0) { - assert(dsu.size(u) == dsu_w.size(u)); - assert(dsu.size(v) == dsu_w.size(v)); int w; cin >> w; dsu_w.join(u, v, w); @@ -22,11 +20,11 @@ int main() { assert(dsu.size(u) == dsu_w.size(u)); assert(dsu.size(v) == dsu_w.size(v)); } else { - assert(dsu.size(u) == dsu_w.size(u)); - assert(dsu.size(v) == dsu_w.size(v)); ll curr_diff = dsu_w.diff(u, v); if (curr_diff == 1e18) cout << "?\n"; else cout << dsu_w.diff(u, v) << '\n'; + assert(dsu.size(u) == dsu_w.size(u)); + assert(dsu.size(v) == dsu_w.size(v)); } } return 0; diff --git a/tests/library_checker_aizu_tests/dsu/dsu_weighted_lib_checker.test.cpp b/tests/library_checker_aizu_tests/dsu/dsu_weighted_lib_checker.test.cpp index ce6e2a12..d41406a2 100644 --- a/tests/library_checker_aizu_tests/dsu/dsu_weighted_lib_checker.test.cpp +++ b/tests/library_checker_aizu_tests/dsu/dsu_weighted_lib_checker.test.cpp @@ -14,8 +14,6 @@ int main() { int type, u, v; cin >> type >> u >> v; if (type == 0) { - assert(dsu.size(u) == dsu_w.size(u)); - assert(dsu.size(v) == dsu_w.size(v)); int w; cin >> w; ll curr_diff = dsu_w.diff(u, v); @@ -29,11 +27,11 @@ int main() { assert(dsu.size(u) == dsu_w.size(u)); assert(dsu.size(v) == dsu_w.size(v)); } else { - assert(dsu.size(u) == dsu_w.size(u)); - assert(dsu.size(v) == dsu_w.size(v)); ll curr_diff = dsu_w.diff(u, v); if (curr_diff == 1e18) cout << -1 << '\n'; else cout << (curr_diff % mod + mod) % mod << '\n'; + assert(dsu.size(u) == dsu_w.size(u)); + assert(dsu.size(v) == dsu_w.size(v)); } } return 0; From bc8888176442bcb29f58bcb4189d0c474dacbc74 Mon Sep 17 00:00:00 2001 From: GitHub Date: Fri, 6 Mar 2026 22:32:20 +0000 Subject: [PATCH 10/22] [auto-verifier] verify commit 73cafbd5d8f2b138ecf4de8991c62be104489a8d --- .verify-helper/timestamps.remote.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.verify-helper/timestamps.remote.json b/.verify-helper/timestamps.remote.json index 775a04be..08b3e657 100644 --- a/.verify-helper/timestamps.remote.json +++ b/.verify-helper/timestamps.remote.json @@ -44,8 +44,7 @@ "tests/library_checker_aizu_tests/data_structures/simple_tree_walk.test.cpp": "2026-03-01 19:36:27 -0700", "tests/library_checker_aizu_tests/dsu/dsu.test.cpp": "2026-02-27 15:26:53 -0700", "tests/library_checker_aizu_tests/dsu/dsu_bipartite.test.cpp": "2026-02-27 15:26:53 -0700", -"tests/library_checker_aizu_tests/dsu/dsu_weighted_aizu.test.cpp": "2026-03-06 15:23:23 -0700", -"tests/library_checker_aizu_tests/dsu/dsu_weighted_lib_checker.test.cpp": "2026-03-06 15:23:23 -0700", +"tests/library_checker_aizu_tests/dsu/dsu_weighted_lib_checker.test.cpp": "2026-03-06 15:30:46 -0700", "tests/library_checker_aizu_tests/dsu/kruskal_tree_aizu.test.cpp": "2026-02-27 15:26:53 -0700", "tests/library_checker_aizu_tests/dsu/line_tree_aizu.test.cpp": "2026-02-27 15:26:53 -0700", "tests/library_checker_aizu_tests/dsu/line_tree_lib_checker.test.cpp": "2026-02-27 15:26:53 -0700", From 035c64c5371721761185c8e4aa793e71cf03968f Mon Sep 17 00:00:00 2001 From: Luke Videckis Date: Fri, 6 Mar 2026 15:34:44 -0700 Subject: [PATCH 11/22] fix bug in test actually --- .../dsu/dsu_weighted_lib_checker.test.cpp | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/tests/library_checker_aizu_tests/dsu/dsu_weighted_lib_checker.test.cpp b/tests/library_checker_aizu_tests/dsu/dsu_weighted_lib_checker.test.cpp index d41406a2..1def0e48 100644 --- a/tests/library_checker_aizu_tests/dsu/dsu_weighted_lib_checker.test.cpp +++ b/tests/library_checker_aizu_tests/dsu/dsu_weighted_lib_checker.test.cpp @@ -16,12 +16,10 @@ int main() { if (type == 0) { int w; cin >> w; - ll curr_diff = dsu_w.diff(u, v); - if (curr_diff == 1e18) { - assert(dsu_w.join(u, v, w)); - cout << 1 << '\n'; - } else - cout << ((curr_diff % mod + mod) % mod == w) + bool joined = dsu_w.join(u, v, w); + if (joined) cout << 1 << '\n'; + else + cout << ((dsu_w.diff(u, v) % mod + mod) % mod == w) << '\n'; dsu.join(u, v); assert(dsu.size(u) == dsu_w.size(u)); From a990dd1da734e6ef87fb05051f438717f004f177 Mon Sep 17 00:00:00 2001 From: GitHub Date: Fri, 6 Mar 2026 22:36:36 +0000 Subject: [PATCH 12/22] [auto-verifier] verify commit 035c64c5371721761185c8e4aa793e71cf03968f --- .verify-helper/timestamps.remote.json | 1 - 1 file changed, 1 deletion(-) diff --git a/.verify-helper/timestamps.remote.json b/.verify-helper/timestamps.remote.json index 08b3e657..2da69947 100644 --- a/.verify-helper/timestamps.remote.json +++ b/.verify-helper/timestamps.remote.json @@ -44,7 +44,6 @@ "tests/library_checker_aizu_tests/data_structures/simple_tree_walk.test.cpp": "2026-03-01 19:36:27 -0700", "tests/library_checker_aizu_tests/dsu/dsu.test.cpp": "2026-02-27 15:26:53 -0700", "tests/library_checker_aizu_tests/dsu/dsu_bipartite.test.cpp": "2026-02-27 15:26:53 -0700", -"tests/library_checker_aizu_tests/dsu/dsu_weighted_lib_checker.test.cpp": "2026-03-06 15:30:46 -0700", "tests/library_checker_aizu_tests/dsu/kruskal_tree_aizu.test.cpp": "2026-02-27 15:26:53 -0700", "tests/library_checker_aizu_tests/dsu/line_tree_aizu.test.cpp": "2026-02-27 15:26:53 -0700", "tests/library_checker_aizu_tests/dsu/line_tree_lib_checker.test.cpp": "2026-02-27 15:26:53 -0700", From b60cf61f9787657aa996a1635cd56df62aaf221c Mon Sep 17 00:00:00 2001 From: Luke Videckis Date: Fri, 6 Mar 2026 15:48:44 -0700 Subject: [PATCH 13/22] fix+golf --- library/dsu/dsu_bipartite.hpp | 12 +++++------- library/dsu/dsu_weighted.hpp | 3 ++- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/library/dsu/dsu_bipartite.hpp b/library/dsu/dsu_bipartite.hpp index f7009587..b08fd523 100644 --- a/library/dsu/dsu_bipartite.hpp +++ b/library/dsu/dsu_bipartite.hpp @@ -8,14 +8,14 @@ struct dsu_bipartite { int f(int v) { if (p[v] < 0) return v; int root = f(p[v]); - parity[v] ^= parity[p[v]]; - return p[v] = root; + return parity[v] ^= parity[p[v]], p[v] = root; } + int size(int v) { return -p[f(v)]; } + bool is_bipartite(int v) { return is_bi[f(v)]; } bool join(int u, int v) { int root_u = f(u), root_v = f(v); - int new_parity = parity[v] ^ parity[u]; - u = root_u, v = root_v; - if (u == v) { + int new_parity = parity[u] ^ parity[v]; + if ((u = root_u) == (v = root_v)) { is_bi[u] &= new_parity; return 0; } @@ -25,6 +25,4 @@ struct dsu_bipartite { p[u] += p[v], p[v] = u; return 1; } - int size(int v) { return -p[f(v)]; } - bool is_bipartite(int v) { return is_bi[f(v)]; } }; diff --git a/library/dsu/dsu_weighted.hpp b/library/dsu/dsu_weighted.hpp index 345bdc3e..027bec0a 100644 --- a/library/dsu/dsu_weighted.hpp +++ b/library/dsu/dsu_weighted.hpp @@ -22,8 +22,9 @@ struct dsu_weighted { return f(u) == f(v) ? d[v] - d[u] : 1e18; } bool join(int u, int v, ll w) { + int root_u = f(u), root_v = f(v); w += d[u] - d[v]; - if ((u = f(u)) == (v = f(v))) return 0; + if ((u = root_u) == (v = root_v)) return 0; if (p[u] > p[v]) swap(u, v), w = -w; return p[u] += p[v], p[v] = u, d[v] = w, 1; } From a6babced2bcef8d4ca9446b58684215f9e89a530 Mon Sep 17 00:00:00 2001 From: GitHub Date: Fri, 6 Mar 2026 22:51:33 +0000 Subject: [PATCH 14/22] [auto-verifier] verify commit b60cf61f9787657aa996a1635cd56df62aaf221c --- .verify-helper/timestamps.remote.json | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.verify-helper/timestamps.remote.json b/.verify-helper/timestamps.remote.json index 2da69947..f8e91363 100644 --- a/.verify-helper/timestamps.remote.json +++ b/.verify-helper/timestamps.remote.json @@ -43,7 +43,9 @@ "tests/library_checker_aizu_tests/data_structures/simple_tree_line.test.cpp": "2026-03-01 19:36:27 -0700", "tests/library_checker_aizu_tests/data_structures/simple_tree_walk.test.cpp": "2026-03-01 19:36:27 -0700", "tests/library_checker_aizu_tests/dsu/dsu.test.cpp": "2026-02-27 15:26:53 -0700", -"tests/library_checker_aizu_tests/dsu/dsu_bipartite.test.cpp": "2026-02-27 15:26:53 -0700", +"tests/library_checker_aizu_tests/dsu/dsu_bipartite.test.cpp": "2026-03-06 15:48:44 -0700", +"tests/library_checker_aizu_tests/dsu/dsu_weighted_aizu.test.cpp": "2026-03-06 15:48:44 -0700", +"tests/library_checker_aizu_tests/dsu/dsu_weighted_lib_checker.test.cpp": "2026-03-06 15:48:44 -0700", "tests/library_checker_aizu_tests/dsu/kruskal_tree_aizu.test.cpp": "2026-02-27 15:26:53 -0700", "tests/library_checker_aizu_tests/dsu/line_tree_aizu.test.cpp": "2026-02-27 15:26:53 -0700", "tests/library_checker_aizu_tests/dsu/line_tree_lib_checker.test.cpp": "2026-02-27 15:26:53 -0700", From d5626d6af3c146396ce2bfd60176a5302acaac07 Mon Sep 17 00:00:00 2001 From: Luke Videckis Date: Fri, 6 Mar 2026 15:51:34 -0700 Subject: [PATCH 15/22] more golf --- library/dsu/dsu_bipartite.hpp | 14 +++++++------- library/dsu/dsu_weighted.hpp | 8 ++++---- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/library/dsu/dsu_bipartite.hpp b/library/dsu/dsu_bipartite.hpp index b08fd523..145774f5 100644 --- a/library/dsu/dsu_bipartite.hpp +++ b/library/dsu/dsu_bipartite.hpp @@ -13,16 +13,16 @@ struct dsu_bipartite { int size(int v) { return -p[f(v)]; } bool is_bipartite(int v) { return is_bi[f(v)]; } bool join(int u, int v) { - int root_u = f(u), root_v = f(v); + int x = f(u), y = f(v); int new_parity = parity[u] ^ parity[v]; - if ((u = root_u) == (v = root_v)) { - is_bi[u] &= new_parity; + if (x == y) { + is_bi[x] &= new_parity; return 0; } - if (p[u] > p[v]) swap(u, v); - is_bi[u] &= is_bi[v]; - parity[v] = new_parity ^ 1; - p[u] += p[v], p[v] = u; + if (p[x] > p[y]) swap(x, y); + is_bi[x] &= is_bi[y]; + parity[y] = new_parity ^ 1; + p[x] += p[y], p[y] = x; return 1; } }; diff --git a/library/dsu/dsu_weighted.hpp b/library/dsu/dsu_weighted.hpp index 027bec0a..0f208fc5 100644 --- a/library/dsu/dsu_weighted.hpp +++ b/library/dsu/dsu_weighted.hpp @@ -22,10 +22,10 @@ struct dsu_weighted { return f(u) == f(v) ? d[v] - d[u] : 1e18; } bool join(int u, int v, ll w) { - int root_u = f(u), root_v = f(v); + int x = f(u), y = f(v); w += d[u] - d[v]; - if ((u = root_u) == (v = root_v)) return 0; - if (p[u] > p[v]) swap(u, v), w = -w; - return p[u] += p[v], p[v] = u, d[v] = w, 1; + if (x == y) return 0; + if (p[x] > p[y]) swap(x, y), w = -w; + return p[x] += p[y], p[y] = x, d[y] = w, 1; } }; From 1955492402abec7931c467f9c9db09232d038f77 Mon Sep 17 00:00:00 2001 From: Luke Videckis Date: Fri, 6 Mar 2026 15:52:44 -0700 Subject: [PATCH 16/22] fix --- tests/.config/.cppcheck_suppression_list | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/.config/.cppcheck_suppression_list b/tests/.config/.cppcheck_suppression_list index 85477864..d6ad7f70 100644 --- a/tests/.config/.cppcheck_suppression_list +++ b/tests/.config/.cppcheck_suppression_list @@ -43,6 +43,7 @@ knownConditionTrueFalse:../library/strings/suffix_array/suffix_array.hpp:62 knownConditionTrueFalse:../library/strings/suffix_array/suffix_array_short.hpp:34 knownConditionTrueFalse:../library/dsu/kruskal_tree.hpp:13 knownConditionTrueFalse:../library/dsu/dsu.hpp:11 +knownConditionTrueFalse:../library/dsu/dsu_weighted.hpp:29 constVariable:../kactl/content/numerical/NumberTheoreticTransform.h:30 constVariable:../kactl/content/graph/CompressTree.h:20 constVariableReference:../kactl/content/graph/CompressTree.h:20 From 709fa477f580e43c76818ac11b73e1353cada598 Mon Sep 17 00:00:00 2001 From: Luke Videckis Date: Fri, 6 Mar 2026 15:53:31 -0700 Subject: [PATCH 17/22] another fix --- tests/library_checker_aizu_tests/dsu/dsu_weighted_aizu.test.cpp | 2 +- .../dsu/dsu_weighted_lib_checker.test.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/library_checker_aizu_tests/dsu/dsu_weighted_aizu.test.cpp b/tests/library_checker_aizu_tests/dsu/dsu_weighted_aizu.test.cpp index 5eca0385..7375f831 100644 --- a/tests/library_checker_aizu_tests/dsu/dsu_weighted_aizu.test.cpp +++ b/tests/library_checker_aizu_tests/dsu/dsu_weighted_aizu.test.cpp @@ -21,7 +21,7 @@ int main() { assert(dsu.size(v) == dsu_w.size(v)); } else { ll curr_diff = dsu_w.diff(u, v); - if (curr_diff == 1e18) cout << "?\n"; + if (curr_diff == ll(1e18)) cout << "?\n"; else cout << dsu_w.diff(u, v) << '\n'; assert(dsu.size(u) == dsu_w.size(u)); assert(dsu.size(v) == dsu_w.size(v)); diff --git a/tests/library_checker_aizu_tests/dsu/dsu_weighted_lib_checker.test.cpp b/tests/library_checker_aizu_tests/dsu/dsu_weighted_lib_checker.test.cpp index 1def0e48..1d309423 100644 --- a/tests/library_checker_aizu_tests/dsu/dsu_weighted_lib_checker.test.cpp +++ b/tests/library_checker_aizu_tests/dsu/dsu_weighted_lib_checker.test.cpp @@ -26,7 +26,7 @@ int main() { assert(dsu.size(v) == dsu_w.size(v)); } else { ll curr_diff = dsu_w.diff(u, v); - if (curr_diff == 1e18) cout << -1 << '\n'; + if (curr_diff == ll(1e18)) cout << -1 << '\n'; else cout << (curr_diff % mod + mod) % mod << '\n'; assert(dsu.size(u) == dsu_w.size(u)); assert(dsu.size(v) == dsu_w.size(v)); From 525fcf322eaa1f68c9008aaa615f4789e1618d22 Mon Sep 17 00:00:00 2001 From: Luke Videckis Date: Fri, 6 Mar 2026 15:53:57 -0700 Subject: [PATCH 18/22] another fix --- tests/scripts/compile_commented_snippets.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/scripts/compile_commented_snippets.sh b/tests/scripts/compile_commented_snippets.sh index 3caa3df9..4d73abfc 100755 --- a/tests/scripts/compile_commented_snippets.sh +++ b/tests/scripts/compile_commented_snippets.sh @@ -25,7 +25,7 @@ git submodule update echo "vi rhs;" echo "vector mat;" echo "vector> grid;" - echo "int n,m,k,tl,tr,l,r,l1,r1,l2,r2,s_l,s_r,root_l,root_r,source,sink,total_flow,bccid,u,v,lsz,rsz,cols,cap,num,x,y,i,j,i1,i2,j1,j2,len;" + echo "int n,m,k,tl,tr,l,r,l1,r1,l2,r2,s_l,s_r,root_l,root_r,source,sink,total_flow,bccid,u,v,w,lsz,rsz,cols,cap,num,x,y,i,j,i1,i2,j1,j2,len;" } >entire_library_without_main { From 45d173fd9ba400232741a6e61e86a76976acd4bc Mon Sep 17 00:00:00 2001 From: GitHub Date: Fri, 6 Mar 2026 22:56:39 +0000 Subject: [PATCH 19/22] [auto-verifier] verify commit 525fcf322eaa1f68c9008aaa615f4789e1618d22 --- .verify-helper/timestamps.remote.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.verify-helper/timestamps.remote.json b/.verify-helper/timestamps.remote.json index f8e91363..ad7245b4 100644 --- a/.verify-helper/timestamps.remote.json +++ b/.verify-helper/timestamps.remote.json @@ -43,9 +43,9 @@ "tests/library_checker_aizu_tests/data_structures/simple_tree_line.test.cpp": "2026-03-01 19:36:27 -0700", "tests/library_checker_aizu_tests/data_structures/simple_tree_walk.test.cpp": "2026-03-01 19:36:27 -0700", "tests/library_checker_aizu_tests/dsu/dsu.test.cpp": "2026-02-27 15:26:53 -0700", -"tests/library_checker_aizu_tests/dsu/dsu_bipartite.test.cpp": "2026-03-06 15:48:44 -0700", -"tests/library_checker_aizu_tests/dsu/dsu_weighted_aizu.test.cpp": "2026-03-06 15:48:44 -0700", -"tests/library_checker_aizu_tests/dsu/dsu_weighted_lib_checker.test.cpp": "2026-03-06 15:48:44 -0700", +"tests/library_checker_aizu_tests/dsu/dsu_bipartite.test.cpp": "2026-03-06 15:51:34 -0700", +"tests/library_checker_aizu_tests/dsu/dsu_weighted_aizu.test.cpp": "2026-03-06 15:53:31 -0700", +"tests/library_checker_aizu_tests/dsu/dsu_weighted_lib_checker.test.cpp": "2026-03-06 15:53:31 -0700", "tests/library_checker_aizu_tests/dsu/kruskal_tree_aizu.test.cpp": "2026-02-27 15:26:53 -0700", "tests/library_checker_aizu_tests/dsu/line_tree_aizu.test.cpp": "2026-02-27 15:26:53 -0700", "tests/library_checker_aizu_tests/dsu/line_tree_lib_checker.test.cpp": "2026-02-27 15:26:53 -0700", From bcd04c66291424461f117a8458845a9227860d74 Mon Sep 17 00:00:00 2001 From: Luke Videckis Date: Fri, 6 Mar 2026 15:57:01 -0700 Subject: [PATCH 20/22] one last optimization --- library/dsu/dsu_weighted.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/dsu/dsu_weighted.hpp b/library/dsu/dsu_weighted.hpp index 0f208fc5..8c224c8f 100644 --- a/library/dsu/dsu_weighted.hpp +++ b/library/dsu/dsu_weighted.hpp @@ -23,8 +23,8 @@ struct dsu_weighted { } bool join(int u, int v, ll w) { int x = f(u), y = f(v); - w += d[u] - d[v]; if (x == y) return 0; + w += d[u] - d[v]; if (p[x] > p[y]) swap(x, y), w = -w; return p[x] += p[y], p[y] = x, d[y] = w, 1; } From 92091d5a2dc1ca246619563544825f05dff729ef Mon Sep 17 00:00:00 2001 From: GitHub Date: Fri, 6 Mar 2026 22:58:34 +0000 Subject: [PATCH 21/22] [auto-verifier] verify commit bcd04c66291424461f117a8458845a9227860d74 --- .verify-helper/timestamps.remote.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.verify-helper/timestamps.remote.json b/.verify-helper/timestamps.remote.json index ad7245b4..6d595151 100644 --- a/.verify-helper/timestamps.remote.json +++ b/.verify-helper/timestamps.remote.json @@ -44,8 +44,8 @@ "tests/library_checker_aizu_tests/data_structures/simple_tree_walk.test.cpp": "2026-03-01 19:36:27 -0700", "tests/library_checker_aizu_tests/dsu/dsu.test.cpp": "2026-02-27 15:26:53 -0700", "tests/library_checker_aizu_tests/dsu/dsu_bipartite.test.cpp": "2026-03-06 15:51:34 -0700", -"tests/library_checker_aizu_tests/dsu/dsu_weighted_aizu.test.cpp": "2026-03-06 15:53:31 -0700", -"tests/library_checker_aizu_tests/dsu/dsu_weighted_lib_checker.test.cpp": "2026-03-06 15:53:31 -0700", +"tests/library_checker_aizu_tests/dsu/dsu_weighted_aizu.test.cpp": "2026-03-06 15:57:01 -0700", +"tests/library_checker_aizu_tests/dsu/dsu_weighted_lib_checker.test.cpp": "2026-03-06 15:57:01 -0700", "tests/library_checker_aizu_tests/dsu/kruskal_tree_aizu.test.cpp": "2026-02-27 15:26:53 -0700", "tests/library_checker_aizu_tests/dsu/line_tree_aizu.test.cpp": "2026-02-27 15:26:53 -0700", "tests/library_checker_aizu_tests/dsu/line_tree_lib_checker.test.cpp": "2026-02-27 15:26:53 -0700", From 3c21db98fdadde1b68ab93df6c72c45b84569017 Mon Sep 17 00:00:00 2001 From: Luke Videckis Date: Fri, 6 Mar 2026 15:59:09 -0700 Subject: [PATCH 22/22] one last fix --- library/dsu/dsu_weighted.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/dsu/dsu_weighted.hpp b/library/dsu/dsu_weighted.hpp index 8c224c8f..113f1237 100644 --- a/library/dsu/dsu_weighted.hpp +++ b/library/dsu/dsu_weighted.hpp @@ -3,7 +3,7 @@ //! dsu_weighted dsu(n); //! dsu.join(u, v, w); //! // we now know a[u] = a[v] + w -//! ll w = dsu.diff(u, v) +//! ll w = dsu.diff(u, v); //! // satisfies a[u] = a[v] + w based on prior joins //! @endcode //! @time O(n + q * \alpha(n))