結果

問題 No.556 仁義なきサルたち
ユーザー lzy9lzy9
提出日時 2018-04-01 14:52:16
言語 Rust
(1.77.0)
結果
AC  
実行時間 17 ms / 2,000 ms
コード長 2,133 bytes
コンパイル時間 1,714 ms
コンパイル使用メモリ 150,712 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-08 11:41:07
合計ジャッジ時間 3,452 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 3 ms
4,380 KB
testcase_11 AC 2 ms
4,384 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 8 ms
4,380 KB
testcase_14 AC 8 ms
4,380 KB
testcase_15 AC 8 ms
4,380 KB
testcase_16 AC 14 ms
4,376 KB
testcase_17 AC 16 ms
4,376 KB
testcase_18 AC 17 ms
4,380 KB
testcase_19 AC 16 ms
4,380 KB
testcase_20 AC 17 ms
4,376 KB
testcase_21 AC 17 ms
4,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: field `rank` is never read
  --> Main.rs:37:5
   |
35 | struct UnionFind {
   |        --------- field in this struct
36 |     par: Vec<usize>,
37 |     rank: Vec<usize>,
   |     ^^^^
   |
   = note: `#[warn(dead_code)]` on by default

warning: associated function `same` is never used
  --> Main.rs:52:8
   |
52 |     fn same(&mut self, a: usize, b: usize) -> bool {
   |        ^^^^

warning: 2 warnings emitted

ソースコード

diff #

use std::io::*;
use std::str::FromStr;
use std::cmp::{min, max};

fn read<T: FromStr>() -> T {
    let stdin = stdin();
    let stdin_lock = stdin.lock();
    let s = stdin_lock
        .bytes()
        .map(|c| c.unwrap() as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect::<String>();
    s.parse::<T>().ok().unwrap()
}

fn main() {
    let n: usize = read();
    let m: usize = read();

    let mut uf = UnionFind::new(n);

    for _ in 0..m {
        let a: usize = read::<usize>() - 1;
        let b: usize = read::<usize>() - 1;

        uf.union(a, b);
    }

    for i in 0..n {
        println!("{}", uf.root(i) + 1);
    }
}

struct UnionFind {
    par: Vec<usize>,
    rank: Vec<usize>,
    groups: usize,
    cnt: Vec<usize>,
}

impl UnionFind {
    fn new(n: usize) -> UnionFind {
        UnionFind {
            par: (0..n).map(|i| i).collect(),
            rank: vec![0; n],
            groups: n,
            cnt: vec![1; n],
        }
    }

    fn same(&mut self, a: usize, b: usize) -> bool {
        self.root(a) == self.root(b)
    }

    fn root(&mut self, a: usize) -> usize {
        if self.par[a] == a {
            a
        } else {
            let par = self.par[a];
            self.par[a] = self.root(par);
            self.par[a]
        }
    }

    fn union(&mut self, a: usize, b: usize) {
        let x = self.root(a);
        let y = self.root(b);

        if x == y {
            return;
        }

        self.groups -= 1;

        if self.cnt[x] < self.cnt[y] {
            self.par[x] = y;
            self.cnt[y] += self.cnt[x];
        } else if self.cnt[x] == self.cnt[y] {
            self.par[max(x, y)] = self.par[min(x, y)];
            self.cnt[min(x, y)] += self.cnt[max(x, y)];
        } else {
            self.par[y] = x;
            self.cnt[x] += self.cnt[y];
        }

//        if self.rank[x] < self.rank[y] {
//            self.par[x] = y;
//        } else {
//            self.par[y] = x;
//            if self.rank[x] == self.rank[y] {
//                self.rank[x] += 1;
//            }
//        }
    }
}
0