結果

問題 No.1293 2種類の道路
ユーザー phsplsphspls
提出日時 2022-11-30 23:45:20
言語 Rust
(1.77.0)
結果
TLE  
実行時間 -
コード長 2,816 bytes
コンパイル時間 3,055 ms
コンパイル使用メモリ 184,924 KB
実行使用メモリ 20,780 KB
最終ジャッジ日時 2024-04-16 18:53:56
合計ジャッジ時間 7,007 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
13,756 KB
testcase_01 AC 1 ms
6,816 KB
testcase_02 AC 1 ms
6,812 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 1 ms
6,944 KB
testcase_05 AC 0 ms
6,940 KB
testcase_06 AC 1 ms
6,944 KB
testcase_07 AC 1 ms
6,940 KB
testcase_08 AC 1 ms
6,940 KB
testcase_09 TLE -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused variable: `croot`
  --> main.rs:87:11
   |
87 |     for (&croot, cchildren) in cmapping.iter() {
   |           ^^^^^ help: if this is intentional, prefix it with an underscore: `_croot`
   |
   = note: `#[warn(unused_variables)]` on by default

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: 3 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 cmapping = HashMap::new();
    let mut wmapping = HashMap::new();
    for i in 0..n {
        cmapping.entry(cuf.find(i)).or_insert(vec![]).push(i);
        wmapping.entry(wuf.find(i)).or_insert(vec![]).push(i);
    }
    let mut result = 0usize;
    for (&croot, cchildren) in cmapping.iter() {
        let mut base = cchildren.iter().copied().collect::<HashSet<usize>>();
        cchildren.iter().map(|&v| wuf.find(v)).collect::<HashSet<usize>>().iter().flat_map(|&v| wmapping.get(&v).unwrap()).for_each(|&v| {
            base.insert(v);
        });
        result += cchildren.len() * base.len();
    }
    println!("{}", result - n);
}
0