結果
問題 | No.1767 BLUE to RED |
ユーザー | phspls |
提出日時 | 2022-10-22 12:00:51 |
言語 | Rust (1.77.0) |
結果 |
AC
|
実行時間 | 251 ms / 2,000 ms |
コード長 | 2,676 bytes |
コンパイル時間 | 3,543 ms |
コンパイル使用メモリ | 173,444 KB |
実行使用メモリ | 46,176 KB |
最終ジャッジ日時 | 2023-09-14 07:21:24 |
合計ジャッジ時間 | 8,907 ms |
ジャッジサーバーID (参考情報) |
judge11 / judge14 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
4,380 KB |
testcase_01 | AC | 1 ms
4,376 KB |
testcase_02 | AC | 1 ms
4,380 KB |
testcase_03 | AC | 1 ms
4,380 KB |
testcase_04 | AC | 1 ms
4,380 KB |
testcase_05 | AC | 1 ms
4,380 KB |
testcase_06 | AC | 1 ms
4,376 KB |
testcase_07 | AC | 2 ms
4,376 KB |
testcase_08 | AC | 2 ms
4,376 KB |
testcase_09 | AC | 124 ms
26,256 KB |
testcase_10 | AC | 162 ms
36,668 KB |
testcase_11 | AC | 142 ms
32,396 KB |
testcase_12 | AC | 118 ms
26,216 KB |
testcase_13 | AC | 182 ms
39,516 KB |
testcase_14 | AC | 244 ms
46,144 KB |
testcase_15 | AC | 237 ms
46,148 KB |
testcase_16 | AC | 239 ms
46,152 KB |
testcase_17 | AC | 244 ms
46,160 KB |
testcase_18 | AC | 237 ms
46,108 KB |
testcase_19 | AC | 248 ms
46,176 KB |
testcase_20 | AC | 239 ms
46,076 KB |
testcase_21 | AC | 249 ms
46,140 KB |
testcase_22 | AC | 251 ms
46,064 KB |
testcase_23 | AC | 241 ms
46,152 KB |
コンパイルメッセージ
warning: unused variable: `m` --> Main.rs:58:9 | 58 | let m = nm[1]; | ^ help: if this is intentional, prefix it with an underscore: `_m` | = note: `#[warn(unused_variables)]` on by default warning: unused variable: `b` --> Main.rs:71:9 | 71 | let b = b.into_iter().map(|v| *mapping.get(&v).unwrap()).collect::<Vec<_>>(); | ^ help: if this is intentional, prefix it with an underscore: `_b` warning: field `n` is never read --> Main.rs:4:5 | 3 | struct UnionFind { | --------- field in this struct 4 | n: usize, | ^ | = note: `#[warn(dead_code)]` on by default warning: associated function `chain_cnt` is never used --> Main.rs:47:8 | 47 | fn chain_cnt(&mut self, i: usize) -> usize { | ^^^^^^^^^ warning: 4 warnings emitted
ソースコード
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 pos = a.iter().copied().collect::<BTreeSet<usize>>(); for &v in b.iter() { pos.insert(v); } let mapping = pos.iter().enumerate().map(|(i, &v)| (v, i)).collect::<HashMap<_, _>>(); let revmap = pos.iter().copied().collect::<Vec<_>>(); let a = a.into_iter().map(|v| *mapping.get(&v).unwrap()).collect::<Vec<_>>(); let b = b.into_iter().map(|v| *mapping.get(&v).unwrap()).collect::<Vec<_>>(); let mut uf = UnionFind::new(pos.len()); for i in 0..n-1 { uf.unite(a[i], a[i+1]); } let mut lines = (0..pos.len()-1).map(|i| (i, revmap[i+1]-revmap[i])).collect::<Vec<_>>(); lines.sort_by_key(|&(_, v)| v); let mut result = 0usize; for &(i, cost) in lines.iter() { if uf.find(i) == uf.find(i+1) { continue; } uf.unite(i, i+1); result += cost; } println!("{}", result); }