結果

問題 No.812 Change of Class
ユーザー phsplsphspls
提出日時 2023-01-30 23:27:01
言語 Rust
(1.77.0 + proconio)
結果
AC  
実行時間 185 ms / 4,000 ms
コード長 3,465 bytes
コンパイル時間 15,186 ms
コンパイル使用メモリ 383,068 KB
実行使用メモリ 13,952 KB
最終ジャッジ日時 2024-06-30 07:28:26
合計ジャッジ時間 21,921 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 55 ms
11,008 KB
testcase_01 AC 9 ms
6,816 KB
testcase_02 AC 30 ms
7,040 KB
testcase_03 AC 64 ms
12,544 KB
testcase_04 AC 56 ms
11,328 KB
testcase_05 AC 179 ms
11,788 KB
testcase_06 AC 147 ms
9,728 KB
testcase_07 AC 4 ms
6,940 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 179 ms
13,032 KB
testcase_10 AC 185 ms
13,032 KB
testcase_11 AC 19 ms
9,860 KB
testcase_12 AC 120 ms
12,460 KB
testcase_13 AC 2 ms
6,944 KB
testcase_14 AC 1 ms
6,940 KB
testcase_15 AC 106 ms
9,344 KB
testcase_16 AC 7 ms
6,940 KB
testcase_17 AC 98 ms
9,784 KB
testcase_18 AC 72 ms
7,848 KB
testcase_19 AC 85 ms
8,736 KB
testcase_20 AC 171 ms
12,676 KB
testcase_21 AC 67 ms
7,500 KB
testcase_22 AC 31 ms
6,940 KB
testcase_23 AC 6 ms
6,944 KB
testcase_24 AC 9 ms
6,940 KB
testcase_25 AC 25 ms
6,944 KB
testcase_26 AC 135 ms
11,084 KB
testcase_27 AC 116 ms
10,020 KB
testcase_28 AC 82 ms
8,676 KB
testcase_29 AC 20 ms
6,940 KB
testcase_30 AC 107 ms
9,704 KB
testcase_31 AC 91 ms
8,448 KB
testcase_32 AC 40 ms
6,940 KB
testcase_33 AC 111 ms
10,552 KB
testcase_34 AC 7 ms
6,944 KB
testcase_35 AC 20 ms
6,940 KB
testcase_36 AC 8 ms
6,944 KB
testcase_37 AC 49 ms
6,940 KB
testcase_38 AC 7 ms
6,944 KB
testcase_39 AC 51 ms
6,944 KB
testcase_40 AC 13 ms
6,940 KB
testcase_41 AC 5 ms
6,944 KB
testcase_42 AC 109 ms
9,480 KB
testcase_43 AC 26 ms
8,064 KB
testcase_44 AC 74 ms
7,424 KB
testcase_45 AC 8 ms
6,940 KB
testcase_46 AC 22 ms
7,860 KB
testcase_47 AC 70 ms
7,504 KB
testcase_48 AC 83 ms
8,832 KB
testcase_49 AC 19 ms
6,940 KB
testcase_50 AC 2 ms
6,940 KB
testcase_51 AC 23 ms
6,944 KB
testcase_52 AC 46 ms
8,264 KB
testcase_53 AC 15 ms
6,944 KB
testcase_54 AC 11 ms
6,940 KB
testcase_55 AC 43 ms
10,496 KB
testcase_56 AC 66 ms
11,904 KB
testcase_57 AC 68 ms
12,544 KB
testcase_58 AC 28 ms
7,680 KB
testcase_59 AC 79 ms
13,952 KB
testcase_60 AC 1 ms
6,944 KB
testcase_61 AC 1 ms
6,944 KB
testcase_62 AC 1 ms
6,940 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: methods `addval` and `getval` are never used
  --> src/main.rs:51:8
   |
13 | impl UnionFind {
   | -------------- methods in this implementation
...
51 |     fn addval(&mut self, idx: usize, val: isize) {
   |        ^^^^^^
...
56 |     fn getval(&self, idx: usize) -> isize {
   |        ^^^^^^

ソースコード

diff #

use std::collections::VecDeque;


const INF: usize = 1usize << 60;

struct UnionFind {
    n: usize,
    parents: Vec<usize>,
    size: Vec<usize>,
    vals: Vec<isize>,
}

impl UnionFind {
    fn new(n: usize) -> Self {
        UnionFind {
            n: n,
            parents: (0..n).collect(),
            size: vec![1usize; n],
            vals: vec![0isize; 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 (a, b) = (a.min(b), a.max(b));
        let x = self.find(a);
        let y = self.find(b);
        if self.size[x] <= self.size[y] {
            self.size[y] += self.size[x];
            self.size[x] = 0;
            self.vals[x] -= self.vals[y];
            self.parents[x] = self.parents[y];
        } else {
            self.size[x] += self.size[y];
            self.size[y] = 0;
            self.vals[y] -= self.vals[x];
            self.parents[y] = self.parents[x];
        }
    }

    fn find(&mut self, a: usize) -> usize {
        if self.parents[a] == a { return a; }
        let p = self.find(self.parents[a]);
        p
    }

    fn addval(&mut self, idx: usize, val: isize) {
        let idx = self.find(idx);
        self.vals[idx] += val;
    }

    fn getval(&self, idx: usize) -> isize {
        let mut ret = self.vals[idx];
        let mut idx = idx;
        while self.parents[idx] != idx {
            idx = self.parents[idx];
            ret += self.vals[idx];
        }
        ret
    }
}

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

    for &v in queries.iter() {
        let mut dists = vec![INF; n];
        dists[v] = 0;
        let mut deque = VecDeque::new();
        deque.push_back(v);
        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);
            }
        }
        println!("{} {}", size[uf.find(v)]-1, (*dists.iter().filter(|&&v| v < INF).max().unwrap() as f64).log2().ceil() as usize);
    }
}
0