Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
d124b1f
first draft of dsu weighted
lrvideckis Mar 6, 2026
5395751
[auto-verifier] verify commit d124b1fbcec68f8e940d0f2f0f9a97961abd211e
web-flow Mar 6, 2026
527432c
golf
lrvideckis Mar 6, 2026
1a8e762
[auto-verifier] verify commit 527432c4ebeb1677cec47cd3f48093faf8e2468f
web-flow Mar 6, 2026
f708280
add aizu test now
lrvideckis Mar 6, 2026
00d952d
[auto-verifier] verify commit f708280dbd511d9a8a2e2463ef8c169189b304cb
web-flow Mar 6, 2026
250ce7d
add docs now
lrvideckis Mar 6, 2026
82effb2
Merge branch 'dsu_potential' of github.com:programming-team-code/prog…
lrvideckis Mar 6, 2026
ef81d50
[auto-verifier] verify commit 82effb2f7831ba1225b7cb47309d882c130757c7
web-flow Mar 6, 2026
73cafbd
do it this way actually
lrvideckis Mar 6, 2026
bc88881
[auto-verifier] verify commit 73cafbd5d8f2b138ecf4de8991c62be104489a8d
web-flow Mar 6, 2026
035c64c
fix bug in test actually
lrvideckis Mar 6, 2026
a990dd1
[auto-verifier] verify commit 035c64c5371721761185c8e4aa793e71cf03968f
web-flow Mar 6, 2026
b60cf61
fix+golf
lrvideckis Mar 6, 2026
a6babce
[auto-verifier] verify commit b60cf61f9787657aa996a1635cd56df62aaf221c
web-flow Mar 6, 2026
d5626d6
more golf
lrvideckis Mar 6, 2026
1955492
fix
lrvideckis Mar 6, 2026
709fa47
another fix
lrvideckis Mar 6, 2026
525fcf3
another fix
lrvideckis Mar 6, 2026
45d173f
[auto-verifier] verify commit 525fcf322eaa1f68c9008aaa615f4789e1618d22
web-flow Mar 6, 2026
bcd04c6
one last optimization
lrvideckis Mar 6, 2026
92091d5
[auto-verifier] verify commit bcd04c66291424461f117a8458845a9227860d74
web-flow Mar 6, 2026
3c21db9
one last fix
lrvideckis Mar 6, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .verify-helper/timestamps.remote.json
Original file line number Diff line number Diff line change
Expand Up @@ -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:51:34 -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",
Expand Down
24 changes: 11 additions & 13 deletions library/dsu/dsu_bipartite.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,21 @@ 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) {
is_bi[u] &= new_parity;
int x = f(u), y = f(v);
int new_parity = parity[u] ^ parity[v];
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;
}
int size(int v) { return -p[f(v)]; }
bool is_bipartite(int v) { return is_bi[f(v)]; }
};
31 changes: 31 additions & 0 deletions library/dsu/dsu_weighted.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#pragma once
//! @code
//! dsu_weighted dsu(n);
//! dsu.join(u, v, w);
//! // 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)
struct dsu_weighted {
vi p;
vector<ll> d;
dsu_weighted(int n): p(n, -1), d(n) {}
int f(int u) {
if (p[u] < 0) return u;
int root = f(p[u]);
return d[u] += d[p[u]], 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) {
int x = f(u), y = f(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;
}
};
1 change: 1 addition & 0 deletions tests/.config/.cppcheck_suppression_list
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 31 additions & 0 deletions tests/library_checker_aizu_tests/dsu/dsu_weighted_aizu.test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#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) {
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 {
ll curr_diff = dsu_w.diff(u, v);
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));
}
}
return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#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) {
int w;
cin >> 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));
assert(dsu.size(v) == dsu_w.size(v));
} else {
ll curr_diff = dsu_w.diff(u, v);
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));
}
}
return 0;
}
2 changes: 1 addition & 1 deletion tests/scripts/compile_commented_snippets.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ git submodule update
echo "vi rhs;"
echo "vector<vi> mat;"
echo "vector<vector<bool>> 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

{
Expand Down
Loading