結果
問題 | No.1767 BLUE to RED |
ユーザー | phspls |
提出日時 | 2022-10-16 21:03:25 |
言語 | Rust (1.77.0 + proconio) |
結果 |
AC
|
実行時間 | 275 ms / 2,000 ms |
コード長 | 2,699 bytes |
コンパイル時間 | 21,401 ms |
コンパイル使用メモリ | 400,236 KB |
実行使用メモリ | 47,444 KB |
最終ジャッジ日時 | 2024-06-27 14:56:44 |
合計ジャッジ時間 | 27,855 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
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,376 KB |
testcase_07 | AC | 2 ms
5,376 KB |
testcase_08 | AC | 2 ms
5,376 KB |
testcase_09 | AC | 134 ms
26,684 KB |
testcase_10 | AC | 184 ms
38,184 KB |
testcase_11 | AC | 161 ms
32,272 KB |
testcase_12 | AC | 129 ms
26,688 KB |
testcase_13 | AC | 202 ms
41,792 KB |
testcase_14 | AC | 254 ms
47,148 KB |
testcase_15 | AC | 262 ms
47,444 KB |
testcase_16 | AC | 250 ms
47,432 KB |
testcase_17 | AC | 261 ms
47,180 KB |
testcase_18 | AC | 269 ms
47,300 KB |
testcase_19 | AC | 271 ms
47,368 KB |
testcase_20 | AC | 275 ms
47,312 KB |
testcase_21 | AC | 258 ms
47,436 KB |
testcase_22 | AC | 256 ms
47,432 KB |
testcase_23 | AC | 255 ms
47,316 KB |
コンパイルメッセージ
warning: unused variable: `b` --> src/main.rs:75:9 | 75 | let b = b.into_iter().map(|v| *mapping.get(&v).unwrap()).collect::<Vec<usize>>(); | ^ help: if this is intentional, prefix it with an underscore: `_b` | = note: `#[warn(unused_variables)]` 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::collections::{BTreeSet, HashMap}; 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 a = String::new(); std::io::stdin().read_line(&mut a).ok(); let a: Vec<usize> = a.trim().split_whitespace().map(|s| s.parse().unwrap()).collect(); let mut b = String::new(); std::io::stdin().read_line(&mut b).ok(); let b: Vec<usize> = b.trim().split_whitespace().map(|s| s.parse().unwrap()).collect(); let mut points = a.iter().copied().collect::<BTreeSet<usize>>(); for &v in b.iter() { points.insert(v); } let points = points.into_iter().collect::<Vec<usize>>(); let mut mapping = HashMap::new(); for i in 0..points.len() { mapping.insert(points[i], i); } let a = a.into_iter().map(|v| *mapping.get(&v).unwrap()).collect::<Vec<usize>>(); let b = b.into_iter().map(|v| *mapping.get(&v).unwrap()).collect::<Vec<usize>>(); let mut uf = UnionFind::new(n+m+1); for &v in a.iter() { uf.unite(v, n+m); } let mut lines = (0..n+m-1).map(|i| (points[i+1] - points[i], i)).collect::<Vec<_>>(); lines.sort(); let mut result = 0usize; for &(cost, i) in lines.iter() { if uf.find(i) == uf.find(i+1) { continue; } uf.unite(i, i+1); result += cost; } println!("{}", result); }