結果

問題 No.812 Change of Class
ユーザー phsplsphspls
提出日時 2023-01-04 00:23:12
言語 Rust
(1.77.0)
結果
AC  
実行時間 169 ms / 4,000 ms
コード長 3,211 bytes
コンパイル時間 5,019 ms
コンパイル使用メモリ 142,404 KB
実行使用メモリ 15,336 KB
最終ジャッジ日時 2023-08-18 01:13:43
合計ジャッジ時間 10,181 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 55 ms
11,640 KB
testcase_01 AC 9 ms
4,376 KB
testcase_02 AC 30 ms
7,384 KB
testcase_03 AC 64 ms
13,444 KB
testcase_04 AC 56 ms
12,200 KB
testcase_05 AC 167 ms
11,648 KB
testcase_06 AC 142 ms
9,552 KB
testcase_07 AC 4 ms
4,480 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 158 ms
12,788 KB
testcase_10 AC 163 ms
12,852 KB
testcase_11 AC 16 ms
9,584 KB
testcase_12 AC 106 ms
12,468 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 105 ms
9,328 KB
testcase_16 AC 7 ms
4,376 KB
testcase_17 AC 97 ms
9,884 KB
testcase_18 AC 73 ms
7,820 KB
testcase_19 AC 84 ms
8,932 KB
testcase_20 AC 169 ms
12,624 KB
testcase_21 AC 70 ms
7,724 KB
testcase_22 AC 31 ms
4,832 KB
testcase_23 AC 6 ms
4,380 KB
testcase_24 AC 9 ms
4,376 KB
testcase_25 AC 26 ms
4,548 KB
testcase_26 AC 133 ms
11,164 KB
testcase_27 AC 117 ms
10,136 KB
testcase_28 AC 81 ms
8,824 KB
testcase_29 AC 19 ms
4,376 KB
testcase_30 AC 104 ms
9,788 KB
testcase_31 AC 91 ms
8,512 KB
testcase_32 AC 40 ms
5,520 KB
testcase_33 AC 110 ms
10,708 KB
testcase_34 AC 8 ms
4,376 KB
testcase_35 AC 21 ms
4,384 KB
testcase_36 AC 7 ms
4,376 KB
testcase_37 AC 50 ms
5,952 KB
testcase_38 AC 6 ms
5,484 KB
testcase_39 AC 51 ms
6,156 KB
testcase_40 AC 13 ms
4,376 KB
testcase_41 AC 6 ms
5,120 KB
testcase_42 AC 106 ms
9,388 KB
testcase_43 AC 25 ms
7,904 KB
testcase_44 AC 72 ms
7,560 KB
testcase_45 AC 8 ms
4,380 KB
testcase_46 AC 23 ms
7,880 KB
testcase_47 AC 71 ms
7,656 KB
testcase_48 AC 84 ms
8,884 KB
testcase_49 AC 20 ms
4,376 KB
testcase_50 AC 2 ms
4,380 KB
testcase_51 AC 24 ms
4,768 KB
testcase_52 AC 49 ms
8,076 KB
testcase_53 AC 14 ms
4,376 KB
testcase_54 AC 11 ms
4,380 KB
testcase_55 AC 44 ms
11,216 KB
testcase_56 AC 66 ms
12,764 KB
testcase_57 AC 72 ms
13,288 KB
testcase_58 AC 30 ms
8,040 KB
testcase_59 AC 80 ms
15,336 KB
testcase_60 AC 1 ms
4,376 KB
testcase_61 AC 1 ms
4,376 KB
testcase_62 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: field `n` is never read
 --> Main.rs:7:5
  |
6 | struct UnionFind {
  |        --------- field in this struct
7 |     n: usize,
  |     ^
  |
  = note: `#[warn(dead_code)]` on by default

warning: associated function `chain_cnt` is never used
  --> Main.rs:50:8
   |
50 |     fn chain_cnt(&mut self, i: usize) -> usize {
   |        ^^^^^^^^^

warning: 2 warnings emitted

ソースコード

diff #

use std::collections::VecDeque;


const INF: usize = 1usize << 60;

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 paths = vec![vec![]; n];
    let mut uf = UnionFind::new(n);
    let mut size = vec![1usize; n];
    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 u = temp[0]-1;
        let v = temp[1]-1;
        paths[u].push(v);
        paths[v].push(u);
        let pu = uf.find(u);
        let pv = uf.find(v);
        if pu == pv { continue; }
        uf.unite(pu, pv);
        let p = uf.find(pu);
        if p == pu {
            size[pu] += size[pv];
            size[pv] = 0;
        } else {
            size[pv] += size[pu];
            size[pu] = 0;
        }
    }
    let mut q = String::new();
    std::io::stdin().read_line(&mut q).ok();
    let q: usize = q.trim().parse().unwrap();
    let queries = (0..q).map(|_| {
            let mut temp = String::new();
            std::io::stdin().read_line(&mut temp).ok();
            let temp: usize = temp.trim().parse().unwrap();
            temp-1
        })
        .collect::<Vec<_>>();

    for &a in queries.iter() {
        let mut dists = vec![INF; n];
        dists[a] = 0;
        let mut deque = VecDeque::new();
        deque.push_back(a);
        while let Some(u) = deque.pop_front() {
            for &v in paths[u].iter() {
                if dists[v] != INF { continue; }
                dists[v] = dists[u]+1;
                deque.push_back(v);
            }
        }
        let max_dist = *dists.iter().filter(|&&v| v < INF).max().unwrap();
        println!("{} {}", size[uf.find(a)] - 1, (max_dist as f64).log2().ceil() as usize);
    }
}
0