結果

問題 No.1767 BLUE to RED
ユーザー phsplsphspls
提出日時 2022-10-16 21:03:25
言語 Rust
(1.77.0)
結果
AC  
実行時間 265 ms / 2,000 ms
コード長 2,699 bytes
コンパイル時間 5,066 ms
コンパイル使用メモリ 170,444 KB
実行使用メモリ 47,356 KB
最終ジャッジ日時 2023-09-09 22:26:29
合計ジャッジ時間 10,787 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 1 ms
4,384 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,384 KB
testcase_06 AC 1 ms
4,384 KB
testcase_07 AC 1 ms
4,384 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 131 ms
26,796 KB
testcase_10 AC 181 ms
38,356 KB
testcase_11 AC 157 ms
32,288 KB
testcase_12 AC 129 ms
26,828 KB
testcase_13 AC 193 ms
41,756 KB
testcase_14 AC 264 ms
47,072 KB
testcase_15 AC 265 ms
47,296 KB
testcase_16 AC 264 ms
47,352 KB
testcase_17 AC 261 ms
47,296 KB
testcase_18 AC 260 ms
47,296 KB
testcase_19 AC 257 ms
47,268 KB
testcase_20 AC 260 ms
47,264 KB
testcase_21 AC 263 ms
47,352 KB
testcase_22 AC 263 ms
47,268 KB
testcase_23 AC 258 ms
47,356 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused variable: `b`
  --> Main.rs:75:9
   |
75 |     let b = b.into_iter().map(|v| *mapping.get(&v).unwrap()).collect::<Vec<usize>>();
   |         ^ help: if this is intentional, prefix it with an underscore: `_b`
   |
   = 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: associated function `chain_cnt` is never used
  --> Main.rs:48:8
   |
48 |     fn chain_cnt(&mut self, i: usize) -> usize {
   |        ^^^^^^^^^

warning: 3 warnings emitted

ソースコード

diff #

use std::collections::{BTreeSet, 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 mut a = String::new();
    std::io::stdin().read_line(&mut a).ok();
    let a: Vec<usize> = a.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();
    let mut b = String::new();
    std::io::stdin().read_line(&mut b).ok();
    let b: Vec<usize> = b.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();

    let mut points = a.iter().copied().collect::<BTreeSet<usize>>();
    for &v in b.iter() { points.insert(v); }
    let points = points.into_iter().collect::<Vec<usize>>();
    let mut mapping = HashMap::new();
    for i in 0..points.len() {
        mapping.insert(points[i], i);
    }
    let a = a.into_iter().map(|v| *mapping.get(&v).unwrap()).collect::<Vec<usize>>();
    let b = b.into_iter().map(|v| *mapping.get(&v).unwrap()).collect::<Vec<usize>>();
    let mut uf = UnionFind::new(n+m+1);
    for &v in a.iter() {
        uf.unite(v, n+m);
    }
    let mut lines = (0..n+m-1).map(|i| (points[i+1] - points[i], i)).collect::<Vec<_>>();
    lines.sort();
    let mut result = 0usize;
    for &(cost, i) in lines.iter() {
        if uf.find(i) == uf.find(i+1) { continue; }
        uf.unite(i, i+1);
        result += cost;
    }
    println!("{}", result);
}
0