結果

問題 No.583 鉄道同好会
ユーザー phsplsphspls
提出日時 2022-12-23 02:34:49
言語 Rust
(1.77.0)
結果
AC  
実行時間 42 ms / 2,000 ms
コード長 2,611 bytes
コンパイル時間 14,372 ms
コンパイル使用メモリ 379,028 KB
実行使用メモリ 6,272 KB
最終ジャッジ日時 2024-04-29 04:10:08
合計ジャッジ時間 16,194 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 1 ms
5,376 KB
testcase_11 AC 11 ms
5,376 KB
testcase_12 AC 16 ms
5,376 KB
testcase_13 AC 16 ms
5,376 KB
testcase_14 AC 15 ms
5,376 KB
testcase_15 AC 19 ms
5,376 KB
testcase_16 AC 34 ms
5,888 KB
testcase_17 AC 42 ms
6,272 KB
testcase_18 AC 42 ms
6,144 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused variable: `n`
  --> src/main.rs:57:9
   |
57 |     let n = nm[0];
   |         ^ help: if this is intentional, prefix it with an underscore: `_n`
   |
   = note: `#[warn(unused_variables)]` on by default

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 {
   |        ^^^^^^^^^

ソースコード

diff #

use std::collections::{HashSet, 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 btree = BTreeSet::new();
    let mut lines = Vec::with_capacity(m);
    for _ in 0..m {
        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();
        let a = temp[0];
        let b = temp[1];
        lines.push((a, b));
        btree.insert(a);
        btree.insert(b);
    }

    let mapping = btree.iter().enumerate().map(|(i, &v)| (v, i)).collect::<HashMap<usize, usize>>();
    let mut uf = UnionFind::new(btree.len());
    let mut paths = vec![vec![]; btree.len()];
    for &(l, r) in lines.iter() {
        let l = *mapping.get(&l).unwrap();
        let r = *mapping.get(&r).unwrap();
        paths[l].push(r);
        paths[r].push(l);
        uf.unite(l, r);
    }
    if (0..btree.len()).map(|i| uf.find(i)).collect::<HashSet<usize>>().len() > 1 {
        println!("NO");
        return;
    }
    let cnt = paths.iter().filter(|&v| v.len() % 2 == 1).count();
    if cnt > 2 {
        println!("NO");
    } else {
        println!("YES");
    }
}
0