Skip to content

Commit

Permalink
rewrite connected() with findroot()
Browse files Browse the repository at this point in the history
  • Loading branch information
azizkayumov committed Nov 20, 2023
1 parent 9ca54f6 commit 846d273
Showing 1 changed file with 28 additions and 28 deletions.
56 changes: 28 additions & 28 deletions src/lctree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,60 +161,58 @@ impl<P: Path> LinkCutTree<P> {
/// assert!(lctree.connected(alice, bob)); // now connected
/// ```
pub fn connected(&mut self, v: usize, w: usize) -> bool {
self.reroot(v); // v is now the root of the tree
self.access(w);
// if access(w) messed with the root of the tree, then v and w are connected:
self.forest.parent_of(v).is_some() || v == w
v == w || self.findroot(v) == self.findroot(w)
}

/// Checks if two nodes are connected by a link
/// (i.e. v is the parent of w or vice versa).
/// Merges two trees into a single tree.
///
/// # Examples
/// ```
/// use lctree::LinkCutTree;
///
/// let mut lctree = LinkCutTree::default();
/// let alice = lctree.make_tree(0.0);
/// let bob = lctree.make_tree(0.0);
/// let clay = lctree.make_tree(0.0);
/// let bob = lctree.make_tree(1.0);
/// let clay = lctree.make_tree(2.0);
///
/// lctree.link(alice, bob);
/// lctree.link(bob, clay);
///
/// assert!(lctree.linked(alice, bob)); // alice and bob are connected by a link
/// assert!(!lctree.linked(alice, clay)); // alice and clay are not connected by a link
/// assert!(lctree.connected(alice, clay));
/// ```
pub fn linked(&mut self, v: usize, w: usize) -> bool {
if self.connected(v, w) {
self.forest.left_of(w) == Some(v) && self.forest.right_of(v).is_none()
} else {
false
pub fn link(&mut self, v: usize, w: usize) -> bool {
self.reroot(v);
self.access(w);
// if access(w) messed with the root of the tree, then v and w are connected:
if self.forest.parent_of(v).is_some() || v == w {
return false;
}
// v is the root of its represented tree:
self.forest.set_left(v, w);
true
}

/// Merges two trees into a single tree.
/// Checks if two nodes are connected by a link
/// (i.e. v is the parent of w or vice versa).
///
/// # Examples
/// ```
/// use lctree::LinkCutTree;
///
/// let mut lctree = LinkCutTree::default();
/// let alice = lctree.make_tree(0.0);
/// let bob = lctree.make_tree(1.0);
/// let clay = lctree.make_tree(2.0);
/// let bob = lctree.make_tree(0.0);
/// let clay = lctree.make_tree(0.0);
///
/// lctree.link(alice, bob);
/// lctree.link(bob, clay);
/// assert!(lctree.connected(alice, clay));
///
/// assert!(lctree.linked(alice, bob)); // alice and bob are connected by a link
/// assert!(!lctree.linked(alice, clay)); // alice and clay are not connected by a link
/// ```
pub fn link(&mut self, v: usize, w: usize) -> bool {
if self.connected(v, w) {
return false;
}
// v is the root of its represented tree:
self.forest.set_left(v, w);
true
pub fn linked(&mut self, v: usize, w: usize) -> bool {
self.reroot(v);
self.access(w);
self.forest.left_of(w) == Some(v) && self.forest.right_of(v).is_none()
}

/// Cuts the link between two nodes (if it exists)
Expand Down Expand Up @@ -265,7 +263,9 @@ impl<P: Path> LinkCutTree<P> {
/// assert_eq!(richest_guy.weight, 10.0);
/// ```
pub fn path(&mut self, v: usize, w: usize) -> P {
if !self.connected(v, w) {
self.reroot(v);
self.access(w);
if self.forest.parent_of(v).is_none() && v != w {
return P::default(f64::INFINITY, usize::MAX);
}
self.forest.aggregated_path_of(w)
Expand Down

0 comments on commit 846d273

Please sign in to comment.