結果
| 問題 | No.1767 BLUE to RED | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2022-10-15 16:33:14 | 
| 言語 | Rust (1.83.0 + proconio) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 705 ms / 2,000 ms | 
| コード長 | 3,264 bytes | 
| コンパイル時間 | 13,465 ms | 
| コンパイル使用メモリ | 395,904 KB | 
| 実行使用メモリ | 53,152 KB | 
| 最終ジャッジ日時 | 2024-06-26 19:58:30 | 
| 合計ジャッジ時間 | 23,550 ms | 
| ジャッジサーバーID (参考情報) | judge4 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 21 | 
コンパイルメッセージ
warning: field `n` is never read
 --> src/main.rs:4:5
  |
3 | struct UnionFind {
  |        --------- field in this struct
4 |     n: usize,
  |     ^
  |
  = note: `#[warn(dead_code)]` on by default
warning: method `chain_cnt` is never used
  --> src/main.rs:47:8
   |
10 | impl UnionFind {
   | -------------- method in this implementation
...
47 |     fn chain_cnt(&mut self, i: usize) -> usize {
   |        ^^^^^^^^^
            
            ソースコード
use std::{collections::{BTreeSet, HashMap, BinaryHeap}, cmp::Reverse};
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: BTreeSet<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 uf = UnionFind::new(n+m+1);
    let mut revmap = Vec::with_capacity(n+m);
    for &v in a.iter() { revmap.push(v); }
    for &v in b.iter() { revmap.push(v); }
    revmap.sort();
    let mapping = (0..n+m).map(|i| (revmap[i], i)).collect::<HashMap<usize, usize>>();
    let a = a.into_iter().map(|v| *mapping.get(&v).unwrap()).collect::<BTreeSet<_>>();
    let b = b.into_iter().map(|v| *mapping.get(&v).unwrap()).collect::<Vec<_>>();
    for &v in a.iter() {
        uf.unite(v, n+m);
    }
    let mut pque = BinaryHeap::new();
    for i in 0..m {
        let v = revmap[b[i]];
        let idx = b[i];
        if let Some(&left) = a.range(..idx).rev().nth(0) {
            let nidx = left;
            pque.push((Reverse(v-revmap[nidx]), idx, nidx));
        }
        if let Some(&right) = a.range(idx+1..).nth(0) {
            let nidx = right;
            pque.push((Reverse(revmap[nidx]-v), idx, nidx));
        }
        if i > 0 {
            let nidx = b[i-1];
            pque.push((Reverse(v-revmap[nidx]), idx, nidx));
        }
        if i+1 < m {
            let nidx = b[i+1];
            pque.push((Reverse(revmap[nidx]-v), idx, nidx));
        }
    }
    let mut result = 0usize;
    while let Some((Reverse(cost), l, r)) = pque.pop() {
        if uf.find(l) == uf.find(r) { continue; }
        uf.unite(l, r);
        result += cost;
    }
    println!("{}", result);
}
            
            
            
        