結果

問題 No.1293 2種類の道路
ユーザー phsplsphspls
提出日時 2022-11-25 01:09:41
言語 Rust
(1.72.1)
結果
AC  
実行時間 79 ms / 2,000 ms
コード長 2,684 bytes
コンパイル時間 2,481 ms
コンパイル使用メモリ 154,636 KB
実行使用メモリ 21,848 KB
最終ジャッジ日時 2023-07-24 17:56:52
合計ジャッジ時間 6,200 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 77 ms
11,188 KB
testcase_10 AC 75 ms
11,208 KB
testcase_11 AC 75 ms
11,208 KB
testcase_12 AC 79 ms
11,184 KB
testcase_13 AC 78 ms
11,112 KB
testcase_14 AC 53 ms
11,356 KB
testcase_15 AC 52 ms
11,412 KB
testcase_16 AC 79 ms
15,084 KB
testcase_17 AC 77 ms
14,800 KB
testcase_18 AC 63 ms
21,848 KB
testcase_19 AC 71 ms
21,064 KB
testcase_20 AC 66 ms
21,096 KB
testcase_21 AC 35 ms
4,380 KB
testcase_22 AC 35 ms
4,376 KB
testcase_23 AC 35 ms
4,380 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: associated function `chain_cnt` is never used
  --> Main.rs:48:8
   |
48 |     fn chain_cnt(&mut self, i: usize) -> usize {
   |        ^^^^^^^^^

warning: 2 warnings emitted

ソースコード

diff #

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


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 ndw = String::new();
    std::io::stdin().read_line(&mut ndw).ok();
    let ndw: Vec<usize> = ndw.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();
    let n = ndw[0];
    let d = ndw[1];
    let w = ndw[2];
    let mut cuf = UnionFind::new(n);
    let mut wuf = UnionFind::new(n);
    for _ in 0..d {
        let mut ab = String::new();
        std::io::stdin().read_line(&mut ab).ok();
        let ab: Vec<usize> = ab.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();
        let a = ab[0]-1;
        let b = ab[1]-1;
        cuf.unite(a, b);
    }
    for _ in 0..w {
        let mut ab = String::new();
        std::io::stdin().read_line(&mut ab).ok();
        let ab: Vec<usize> = ab.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();
        let a = ab[0]-1;
        let b = ab[1]-1;
        wuf.unite(a, b);
    }

    let mut wchildren = vec![0usize; n];
    for i in 0..n {
        wchildren[wuf.find(i)] += 1;
    }
    let mut mapping = HashMap::new();
    for i in 0..n {
        mapping.entry(cuf.find(i)).or_insert(HashSet::new()).insert(wuf.find(i));
    }
    let mut children = vec![0usize; n];
    for (&root, wroots) in mapping.iter() {
        children[root] = wroots.iter().map(|&v| wchildren[v]).sum::<usize>();
    }
    let result = (0..n).map(|i| children[cuf.find(i)]).sum::<usize>();
    println!("{}", result-n);
}
0