結果

問題 No.748 yuki国のお財布事情
ユーザー phsplsphspls
提出日時 2022-12-23 02:14:41
言語 Rust
(1.77.0)
結果
AC  
実行時間 53 ms / 2,000 ms
コード長 2,643 bytes
コンパイル時間 2,511 ms
コンパイル使用メモリ 171,016 KB
実行使用メモリ 9,812 KB
最終ジャッジ日時 2023-08-11 11:43:11
合計ジャッジ時間 6,324 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 0 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,384 KB
testcase_04 AC 1 ms
4,384 KB
testcase_05 AC 1 ms
4,384 KB
testcase_06 AC 1 ms
4,384 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,384 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,384 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 6 ms
4,384 KB
testcase_14 AC 10 ms
4,380 KB
testcase_15 AC 7 ms
4,380 KB
testcase_16 AC 18 ms
4,384 KB
testcase_17 AC 38 ms
7,912 KB
testcase_18 AC 48 ms
7,960 KB
testcase_19 AC 53 ms
8,260 KB
testcase_20 AC 45 ms
7,412 KB
testcase_21 AC 46 ms
8,912 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 0 ms
4,380 KB
testcase_24 AC 1 ms
4,384 KB
testcase_25 AC 34 ms
7,596 KB
testcase_26 AC 46 ms
9,812 KB
testcase_27 AC 44 ms
9,140 KB
testcase_28 AC 35 ms
5,944 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::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 nmk = String::new();
    std::io::stdin().read_line(&mut nmk).ok();
    let nmk: Vec<usize> = nmk.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();
    let n = nmk[0];
    let m = nmk[1];
    let k = nmk[2];
    let lines = (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, temp[2])
        })
        .collect::<Vec<_>>();
    let uses = (0..k).map(|_| {
            let mut temp = String::new();
            std::io::stdin().read_line(&mut temp).ok();
            let temp: usize = temp.trim().parse().unwrap();
            temp - 1
        })
        .collect::<HashSet<_>>();

    let total = lines.iter().map(|&(_, _, c)| c).sum::<usize>();
    let mut uf = UnionFind::new(n);
    let mut result = 0usize;
    for &i in uses.iter() {
        let (a, b, c) = lines[i];
        uf.unite(a, b);
        result += c;
    }
    let mut lines = (0..m).filter(|&i| !uses.contains(&i)).map(|i| lines[i]).collect::<Vec<_>>();
    lines.sort_by_key(|&(_, _, w)| w);
    for &(a, b, c) in lines.iter() {
        if uf.find(a) == uf.find(b) { continue; }
        uf.unite(a, b);
        result += c;
    }
    println!("{}", total - result);
}
0