結果
問題 | No.812 Change of Class |
ユーザー | phspls |
提出日時 | 2023-01-04 00:23:12 |
言語 | Rust (1.77.0 + proconio) |
結果 |
AC
|
実行時間 | 155 ms / 4,000 ms |
コード長 | 3,211 bytes |
コンパイル時間 | 26,726 ms |
コンパイル使用メモリ | 378,492 KB |
実行使用メモリ | 15,232 KB |
最終ジャッジ日時 | 2024-11-27 02:57:03 |
合計ジャッジ時間 | 31,269 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 51 ms
11,688 KB |
testcase_01 | AC | 8 ms
5,248 KB |
testcase_02 | AC | 28 ms
7,296 KB |
testcase_03 | AC | 59 ms
13,356 KB |
testcase_04 | AC | 52 ms
11,936 KB |
testcase_05 | AC | 155 ms
11,788 KB |
testcase_06 | AC | 133 ms
9,728 KB |
testcase_07 | AC | 3 ms
5,248 KB |
testcase_08 | AC | 2 ms
5,248 KB |
testcase_09 | AC | 146 ms
13,032 KB |
testcase_10 | AC | 153 ms
13,028 KB |
testcase_11 | AC | 15 ms
9,856 KB |
testcase_12 | AC | 100 ms
12,588 KB |
testcase_13 | AC | 1 ms
5,248 KB |
testcase_14 | AC | 1 ms
5,248 KB |
testcase_15 | AC | 96 ms
9,344 KB |
testcase_16 | AC | 7 ms
5,248 KB |
testcase_17 | AC | 89 ms
9,856 KB |
testcase_18 | AC | 68 ms
7,848 KB |
testcase_19 | AC | 80 ms
8,896 KB |
testcase_20 | AC | 155 ms
12,684 KB |
testcase_21 | AC | 62 ms
7,504 KB |
testcase_22 | AC | 28 ms
5,248 KB |
testcase_23 | AC | 6 ms
5,248 KB |
testcase_24 | AC | 8 ms
5,248 KB |
testcase_25 | AC | 23 ms
5,248 KB |
testcase_26 | AC | 126 ms
11,080 KB |
testcase_27 | AC | 108 ms
10,024 KB |
testcase_28 | AC | 71 ms
8,704 KB |
testcase_29 | AC | 17 ms
5,248 KB |
testcase_30 | AC | 103 ms
9,704 KB |
testcase_31 | AC | 82 ms
8,448 KB |
testcase_32 | AC | 38 ms
5,692 KB |
testcase_33 | AC | 104 ms
10,624 KB |
testcase_34 | AC | 7 ms
5,248 KB |
testcase_35 | AC | 19 ms
5,248 KB |
testcase_36 | AC | 7 ms
5,248 KB |
testcase_37 | AC | 45 ms
6,088 KB |
testcase_38 | AC | 6 ms
5,500 KB |
testcase_39 | AC | 46 ms
5,960 KB |
testcase_40 | AC | 12 ms
5,248 KB |
testcase_41 | AC | 5 ms
5,248 KB |
testcase_42 | AC | 96 ms
9,612 KB |
testcase_43 | AC | 23 ms
8,040 KB |
testcase_44 | AC | 67 ms
7,424 KB |
testcase_45 | AC | 7 ms
5,248 KB |
testcase_46 | AC | 21 ms
7,864 KB |
testcase_47 | AC | 64 ms
7,500 KB |
testcase_48 | AC | 74 ms
8,908 KB |
testcase_49 | AC | 19 ms
5,248 KB |
testcase_50 | AC | 1 ms
5,248 KB |
testcase_51 | AC | 21 ms
5,248 KB |
testcase_52 | AC | 42 ms
8,448 KB |
testcase_53 | AC | 12 ms
5,248 KB |
testcase_54 | AC | 10 ms
5,248 KB |
testcase_55 | AC | 39 ms
11,076 KB |
testcase_56 | AC | 58 ms
12,684 KB |
testcase_57 | AC | 62 ms
13,440 KB |
testcase_58 | AC | 25 ms
8,048 KB |
testcase_59 | AC | 71 ms
15,232 KB |
testcase_60 | AC | 1 ms
5,248 KB |
testcase_61 | AC | 1 ms
5,248 KB |
testcase_62 | AC | 0 ms
5,248 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 { | ^^^^^^^^^
ソースコード
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); } }