結果
問題 | No.1293 2種類の道路 |
ユーザー | phspls |
提出日時 | 2022-12-07 12:02:32 |
言語 | Rust (1.77.0 + proconio) |
結果 |
AC
|
実行時間 | 71 ms / 2,000 ms |
コード長 | 3,036 bytes |
コンパイル時間 | 13,608 ms |
コンパイル使用メモリ | 378,672 KB |
実行使用メモリ | 13,568 KB |
最終ジャッジ日時 | 2024-10-13 19:16:36 |
合計ジャッジ時間 | 16,881 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
5,248 KB |
testcase_01 | AC | 1 ms
5,248 KB |
testcase_02 | AC | 1 ms
5,248 KB |
testcase_03 | AC | 1 ms
5,248 KB |
testcase_04 | AC | 1 ms
5,248 KB |
testcase_05 | AC | 1 ms
5,248 KB |
testcase_06 | AC | 1 ms
5,248 KB |
testcase_07 | AC | 1 ms
5,248 KB |
testcase_08 | AC | 1 ms
5,248 KB |
testcase_09 | AC | 71 ms
12,288 KB |
testcase_10 | AC | 66 ms
12,288 KB |
testcase_11 | AC | 68 ms
12,288 KB |
testcase_12 | AC | 68 ms
12,288 KB |
testcase_13 | AC | 66 ms
12,288 KB |
testcase_14 | AC | 40 ms
11,392 KB |
testcase_15 | AC | 38 ms
11,520 KB |
testcase_16 | AC | 63 ms
13,184 KB |
testcase_17 | AC | 62 ms
12,672 KB |
testcase_18 | AC | 45 ms
13,184 KB |
testcase_19 | AC | 48 ms
13,568 KB |
testcase_20 | AC | 47 ms
13,568 KB |
testcase_21 | AC | 31 ms
5,248 KB |
testcase_22 | AC | 31 ms
5,248 KB |
testcase_23 | AC | 30 ms
5,248 KB |
コンパイルメッセージ
warning: unused imports: `HashMap`, `result` --> src/main.rs:1:11 | 1 | use std::{result, collections::{HashMap, HashSet}}; | ^^^^^^ ^^^^^^^ | = note: `#[warn(unused_imports)]` on by default warning: field `n` is never read --> src/main.rs:5:5 | 4 | struct UnionFind { | --------- field in this struct 5 | n: usize, | ^ | = note: `#[warn(dead_code)]` on by default warning: method `chain_cnt` is never used --> src/main.rs:48:8 | 11 | impl UnionFind { | -------------- method in this implementation ... 48 | fn chain_cnt(&mut self, i: usize) -> usize { | ^^^^^^^^^
ソースコード
use std::{result, collections::{HashMap, HashSet}}; 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 ndw = String::new(); std::io::stdin().read_line(&mut ndw).ok(); let ndw: Vec<usize> = ndw.trim().split_whitespace().map(|s| s.parse().unwrap()).collect(); let n = ndw[0]; let d = ndw[1]; let w = ndw[2]; let mut cuf = UnionFind::new(n); let mut wuf = UnionFind::new(n); let mut wsize = vec![1usize; n]; let merge = |a: usize, b: usize, uf: &mut UnionFind, size: &mut Vec<usize>| { let pa = uf.find(a); let pb = uf.find(b); if pa == pb { return; } uf.unite(pa, pb); let p = uf.find(pa); if p == pa { size[pa] += size[pb]; size[pb] = 0; } else { size[pb] += size[pa]; size[pa] = 0; } }; for _ in 0..d { 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]-1; let b = temp[1]-1; cuf.unite(a, b); } for _ in 0..w { 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]-1; let b = temp[1]-1; merge(a, b, &mut wuf, &mut wsize); } let mut mapping = vec![vec![]; n]; for i in 0..n { mapping[cuf.find(i)].push(i); } let mut result = 0usize; for i in 0..n { if i != cuf.find(i) { continue; } result += mapping[i].len() * mapping[i].iter().map(|&j| wuf.find(j)).collect::<HashSet<usize>>().iter().map(|&j| wsize[j]).sum::<usize>(); } println!("{}", result - n); }