pub struct LowestCommonAncestor { /* private fields */ }
Expand description

根付き木の LCA です。

Examples

use lowest_common_ancestor::LowestCommonAncestor;

// 0 -- 2 -- 4
// |    |
// 1    3

let lca = LowestCommonAncestor::new(5, 0, &[(0, 1), (0, 2), (2, 3), (2, 4)]);
assert_eq!(lca.get(0, 1), 0);
assert_eq!(lca.get(0, 4), 0);
assert_eq!(lca.get(1, 1), 1);
assert_eq!(lca.get(1, 2), 0);
assert_eq!(lca.get(2, 3), 2);
assert_eq!(lca.get(3, 4), 2);

Implementations§

source§

impl LowestCommonAncestor

source

pub fn new(n: usize, root: usize, edges: &[(usize, usize)]) -> Self

頂点数 n, 根 root, 木をなす無向辺の集合 edges を渡します。

source

pub fn get(&self, u: usize, v: usize) -> usize

uv の LCA を返します。

source

pub fn get_dist(&self, u: usize, v: usize) -> usize

uv の距離 (頂点間にある辺の数) を返します。

source

pub fn depth(&self, u: usize) -> usize

頂点 u の深さを返します。

source

pub fn kth_parent(&self, u: usize, k: usize) -> Option<usize>

頂点 u から根の方向に k 本の辺を登って着く頂点を返します。

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.