結果
問題 | No.1917 LCMST |
ユーザー | hiromi_ayase |
提出日時 | 2022-04-30 06:00:11 |
言語 | Rust (1.77.0 + proconio) |
結果 |
MLE
|
実行時間 | - |
コード長 | 4,952 bytes |
コンパイル時間 | 13,809 ms |
コンパイル使用メモリ | 378,852 KB |
実行使用メモリ | 816,396 KB |
最終ジャッジ日時 | 2024-06-29 12:05:44 |
合計ジャッジ時間 | 128,292 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
5,248 KB |
testcase_01 | AC | 1 ms
5,376 KB |
testcase_02 | AC | 1 ms
5,376 KB |
testcase_03 | AC | 4 ms
5,376 KB |
testcase_04 | AC | 4 ms
5,376 KB |
testcase_05 | AC | 5 ms
5,376 KB |
testcase_06 | AC | 4 ms
5,376 KB |
testcase_07 | AC | 4 ms
5,376 KB |
testcase_08 | AC | 1 ms
5,376 KB |
testcase_09 | MLE | - |
testcase_10 | MLE | - |
testcase_11 | MLE | - |
testcase_12 | MLE | - |
testcase_13 | MLE | - |
testcase_14 | MLE | - |
testcase_15 | MLE | - |
testcase_16 | MLE | - |
testcase_17 | MLE | - |
testcase_18 | MLE | - |
testcase_19 | MLE | - |
testcase_20 | MLE | - |
testcase_21 | MLE | - |
testcase_22 | MLE | - |
testcase_23 | MLE | - |
testcase_24 | AC | 2,977 ms
509,328 KB |
testcase_25 | MLE | - |
testcase_26 | MLE | - |
testcase_27 | MLE | - |
testcase_28 | MLE | - |
testcase_29 | AC | 3,136 ms
495,652 KB |
testcase_30 | MLE | - |
testcase_31 | AC | 3,162 ms
495,760 KB |
testcase_32 | AC | 3,218 ms
499,724 KB |
testcase_33 | MLE | - |
testcase_34 | MLE | - |
testcase_35 | AC | 1,306 ms
172,684 KB |
testcase_36 | MLE | - |
testcase_37 | MLE | - |
testcase_38 | MLE | - |
testcase_39 | MLE | - |
testcase_40 | AC | 3,220 ms
504,080 KB |
testcase_41 | MLE | - |
testcase_42 | AC | 3,206 ms
504,204 KB |
testcase_43 | MLE | - |
testcase_44 | -- | - |
ソースコード
use std::{ collections::{HashMap}, }; #[allow(clippy::many_single_char_names)] fn main() { let n = getline().parse::<usize>().unwrap(); let mut a = getline() .split(' ') .map(|x| x.parse::<i64>().unwrap()) .collect::<Vec<_>>(); a.sort(); let mut div_map: HashMap<i64, _> = HashMap::new(); let mut edges: Vec<(usize, usize, i64)> = Vec::new(); for i in 0..n { let divs = divisors(a[i]); for d in divs { div_map.entry(d).or_insert_with(Vec::new).push(i); } } for e in &mut div_map { let d = *e.0; let u = e.1[0]; for v in e.1 { if u == *v { continue; } edges.push((u, *v, a[u] / d * a[*v])); } } edges.sort_by_key(|e| e.2); let mut ds = Dsu::new(n); let mut ans = 0; for e in edges { if ds.same(e.0, e.1) { continue; } ds.merge(e.0, e.1); ans += e.2; } println!("{}", ans); } fn divisors(x: i64) -> Vec<i64> { let mut ret = vec![]; let mut i = 1; while i * i <= x { if x % i == 0 { ret.push(i); if i * i != x { ret.push(x / i); } } i += 1; } ret } fn getline() -> String { let mut buf = String::new(); std::io::stdin().read_line(&mut buf).unwrap(); buf.trim().to_string() } pub struct Dsu { n: usize, // root node: -1 * component size // otherwise: parent parent_or_size: Vec<i32>, } impl Dsu { /// Creates a new `Dsu`. /// /// # Constraints /// /// - $0 \leq n \leq 10^8$ /// /// # Complexity /// /// - $O(n)$ pub fn new(size: usize) -> Self { Self { n: size, parent_or_size: vec![-1; size], } } // `\textsc` does not work in KaTeX /// Performs the Uɴɪᴏɴ operation. /// /// # Constraints /// /// - $0 \leq a < n$ /// - $0 \leq b < n$ /// /// # Panics /// /// Panics if the above constraints are not satisfied. /// /// # Complexity /// /// - $O(\alpha(n))$ amortized pub fn merge(&mut self, a: usize, b: usize) -> usize { assert!(a < self.n); assert!(b < self.n); let (mut x, mut y) = (self.leader(a), self.leader(b)); if x == y { return x; } if -self.parent_or_size[x] < -self.parent_or_size[y] { std::mem::swap(&mut x, &mut y); } self.parent_or_size[x] += self.parent_or_size[y]; self.parent_or_size[y] = x as i32; x } /// Returns whether the vertices $a$ and $b$ are in the same connected component. /// /// # Constraints /// /// - $0 \leq a < n$ /// - $0 \leq b < n$ /// /// # Panics /// /// Panics if the above constraint is not satisfied. /// /// # Complexity /// /// - $O(\alpha(n))$ amortized pub fn same(&mut self, a: usize, b: usize) -> bool { assert!(a < self.n); assert!(b < self.n); self.leader(a) == self.leader(b) } /// Performs the Fɪɴᴅ operation. /// /// # Constraints /// /// - $0 \leq a < n$ /// /// # Panics /// /// Panics if the above constraint is not satisfied. /// /// # Complexity /// /// - $O(\alpha(n))$ amortized pub fn leader(&mut self, a: usize) -> usize { assert!(a < self.n); if self.parent_or_size[a] < 0 { return a; } self.parent_or_size[a] = self.leader(self.parent_or_size[a] as usize) as i32; self.parent_or_size[a] as usize } /// Returns the size of the connected component that contains the vertex $a$. /// /// # Constraints /// /// - $0 \leq a < n$ /// /// # Panics /// /// Panics if the above constraint is not satisfied. /// /// # Complexity /// /// - $O(\alpha(n))$ amortized pub fn size(&mut self, a: usize) -> usize { assert!(a < self.n); let x = self.leader(a); -self.parent_or_size[x] as usize } /// Divides the graph into connected components. /// /// The result may not be ordered. /// /// # Complexity /// /// - $O(n)$ pub fn groups(&mut self) -> Vec<Vec<usize>> { let mut leader_buf = vec![0; self.n]; let mut group_size = vec![0; self.n]; for i in 0..self.n { leader_buf[i] = self.leader(i); group_size[leader_buf[i]] += 1; } let mut result = vec![Vec::new(); self.n]; for i in 0..self.n { result[i].reserve(group_size[i]); } for i in 0..self.n { result[leader_buf[i]].push(i); } result .into_iter() .filter(|x| !x.is_empty()) .collect::<Vec<Vec<usize>>>() } }