結果
問題 | No.2290 UnUnion Find |
ユーザー | Sarievo |
提出日時 | 2023-05-05 23:02:16 |
言語 | Rust (1.77.0 + proconio) |
結果 |
RE
|
実行時間 | - |
コード長 | 3,690 bytes |
コンパイル時間 | 15,783 ms |
コンパイル使用メモリ | 379,664 KB |
実行使用メモリ | 38,784 KB |
最終ジャッジ日時 | 2024-11-23 11:17:04 |
合計ジャッジ時間 | 35,070 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
5,248 KB |
testcase_01 | AC | 1 ms
5,248 KB |
testcase_02 | RE | - |
testcase_03 | RE | - |
testcase_04 | AC | 309 ms
22,244 KB |
testcase_05 | AC | 311 ms
22,244 KB |
testcase_06 | AC | 317 ms
22,244 KB |
testcase_07 | AC | 314 ms
22,244 KB |
testcase_08 | AC | 317 ms
22,372 KB |
testcase_09 | AC | 315 ms
22,372 KB |
testcase_10 | AC | 317 ms
22,244 KB |
testcase_11 | AC | 326 ms
22,244 KB |
testcase_12 | AC | 314 ms
22,244 KB |
testcase_13 | AC | 315 ms
22,368 KB |
testcase_14 | AC | 314 ms
22,240 KB |
testcase_15 | AC | 323 ms
22,368 KB |
testcase_16 | AC | 314 ms
22,368 KB |
testcase_17 | AC | 313 ms
22,376 KB |
testcase_18 | AC | 313 ms
22,248 KB |
testcase_19 | WA | - |
testcase_20 | WA | - |
testcase_21 | WA | - |
testcase_22 | WA | - |
testcase_23 | WA | - |
testcase_24 | WA | - |
testcase_25 | WA | - |
testcase_26 | AC | 485 ms
34,176 KB |
testcase_27 | WA | - |
testcase_28 | WA | - |
testcase_29 | WA | - |
testcase_30 | WA | - |
testcase_31 | AC | 428 ms
25,600 KB |
testcase_32 | WA | - |
testcase_33 | WA | - |
testcase_34 | WA | - |
testcase_35 | WA | - |
testcase_36 | WA | - |
testcase_37 | WA | - |
testcase_38 | WA | - |
testcase_39 | WA | - |
testcase_40 | WA | - |
testcase_41 | WA | - |
testcase_42 | WA | - |
testcase_43 | AC | 399 ms
21,760 KB |
testcase_44 | AC | 309 ms
22,368 KB |
testcase_45 | RE | - |
testcase_46 | RE | - |
ソースコード
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, 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); if !dsu.merge(u, v) { continue; } 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 + 1..).next(); if set.is_none() { set = sets.range(..id).next_back(); } println!("{}", set.unwrap().1.iter().nth(0).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() } }