pub struct Dsu { /* private fields */ }Expand description
A Disjoint set union (DSU) with union by size and path compression.
See: Zvi Galil and Giuseppe F. Italiano, Data structures and algorithms for disjoint set union problems
In the following documentation, let $n$ be the size of the DSU.
§Example
use ac_library::Dsu;
use proconio::{input, source::once::OnceSource};
input! {
from OnceSource::from(
"5\n\
3\n\
0 1\n\
2 3\n\
3 4\n",
),
n: usize,
abs: [(usize, usize)],
}
let mut dsu = Dsu::new(n);
for (a, b) in abs {
dsu.merge(a, b);
}
assert!(dsu.same(0, 1));
assert!(!dsu.same(1, 2));
assert!(dsu.same(2, 4));
assert_eq!(
dsu.groups()
.into_iter()
.map(|mut group| {
group.sort_unstable();
group
})
.collect::<Vec<_>>(),
[&[0, 1][..], &[2, 3, 4][..]],
);Implementations§
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Dsu
impl RefUnwindSafe for Dsu
impl Send for Dsu
impl Sync for Dsu
impl Unpin for Dsu
impl UnwindSafe for Dsu
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more