結果
問題 | No.1767 BLUE to RED |
ユーザー | phspls |
提出日時 | 2022-10-29 19:18:17 |
言語 | Rust (1.77.0 + proconio) |
結果 |
AC
|
実行時間 | 167 ms / 2,000 ms |
コード長 | 2,805 bytes |
コンパイル時間 | 12,445 ms |
コンパイル使用メモリ | 401,952 KB |
実行使用メモリ | 48,108 KB |
最終ジャッジ日時 | 2024-07-06 16:26:59 |
合計ジャッジ時間 | 17,266 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
6,816 KB |
testcase_01 | AC | 1 ms
6,812 KB |
testcase_02 | AC | 1 ms
6,812 KB |
testcase_03 | AC | 1 ms
6,816 KB |
testcase_04 | AC | 1 ms
6,940 KB |
testcase_05 | AC | 1 ms
6,944 KB |
testcase_06 | AC | 0 ms
6,940 KB |
testcase_07 | AC | 1 ms
6,940 KB |
testcase_08 | AC | 1 ms
6,944 KB |
testcase_09 | AC | 87 ms
27,064 KB |
testcase_10 | AC | 108 ms
36,616 KB |
testcase_11 | AC | 105 ms
34,440 KB |
testcase_12 | AC | 84 ms
26,844 KB |
testcase_13 | AC | 114 ms
38,796 KB |
testcase_14 | AC | 167 ms
48,108 KB |
testcase_15 | AC | 165 ms
48,072 KB |
testcase_16 | AC | 162 ms
48,108 KB |
testcase_17 | AC | 161 ms
47,980 KB |
testcase_18 | AC | 163 ms
47,980 KB |
testcase_19 | AC | 165 ms
47,976 KB |
testcase_20 | AC | 164 ms
48,108 KB |
testcase_21 | AC | 163 ms
47,976 KB |
testcase_22 | AC | 160 ms
48,104 KB |
testcase_23 | AC | 165 ms
47,984 KB |
コンパイルメッセージ
warning: unused import: `BTreeSet` --> src/main.rs:1:24 | 1 | use std::collections::{BTreeSet, HashMap}; | ^^^^^^^^ | = 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::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 vals = a.clone().to_owned(); for &v in b.iter() { vals.push(v); } vals.sort(); let mapping = (0..n+m).map(|i| (vals[i], i)).collect::<HashMap<usize, usize>>(); let revmap = vals; let a = a.iter().map(|&v| *mapping.get(&v).unwrap()).collect::<Vec<usize>>(); let b = b.iter().map(|&v| *mapping.get(&v).unwrap()).collect::<Vec<usize>>(); let mut uf = UnionFind::new(n+m); for i in 0..n-1 { uf.unite(a[i], a[i+1]); } let mut lines = b.iter().flat_map(|&i| { let mut ret = vec![]; if i > 0 { ret.push((i-1, i, revmap[i]-revmap[i-1])); } if i+1 < n+m { ret.push((i, i+1, revmap[i+1]-revmap[i])); } ret }) .collect::<Vec<_>>(); lines.sort_by_key(|&(_, _, v)| v); let mut result = 0usize; for &(u, v, cost) in lines.iter() { if uf.find(u) == uf.find(v) { continue; } uf.unite(u, v); result += cost; } println!("{}", result); }