結果

問題 No.812 Change of Class
ユーザー phsplsphspls
提出日時 2023-01-04 00:23:12
言語 Rust
(1.77.0)
結果
AC  
実行時間 151 ms / 4,000 ms
コード長 3,211 bytes
コンパイル時間 11,787 ms
コンパイル使用メモリ 377,232 KB
実行使用メモリ 15,232 KB
最終ジャッジ日時 2024-05-05 07:59:55
合計ジャッジ時間 18,088 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 48 ms
11,688 KB
testcase_01 AC 8 ms
5,248 KB
testcase_02 AC 26 ms
7,424 KB
testcase_03 AC 58 ms
13,480 KB
testcase_04 AC 50 ms
12,080 KB
testcase_05 AC 146 ms
11,784 KB
testcase_06 AC 132 ms
9,728 KB
testcase_07 AC 3 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 151 ms
13,160 KB
testcase_10 AC 146 ms
13,036 KB
testcase_11 AC 15 ms
9,980 KB
testcase_12 AC 94 ms
12,460 KB
testcase_13 AC 1 ms
5,376 KB
testcase_14 AC 1 ms
5,376 KB
testcase_15 AC 94 ms
9,232 KB
testcase_16 AC 6 ms
5,376 KB
testcase_17 AC 84 ms
9,912 KB
testcase_18 AC 63 ms
7,976 KB
testcase_19 AC 74 ms
8,772 KB
testcase_20 AC 149 ms
12,676 KB
testcase_21 AC 61 ms
7,496 KB
testcase_22 AC 27 ms
5,376 KB
testcase_23 AC 5 ms
5,376 KB
testcase_24 AC 7 ms
5,376 KB
testcase_25 AC 22 ms
5,376 KB
testcase_26 AC 117 ms
11,008 KB
testcase_27 AC 103 ms
10,016 KB
testcase_28 AC 71 ms
8,704 KB
testcase_29 AC 17 ms
5,376 KB
testcase_30 AC 91 ms
9,728 KB
testcase_31 AC 76 ms
8,576 KB
testcase_32 AC 35 ms
5,556 KB
testcase_33 AC 97 ms
10,552 KB
testcase_34 AC 7 ms
5,376 KB
testcase_35 AC 18 ms
5,376 KB
testcase_36 AC 6 ms
5,376 KB
testcase_37 AC 43 ms
6,088 KB
testcase_38 AC 6 ms
5,500 KB
testcase_39 AC 43 ms
6,084 KB
testcase_40 AC 11 ms
5,376 KB
testcase_41 AC 5 ms
5,376 KB
testcase_42 AC 91 ms
9,608 KB
testcase_43 AC 21 ms
8,064 KB
testcase_44 AC 62 ms
7,424 KB
testcase_45 AC 6 ms
5,376 KB
testcase_46 AC 20 ms
7,936 KB
testcase_47 AC 63 ms
7,500 KB
testcase_48 AC 74 ms
8,904 KB
testcase_49 AC 17 ms
5,376 KB
testcase_50 AC 1 ms
5,376 KB
testcase_51 AC 21 ms
5,376 KB
testcase_52 AC 44 ms
8,320 KB
testcase_53 AC 12 ms
5,376 KB
testcase_54 AC 9 ms
5,376 KB
testcase_55 AC 38 ms
11,080 KB
testcase_56 AC 57 ms
12,812 KB
testcase_57 AC 59 ms
13,440 KB
testcase_58 AC 25 ms
8,176 KB
testcase_59 AC 71 ms
15,232 KB
testcase_60 AC 1 ms
5,376 KB
testcase_61 AC 1 ms
5,376 KB
testcase_62 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: field `n` is never read
 --> src/main.rs:7:5
  |
6 | struct UnionFind {
  |        --------- field in this struct
7 |     n: usize,
  |     ^
  |
  = note: `#[warn(dead_code)]` on by default

warning: method `chain_cnt` is never used
  --> src/main.rs:50:8
   |
13 | impl UnionFind {
   | -------------- method in this implementation
...
50 |     fn chain_cnt(&mut self, i: usize) -> usize {
   |        ^^^^^^^^^

ソースコード

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