結果
| 問題 | 
                            No.1639 最小通信路
                             | 
                    
| コンテスト | |
| ユーザー | 
                             | 
                    
| 提出日時 | 2022-10-24 00:52:49 | 
| 言語 | Rust  (1.83.0 + proconio)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 3 ms / 2,000 ms | 
| コード長 | 1,936 bytes | 
| コンパイル時間 | 12,519 ms | 
| コンパイル使用メモリ | 400,452 KB | 
| 実行使用メモリ | 5,376 KB | 
| 最終ジャッジ日時 | 2024-07-02 12:42:59 | 
| 合計ジャッジ時間 | 14,502 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge3 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 2 | 
| other | AC * 43 | 
コンパイルメッセージ
warning: field `n` is never read
 --> src/main.rs:3:5
  |
2 | struct UnionFind {
  |        --------- field in this struct
3 |     n: usize,
  |     ^
  |
  = note: `#[warn(dead_code)]` on by default
warning: method `chain_cnt` is never used
  --> src/main.rs:46:8
   |
9  | impl UnionFind {
   | -------------- method in this implementation
...
46 |     fn chain_cnt(&mut self, i: usize) -> usize {
   |        ^^^^^^^^^
            
            ソースコード
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 n = String::new();
    std::io::stdin().read_line(&mut n).ok();
    let n: usize = n.trim().parse().unwrap();
    let mut uf = UnionFind::new(n);
    let mut cnt = 0usize;
    for _ in 0..n*(n-1)/2 {
        let mut temp = String::new();
        std::io::stdin().read_line(&mut temp).ok();
        let temp: Vec<&str> = temp.trim().split_whitespace().collect();
        let a = temp[0].parse::<usize>().unwrap() - 1;
        let b = temp[1].parse::<usize>().unwrap() - 1;
        let c = temp[2];
        if uf.find(a) == uf.find(b) { continue; }
        uf.unite(a, b);
        cnt += 1;
        if cnt == n-1 {
            println!("{}", c);
            return;
        }
    }
}