NEPLg2 Standard Library - kpdsu
Web Playground
Web Playground

kpdsu: 競技プログラミング向け Union-Find(Disjoint Set Union)

目的もくてき:

実装じっそう:

注意ちゅうい:

TESTstdionormalize_newlines
#entry main
#target std
#import "kp/kpdsu" as *
#import "std/stdio" as *
fn main <()*> ()> ():
    let d <i32> dsu_new 5;
    dsu_unite d 0 1;
    dsu_unite d 1 2;
    print_i32 if dsu_same d 0 2 then 1 else 0;
    print " ";
    print_i32 if dsu_same d 0 3 then 1 else 0;
    print " ";
    println_i32 dsu_size d 0;
    dsu_free d

dsu_new: n 頂点の DSU を作る
dsu_free: DSU の内部領域を解放する
dsu_find: 要素 x の代表元を返す
dsu_same: ab が同じ連結成分か判定する
dsu_unite: ab の成分を併合する(既に同じなら何もしない)
dsu_size: x が属する成分サイズを返す