結果

問題 No.1390 Get together
ユーザー phsplsphspls
提出日時 2022-11-21 00:20:57
言語 Rust
(1.77.0)
結果
AC  
実行時間 153 ms / 2,000 ms
コード長 2,288 bytes
コンパイル時間 2,863 ms
コンパイル使用メモリ 195,432 KB
実行使用メモリ 28,016 KB
最終ジャッジ日時 2023-10-21 19:12:58
合計ジャッジ時間 6,856 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 0 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 3 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 3 ms
4,348 KB
testcase_06 AC 3 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 3 ms
4,348 KB
testcase_09 AC 3 ms
4,348 KB
testcase_10 AC 1 ms
4,348 KB
testcase_11 AC 1 ms
4,348 KB
testcase_12 AC 1 ms
4,348 KB
testcase_13 AC 1 ms
4,348 KB
testcase_14 AC 1 ms
4,348 KB
testcase_15 AC 1 ms
4,348 KB
testcase_16 AC 97 ms
22,272 KB
testcase_17 AC 79 ms
15,928 KB
testcase_18 AC 116 ms
25,688 KB
testcase_19 AC 141 ms
27,740 KB
testcase_20 AC 99 ms
18,392 KB
testcase_21 AC 99 ms
18,404 KB
testcase_22 AC 132 ms
25,688 KB
testcase_23 AC 126 ms
25,664 KB
testcase_24 AC 132 ms
25,684 KB
testcase_25 AC 146 ms
28,016 KB
testcase_26 AC 143 ms
28,004 KB
testcase_27 AC 142 ms
27,996 KB
testcase_28 AC 151 ms
28,004 KB
testcase_29 AC 153 ms
28,000 KB
testcase_30 AC 142 ms
28,012 KB
testcase_31 AC 145 ms
28,016 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: field `n` is never read
 --> Main.rs:5:5
  |
4 | struct UnionFind {
  |        --------- field in this struct
5 |     n: usize,
  |     ^
  |
  = note: `#[warn(dead_code)]` on by default

warning: method `chain_cnt` is never used
  --> Main.rs:48:8
   |
11 | impl UnionFind {
   | -------------- method in this implementation
...
48 |     fn chain_cnt(&mut self, i: usize) -> usize {
   |        ^^^^^^^^^

warning: 2 warnings emitted

ソースコード

diff #

use std::collections::{HashSet, HashMap};


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 slimes = (0..n).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 color_box = vec![vec![]; m];
    for &(b, c) in slimes.iter() {
        color_box[b].push(c);
    }
    let mut uf = UnionFind::new(n);
    for c in color_box.iter() {
        for &v in c.iter() {
            uf.unite(c[0], v);
        }
    }
    let mut mapping = HashMap::new();
    for &(b, c) in slimes.iter() {
        mapping.entry(uf.find(c)).or_insert(HashSet::new()).insert(b);
    }
    println!("{}", mapping.values().map(|v| v.len()-1).sum::<usize>());
}
0