Skip to main content

UnionFind

Struct UnionFind 

Source
pub struct UnionFind { /* private fields */ }
Expand description

0..n の要素を互いに素な集合へ分割して管理します。

経路圧縮とサイズによる併合を使用します。各操作の償却計算量は O(α(N)) です。 添字は 0-indexed で、範囲外の添字を渡すと panic します。

§Examples

use atcoder::data_structure::union_find::UnionFind;

let mut dsu = UnionFind::new(4);
assert!(dsu.merge(0, 1));
assert!(dsu.same(0, 1));
assert_eq!(dsu.size(0), 2);
assert_eq!(dsu.group_count(), 3);

Implementations§

Source§

impl UnionFind

Source

pub fn new(n: usize) -> Self

0..n の各要素を含む単一要素集合を作ります。計算量・メモリ量は O(N) です。

Source

pub fn find(&mut self, x: usize) -> usize

x の属する集合の代表元を返します。経路圧縮を行い、償却計算量は O(α(N)) です。 x >= n なら panic します。

Source

pub fn merge(&mut self, x: usize, y: usize) -> bool

x と y の集合を併合します。

新しく併合した場合は true、既に同じ集合なら false を返します。 償却計算量は O(α(N)) です。x >= n または y >= n なら panic します。

Source

pub fn same(&mut self, x: usize, y: usize) -> bool

x と y が同じ集合なら true を返します。代表元の検索を行うため、 償却計算量は O(α(N)) です。x >= n または y >= n なら panic します。

Source

pub fn size(&mut self, x: usize) -> usize

x の属する集合の要素数を返します。代表元の検索を行うため、 償却計算量は O(α(N)) です。x >= n なら panic します。

Source

pub const fn group_count(&self) -> usize

現在の集合数を返します。計算量は O(1) です。

Trait Implementations§

Source§

impl Clone for UnionFind

Source§

fn clone(&self) -> UnionFind

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for UnionFind

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.
Source§

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

Source§

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

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.