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
+76-5Lines changed: 76 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,21 @@
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).
2
+
3
+
/// A Disjoint set union (DSU).
4
+
///
5
+
/// Given an undirected graph, it processes the following queries in $O(\alpha(n))$ time (amortized).
6
+
///
7
+
/// - Edge addition
8
+
/// - Deciding whether given two vertices are in the same connected component
9
+
///
10
+
/// Each connected component internally has a representative vertex.
11
+
///
12
+
/// When two connected components are merged by edge addition, one of the two representatives of these connected components becomes the representative of the new connected component.
13
+
///
14
+
/// In the following documentation, let $n$ be size of the DSU.
15
+
// Implement (union by size) + (path compression)
16
+
// Reference:
17
+
// Zvi Galil and Giuseppe F. Italiano,
18
+
// Data structures and algorithms for disjoint set union problems
5
19
pubstructDsu{
6
20
n:usize,
7
21
// root node: -1 * component size
@@ -10,13 +24,32 @@ pub struct Dsu {
10
24
}
11
25
12
26
implDsu{
13
-
// 0 <= size <= 10^8 is constrained.
27
+
/// Creates a new `Dsu`.
28
+
///
29
+
/// # Constraints
30
+
///
31
+
/// - $0 \leq n \leq 10^8$
32
+
///
33
+
/// # Complexity
34
+
///
35
+
/// - $O(n)$
14
36
pubfnnew(size:usize) -> Self{
15
37
Self{
16
38
n: size,
17
39
parent_or_size:vec![-1; size],
18
40
}
19
41
}
42
+
43
+
/// Adds an edge $(a, b)$.
44
+
///
45
+
/// # Constraints
46
+
///
47
+
/// - $0 \leq a < n$
48
+
/// - $0 \leq b < n$
49
+
///
50
+
/// # Complexity
51
+
///
52
+
/// - $O(\alpha(n))$ amortized
20
53
pubfnmerge(&mutself,a:usize,b:usize) -> usize{
21
54
assert!(a < self.n);
22
55
assert!(b < self.n);
@@ -32,11 +65,31 @@ impl Dsu {
32
65
x
33
66
}
34
67
68
+
/// Returns whether the vertices $a$ and $b$ are in the same connected component.
69
+
///
70
+
/// # Constraints
71
+
///
72
+
/// - $0 \leq a < n$
73
+
/// - $0 \leq b < n$
74
+
///
75
+
/// # Complexity
76
+
///
77
+
/// - $O(\alpha(n))$ amortized
35
78
pubfnsame(&mutself,a:usize,b:usize) -> bool{
36
79
assert!(a < self.n);
37
80
assert!(b < self.n);
38
81
self.leader(a) == self.leader(b)
39
82
}
83
+
84
+
/// Returns the representative of the connected component that contains the vertex $a$.
0 commit comments