結果
問題 | No.2290 UnUnion Find |
ユーザー | Sarievo |
提出日時 | 2023-05-05 22:56:57 |
言語 | Rust (1.77.0 + proconio) |
結果 |
RE
|
実行時間 | - |
コード長 | 3,695 bytes |
コンパイル時間 | 15,608 ms |
コンパイル使用メモリ | 378,836 KB |
実行使用メモリ | 38,784 KB |
最終ジャッジ日時 | 2024-11-23 11:01:47 |
合計ジャッジ時間 | 37,753 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,248 KB |
testcase_02 | RE | - |
testcase_03 | RE | - |
testcase_04 | AC | 338 ms
22,372 KB |
testcase_05 | AC | 339 ms
22,240 KB |
testcase_06 | AC | 343 ms
22,324 KB |
testcase_07 | AC | 342 ms
22,368 KB |
testcase_08 | AC | 341 ms
22,240 KB |
testcase_09 | AC | 346 ms
22,368 KB |
testcase_10 | AC | 336 ms
22,372 KB |
testcase_11 | AC | 344 ms
22,240 KB |
testcase_12 | AC | 343 ms
22,244 KB |
testcase_13 | AC | 338 ms
22,212 KB |
testcase_14 | AC | 333 ms
22,244 KB |
testcase_15 | AC | 343 ms
22,244 KB |
testcase_16 | AC | 336 ms
22,244 KB |
testcase_17 | AC | 341 ms
22,244 KB |
testcase_18 | AC | 338 ms
22,240 KB |
testcase_19 | WA | - |
testcase_20 | AC | 555 ms
38,784 KB |
testcase_21 | WA | - |
testcase_22 | WA | - |
testcase_23 | WA | - |
testcase_24 | AC | 504 ms
29,696 KB |
testcase_25 | WA | - |
testcase_26 | WA | - |
testcase_27 | AC | 515 ms
31,360 KB |
testcase_28 | WA | - |
testcase_29 | AC | 497 ms
27,648 KB |
testcase_30 | WA | - |
testcase_31 | WA | - |
testcase_32 | WA | - |
testcase_33 | WA | - |
testcase_34 | AC | 558 ms
38,784 KB |
testcase_35 | WA | - |
testcase_36 | WA | - |
testcase_37 | WA | - |
testcase_38 | WA | - |
testcase_39 | WA | - |
testcase_40 | AC | 511 ms
32,128 KB |
testcase_41 | WA | - |
testcase_42 | WA | - |
testcase_43 | WA | - |
testcase_44 | AC | 341 ms
22,240 KB |
testcase_45 | RE | - |
testcase_46 | RE | - |
コンパイルメッセージ
warning: unused imports: `BTreeSet`, `HashMap` --> src/main.rs:37:34 | 37 | use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet}; | ^^^^^^^^ ^^^^^^^ | = note: `#[warn(unused_imports)]` on by default
ソースコード
pub struct DisjointSetUnion { parent: Vec<usize>, sizes: Vec<usize>, size: usize, } impl DisjointSetUnion { pub fn new(n: usize) -> DisjointSetUnion { DisjointSetUnion { parent: (0..n).map(|i| i).collect::<Vec<usize>>(), sizes: vec![1; n], size: n, } } pub fn leader(&mut self, x: usize) -> usize { if x == self.parent[x] { return x; } let px = self.parent[x]; self.parent[x] = self.leader(px); self.parent[x] } pub fn merge(&mut self, x: usize, y: usize) -> bool { let px = self.leader(x); let py = self.leader(y); if px == py { return false; } let (l, s) = if self.sizes[px] < self.sizes[py] { (py, px) } else { (px, py) }; self.parent[s] = l; self.sizes[l] += self.sizes[s]; self.sizes[s] = 0; self.size -= 1; true } } use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet}; use DisjointSetUnion as DSU; fn main() { let (r, w) = (std::io::stdin(), std::io::stdout()); let mut sc = IO::new(r.lock(), w.lock()); let n = sc.read(); let q = sc.read(); let mut dsu = DSU::new(n); let mut sets: BTreeMap<usize, HashSet<usize>> = BTreeMap::new(); for i in 0..n { let mut set = HashSet::new(); set.insert(i); sets.insert(i, set); } for _ in 0..q { let t = sc.read(); match t { 1 => { let u = sc.usize0(); let v = sc.usize0(); let pu = dsu.leader(u); let pv = dsu.leader(v); dsu.merge(u, v); let p = dsu.leader(u); sets.get_mut(&pu).unwrap().remove(&u); sets.get_mut(&pv).unwrap().remove(&v); sets.get_mut(&p).unwrap().insert(u); sets.get_mut(&p).unwrap().insert(v); if sets.get_mut(&pu).unwrap().is_empty() { sets.remove(&pu); } if sets.get_mut(&pv).unwrap().is_empty() { sets.remove(&pv); } } 2 => { let u = sc.usize0(); let size = dsu.sizes[u]; if size == n { println!("{}", -1); } else { let id = dsu.leader(u); let mut set = sets.range(..id).next_back(); if set.is_none() { set = sets.range((id + 1)..).next(); } println!("{}", set.unwrap().1.iter().next().unwrap() + 1); } } _ => unreachable!() } } } pub struct IO<R, W: std::io::Write>(R, std::io::BufWriter<W>); impl<R: std::io::Read, W: std::io::Write> IO<R, W> { pub fn new(r: R, w: W) -> Self { Self(r, std::io::BufWriter::new(w)) } pub fn write<S: ToString>(&mut self, s: S) { use std::io::Write; self.1.write_all(s.to_string().as_bytes()).unwrap(); } pub fn read<T: std::str::FromStr>(&mut self) -> T { use std::io::Read; let buf = self.0.by_ref().bytes() .map(|b| b.unwrap()) .skip_while(|&b| b == b' ' || b == b'\n' || b == b'\r' || b == b'\t') .take_while(|&b| b != b' ' && b != b'\n' && b != b'\r' && b != b'\t') .collect::<Vec<_>>(); unsafe { std::str::from_utf8_unchecked(&buf) }.parse().ok().expect("Parse error.") } pub fn chars(&mut self) -> Vec<char> { self.read::<String>().chars().collect() } pub fn usize0(&mut self) -> usize { self.read::<usize>() - 1 } pub fn vec<T: std::str::FromStr>(&mut self, n: usize) -> Vec<T> { (0..n).map(|_| self.read()).collect() } }