結果
問題 | No.1059 素敵な集合 |
ユーザー | kaki |
提出日時 | 2020-05-23 10:00:50 |
言語 | Rust (1.77.0 + proconio) |
結果 |
AC
|
実行時間 | 236 ms / 2,000 ms |
コード長 | 3,799 bytes |
コンパイル時間 | 14,100 ms |
コンパイル使用メモリ | 396,208 KB |
実行使用メモリ | 82,256 KB |
最終ジャッジ日時 | 2024-07-23 09:37:48 |
合計ジャッジ時間 | 15,182 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
6,816 KB |
testcase_01 | AC | 230 ms
82,256 KB |
testcase_02 | AC | 24 ms
8,192 KB |
testcase_03 | AC | 1 ms
6,940 KB |
testcase_04 | AC | 1 ms
6,940 KB |
testcase_05 | AC | 1 ms
6,944 KB |
testcase_06 | AC | 19 ms
6,944 KB |
testcase_07 | AC | 19 ms
6,940 KB |
testcase_08 | AC | 28 ms
8,832 KB |
testcase_09 | AC | 6 ms
6,940 KB |
testcase_10 | AC | 49 ms
16,728 KB |
testcase_11 | AC | 22 ms
9,108 KB |
testcase_12 | AC | 10 ms
6,944 KB |
testcase_13 | AC | 48 ms
16,184 KB |
testcase_14 | AC | 1 ms
6,944 KB |
testcase_15 | AC | 97 ms
28,164 KB |
testcase_16 | AC | 24 ms
8,064 KB |
testcase_17 | AC | 23 ms
7,552 KB |
testcase_18 | AC | 6 ms
6,944 KB |
testcase_19 | AC | 236 ms
77,916 KB |
testcase_20 | AC | 231 ms
71,728 KB |
testcase_21 | AC | 89 ms
26,816 KB |
ソースコード
#![allow(non_snake_case)] #![allow(dead_code)] #![allow(unused_macros)] #![allow(unused_imports)] use std::str::FromStr; use std::io::*; use std::collections::*; use std::cmp::*; struct Scanner<I: Iterator<Item = char>> { iter: std::iter::Peekable<I>, } macro_rules! exit { () => {{ exit!(0) }}; ($code:expr) => {{ if cfg!(local) { writeln!(std::io::stderr(), "===== Terminated =====") .expect("failed printing to stderr"); } std::process::exit($code); }} } impl<I: Iterator<Item = char>> Scanner<I> { pub fn new(iter: I) -> Scanner<I> { Scanner { iter: iter.peekable(), } } pub fn safe_get_token(&mut self) -> Option<String> { let token = self.iter .by_ref() .skip_while(|c| c.is_whitespace()) .take_while(|c| !c.is_whitespace()) .collect::<String>(); if token.is_empty() { None } else { Some(token) } } pub fn token(&mut self) -> String { self.safe_get_token().unwrap_or_else(|| exit!()) } pub fn get<T: FromStr>(&mut self) -> T { self.token().parse::<T>().unwrap_or_else(|_| exit!()) } pub fn vec<T: FromStr>(&mut self, len: usize) -> Vec<T> { (0..len).map(|_| self.get()).collect() } pub fn mat<T: FromStr>(&mut self, row: usize, col: usize) -> Vec<Vec<T>> { (0..row).map(|_| self.vec(col)).collect() } pub fn char(&mut self) -> char { self.iter.next().unwrap_or_else(|| exit!()) } pub fn chars(&mut self) -> Vec<char> { self.get::<String>().chars().collect() } pub fn mat_chars(&mut self, row: usize) -> Vec<Vec<char>> { (0..row).map(|_| self.chars()).collect() } pub fn line(&mut self) -> String { if self.peek().is_some() { self.iter .by_ref() .take_while(|&c| !(c == '\n' || c == '\r')) .collect::<String>() } else { exit!(); } } pub fn peek(&mut self) -> Option<&char> { self.iter.peek() } } use std::mem::swap; struct UnionFind { parents: Vec<i64>, } impl UnionFind { fn new(n: usize) -> UnionFind { UnionFind { parents: vec![-1; n] } } fn unite(&mut self, x: usize, y: usize) { let mut px = self.root(x); let mut py = self.root(y); if px == py { return; } if -self.parents[x] < -self.parents[y] { swap(&mut px, &mut py); } self.parents[px] += self.parents[py]; self.parents[py] = px as i64; } fn root(&mut self, x: usize) -> usize { let px = self.parents[x]; if px < 0 { return x; } self.parents[x] = self.root(px as usize) as i64; self.parents[x] as usize } fn same(&mut self, x: usize, y: usize) -> bool { self.root(x) == self.root(y) } fn size(&mut self, x: usize) -> usize { let px = self.root(x); -self.parents[px as usize] as usize } } fn main() { let cin = stdin(); let cin = cin.lock(); let mut sc = Scanner::new(cin.bytes().map(|c| c.unwrap() as char)); let L: usize = sc.get(); let R: usize = sc.get(); let mut edges = Vec::new(); for i in L..=R { for j in (i*2..=R).step_by(i) { edges.push((j%i, i, j)); } if i < R { edges.push(((i+1)%i, i, i+1)); } } edges.sort(); let mut uf = UnionFind::new(R+1); let mut ans = 0; for &(cost, i, j) in &edges { if !uf.same(i, j) { ans += cost; uf.unite(i, j); } } println!("{}", ans); }