結果

問題 No.556 仁義なきサルたち
ユーザー phsplsphspls
提出日時 2022-11-26 00:59:02
言語 Rust
(1.72.1)
結果
AC  
実行時間 17 ms / 2,000 ms
コード長 2,640 bytes
コンパイル時間 3,557 ms
コンパイル使用メモリ 141,940 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-07-25 12:53:14
合計ジャッジ時間 5,900 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

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

warning: associated function `chain_cnt` is never used
  --> Main.rs:46:8
   |
46 |     fn chain_cnt(&mut self, i: usize) -> usize {
   |        ^^^^^^^^^

warning: 2 warnings emitted

ソースコード

diff #

struct UnionFind {
    n: usize,
    parents: Vec<usize>,
    depths: Vec<usize>,
    chains: Vec<usize>,
}

impl UnionFind {
    fn new(n: usize) -> Self {
        UnionFind {
            n: n,
            parents: (0..n).collect(),
            depths: vec![0; n],
            chains: vec![1; n],
        }
    }

    fn equiv(&mut self, a: usize, b: usize) -> bool {
        self.find(a) == self.find(b)
    }
    
    fn unite(&mut self, a: usize, b: usize) {
        if self.equiv(a, b) { return; }
        let x = self.parents[a];
        let y = self.parents[b];
        if self.depths[x] > self.depths[y] {
            self.parents[x] = self.parents[y];
            self.chains[y] += self.chains[x];
        } else {
            self.parents[y] = self.parents[x];
            self.chains[x] += self.chains[y];
            if self.depths[x] == self.depths[y] {
                self.depths[x] += 1;
            }
        }
    }

    fn find(&mut self, a: usize) -> usize {
        if self.parents[a] == a { return a; }
        let p = self.find(self.parents[a]);
        self.parents[a] = p;
        p
    }

    fn chain_cnt(&mut self, i: usize) -> usize {
        let idx = self.find(i);
        self.chains[idx]
    }
}

fn main() {
    let mut nm = String::new();
    std::io::stdin().read_line(&mut nm).ok();
    let nm: Vec<usize> = nm.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();
    let n = nm[0];
    let m = nm[1];
    let queries = (0..m).map(|_| {
            let mut temp = String::new();
            std::io::stdin().read_line(&mut temp).ok();
            let temp: Vec<usize> = temp.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();
            (temp[0]-1, temp[1]-1)
        })
        .collect::<Vec<_>>();

    let mut uf = UnionFind::new(n);
    let mut size = vec![1usize; n];
    let mut boss = (0..n).collect::<Vec<_>>();
    for &(a, b) in queries.iter() {
        let pa = uf.find(a);
        let pb = uf.find(b);
        if pa == pb { continue; }
        uf.unite(pa, pb);
        let p = uf.find(pa);
        let winner = if size[pa] > size[pb] {
                boss[pa]
            } else if size[pa] < size[pb] {
                boss[pb]
            } else if boss[pa] < boss[pb] {
                boss[pa]
            } else {
                boss[pb]
            };
        if p == pa {
            size[pa] += size[pb];
            size[pb] = 0;
        } else {
            size[pb] += size[pa];
            size[pa] = 0;
        }
        boss[pa] = winner;
        boss[pb] = winner;
    }
    for i in 0..n {
        println!("{}", boss[uf.find(i)] + 1);
    }
}
0