結果
問題 |
No.1767 BLUE to RED
|
ユーザー |
|
提出日時 | 2022-10-22 12:00:51 |
言語 | Rust (1.83.0 + proconio) |
結果 |
AC
|
実行時間 | 257 ms / 2,000 ms |
コード長 | 2,676 bytes |
コンパイル時間 | 14,216 ms |
コンパイル使用メモリ | 380,120 KB |
実行使用メモリ | 46,348 KB |
最終ジャッジ日時 | 2024-07-01 14:47:39 |
合計ジャッジ時間 | 20,048 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 21 |
コンパイルメッセージ
warning: unused variable: `m` --> src/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` --> src/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 --> src/main.rs:4:5 | 3 | struct UnionFind { | --------- field in this struct 4 | n: usize, | ^ | = note: `#[warn(dead_code)]` on by default warning: method `chain_cnt` is never used --> src/main.rs:47:8 | 10 | impl UnionFind { | -------------- method in this implementation ... 47 | 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 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); }