You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/dsu.rs
+121-5
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,49 @@
1
-
/// Implement (union by size) + (path compression)
2
-
/// Reference:
3
-
/// Zvi Galil and Giuseppe F. Italiano,
4
-
/// Data structures and algorithms for disjoint set union problems
1
+
//! A Disjoint set union (DSU) with union by size and path compression.
2
+
3
+
/// A Disjoint set union (DSU) with union by size and path compressione.
4
+
///
5
+
/// See: [Zvi Galil and Giuseppe F. Italiano, Data structures and algorithms for disjoint set union problems](https://core.ac.uk/download/pdf/161439519.pdf)
6
+
///
7
+
/// In the following documentation, let $n$ be size of the DSU.
8
+
///
9
+
/// # Example
10
+
///
11
+
/// ```
12
+
/// use ac_library_rs::Dsu;
13
+
/// use proconio::{input, source::once::OnceSource};
14
+
///
15
+
/// input! {
16
+
/// from OnceSource::from(
17
+
/// "5\n\
18
+
/// 3\n\
19
+
/// 0 1\n\
20
+
/// 2 3\n\
21
+
/// 3 4\n",
22
+
/// ),
23
+
/// n: usize,
24
+
/// abs: [(usize, usize)],
25
+
/// }
26
+
///
27
+
/// let mut dsu = Dsu::new(n);
28
+
/// for (a, b) in abs {
29
+
/// dsu.merge(a, b);
30
+
/// }
31
+
///
32
+
/// assert!(dsu.same(0, 1));
33
+
/// assert!(!dsu.same(1, 2));
34
+
/// assert!(dsu.same(2, 4));
35
+
///
36
+
/// assert_eq!(
37
+
/// dsu.groups()
38
+
/// .into_iter()
39
+
/// .map(|mut group| {
40
+
/// group.sort_unstable();
41
+
/// group
42
+
/// })
43
+
/// .collect::<Vec<_>>(),
44
+
/// [&[0, 1][..], &[2, 3, 4][..]],
45
+
/// );
46
+
/// ```
5
47
pubstructDsu{
6
48
n:usize,
7
49
// root node: -1 * component size
@@ -10,13 +52,37 @@ pub struct Dsu {
10
52
}
11
53
12
54
implDsu{
13
-
// 0 <= size <= 10^8 is constrained.
55
+
/// Creates a new `Dsu`.
56
+
///
57
+
/// # Constraints
58
+
///
59
+
/// - $0 \leq n \leq 10^8$
60
+
///
61
+
/// # Complexity
62
+
///
63
+
/// - $O(n)$
14
64
pubfnnew(size:usize) -> Self{
15
65
Self{
16
66
n: size,
17
67
parent_or_size:vec![-1; size],
18
68
}
19
69
}
70
+
71
+
// `\textsc` does not work in KaTeX
72
+
/// Performs the `UNION` operation.
73
+
///
74
+
/// # Constraints
75
+
///
76
+
/// - $0 \leq a < n$
77
+
/// - $0 \leq b < n$
78
+
///
79
+
/// # Panics
80
+
///
81
+
/// Panics if the above constraints are not satisfied.
82
+
///
83
+
/// # Complexity
84
+
///
85
+
/// - $O(\alpha(n))$ amortized
20
86
pubfnmerge(&mutself,a:usize,b:usize) -> usize{
21
87
assert!(a < self.n);
22
88
assert!(b < self.n);
@@ -32,11 +98,39 @@ impl Dsu {
32
98
x
33
99
}
34
100
101
+
/// Returns whether the vertices $a$ and $b$ are in the same connected component.
102
+
///
103
+
/// # Constraints
104
+
///
105
+
/// - $0 \leq a < n$
106
+
/// - $0 \leq b < n$
107
+
///
108
+
/// # Panics
109
+
///
110
+
/// Panics if the above constraint is not satisfied.
111
+
///
112
+
/// # Complexity
113
+
///
114
+
/// - $O(\alpha(n))$ amortized
35
115
pubfnsame(&mutself,a:usize,b:usize) -> bool{
36
116
assert!(a < self.n);
37
117
assert!(b < self.n);
38
118
self.leader(a) == self.leader(b)
39
119
}
120
+
121
+
/// Performs the `FIND` operation.
122
+
///
123
+
/// # Constraints
124
+
///
125
+
/// - $0 \leq a < n$
126
+
///
127
+
/// # Panics
128
+
///
129
+
/// Panics if the above constraint is not satisfied.
0 commit comments