From 684ba2b535cc09d9ae06775335d27a228a8c19ac Mon Sep 17 00:00:00 2001 From: NotLeonian <75620009+NotLeonian@users.noreply.github.com> Date: Sat, 3 May 2025 17:21:23 +0900 Subject: [PATCH] fix `Dsu::leader` to avoid multiple assert checks --- src/dsu.rs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/dsu.rs b/src/dsu.rs index 44c327b..31a15bf 100644 --- a/src/dsu.rs +++ b/src/dsu.rs @@ -134,11 +134,7 @@ impl Dsu { /// - $O(\alpha(n))$ amortized pub fn leader(&mut self, a: usize) -> usize { assert!(a < self.n); - if self.parent_or_size[a] < 0 { - return a; - } - self.parent_or_size[a] = self.leader(self.parent_or_size[a] as usize) as i32; - self.parent_or_size[a] as usize + self._leader(a) } /// Returns the size of the connected component that contains the vertex $a$. @@ -186,6 +182,14 @@ impl Dsu { .filter(|x| !x.is_empty()) .collect::>>() } + + fn _leader(&mut self, a: usize) -> usize { + if self.parent_or_size[a] < 0 { + return a; + } + self.parent_or_size[a] = self._leader(self.parent_or_size[a] as usize) as i32; + self.parent_or_size[a] as usize + } } #[cfg(test)]