結果
問題 | No.1553 Lovely City |
ユーザー | akakimidori |
提出日時 | 2021-06-19 00:56:18 |
言語 | Rust (1.77.0 + proconio) |
結果 |
AC
|
実行時間 | 208 ms / 2,000 ms |
コード長 | 5,173 bytes |
コンパイル時間 | 14,240 ms |
コンパイル使用メモリ | 383,212 KB |
実行使用メモリ | 33,544 KB |
最終ジャッジ日時 | 2024-06-22 21:46:18 |
合計ジャッジ時間 | 21,365 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
6,812 KB |
testcase_01 | AC | 1 ms
6,816 KB |
testcase_02 | AC | 9 ms
7,384 KB |
testcase_03 | AC | 1 ms
6,940 KB |
testcase_04 | AC | 1 ms
6,944 KB |
testcase_05 | AC | 1 ms
6,940 KB |
testcase_06 | AC | 1 ms
6,940 KB |
testcase_07 | AC | 1 ms
6,944 KB |
testcase_08 | AC | 131 ms
20,984 KB |
testcase_09 | AC | 149 ms
22,476 KB |
testcase_10 | AC | 132 ms
22,724 KB |
testcase_11 | AC | 101 ms
19,068 KB |
testcase_12 | AC | 125 ms
22,544 KB |
testcase_13 | AC | 118 ms
19,412 KB |
testcase_14 | AC | 133 ms
21,064 KB |
testcase_15 | AC | 110 ms
20,208 KB |
testcase_16 | AC | 154 ms
23,636 KB |
testcase_17 | AC | 96 ms
17,476 KB |
testcase_18 | AC | 202 ms
31,976 KB |
testcase_19 | AC | 195 ms
33,544 KB |
testcase_20 | AC | 202 ms
32,660 KB |
testcase_21 | AC | 205 ms
32,900 KB |
testcase_22 | AC | 205 ms
32,472 KB |
testcase_23 | AC | 208 ms
32,076 KB |
testcase_24 | AC | 203 ms
32,248 KB |
testcase_25 | AC | 207 ms
29,956 KB |
testcase_26 | AC | 204 ms
32,108 KB |
testcase_27 | AC | 207 ms
32,472 KB |
ソースコード
//---------- begin union_find ---------- pub struct DSU { p: Vec<i32>, } impl DSU { pub fn new(n: usize) -> DSU { assert!(n < std::i32::MAX as usize); DSU { p: vec![-1; n] } } pub fn init(&mut self) { self.p.iter_mut().for_each(|p| *p = -1); } pub fn root(&self, mut x: usize) -> usize { assert!(x < self.p.len()); while self.p[x] >= 0 { x = self.p[x] as usize; } x } pub fn same(&self, x: usize, y: usize) -> bool { assert!(x < self.p.len() && y < self.p.len()); self.root(x) == self.root(y) } pub fn unite(&mut self, x: usize, y: usize) -> Option<(usize, usize)> { assert!(x < self.p.len() && y < self.p.len()); let mut x = self.root(x); let mut y = self.root(y); if x == y { return None; } if self.p[x] > self.p[y] { std::mem::swap(&mut x, &mut y); } self.p[x] += self.p[y]; self.p[y] = x as i32; Some((x, y)) } pub fn parent(&self, x: usize) -> Option<usize> { assert!(x < self.p.len()); let p = self.p[x]; if p >= 0 { Some(p as usize) } else { None } } pub fn sum<F>(&self, mut x: usize, mut f: F) -> usize where F: FnMut(usize), { while let Some(p) = self.parent(x) { f(x); x = p; } x } pub fn size(&self, x: usize) -> usize { assert!(x < self.p.len()); let r = self.root(x); (-self.p[r]) as usize } } //---------- end union_find ---------- // ---------- begin input macro ---------- // reference: https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 macro_rules! input { (source = $s:expr, $($r:tt)*) => { let mut iter = $s.split_whitespace(); input_inner!{iter, $($r)*} }; ($($r:tt)*) => { let s = { use std::io::Read; let mut s = String::new(); std::io::stdin().read_to_string(&mut s).unwrap(); s }; let mut iter = s.split_whitespace(); input_inner!{iter, $($r)*} }; } macro_rules! input_inner { ($iter:expr) => {}; ($iter:expr, ) => {}; ($iter:expr, $var:ident : $t:tt $($r:tt)*) => { let $var = read_value!($iter, $t); input_inner!{$iter $($r)*} }; } macro_rules! read_value { ($iter:expr, ( $($t:tt),* )) => { ( $(read_value!($iter, $t)),* ) }; ($iter:expr, [ $t:tt ; $len:expr ]) => { (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>() }; ($iter:expr, chars) => { read_value!($iter, String).chars().collect::<Vec<char>>() }; ($iter:expr, bytes) => { read_value!($iter, String).bytes().collect::<Vec<u8>>() }; ($iter:expr, usize1) => { read_value!($iter, usize) - 1 }; ($iter:expr, $t:ty) => { $iter.next().unwrap().parse::<$t>().expect("Parse error") }; } // ---------- end input macro ---------- fn run() { input! { n: usize, m: usize, cond: [(usize1, usize1); m], } let mut dsu = DSU::new(n); let mut edge = vec![vec![]; n]; for (a, b) in cond { if let Some((x, y)) = dsu.unite(a, b) { let mut p = std::mem::take(&mut edge[y]); edge[x].append(&mut p); } edge[dsu.root(a)].push((a, b)); } let mut ans = vec![]; for i in 0..n { if i != dsu.root(i) { continue; } let edge = std::mem::take(&mut edge[i]); let mut z = vec![]; z.extend(edge.iter().map(|e| e.0)); z.extend(edge.iter().map(|e| e.1)); z.sort(); z.dedup(); if edge.len() == z.len() - 1 { ans.extend_from_slice(&edge); continue; } let n = z.len(); let mut g = vec![vec![]; n]; let mut deg = vec![0; n]; for &(a, b) in edge.iter() { let a = z.binary_search(&a).unwrap(); let b = z.binary_search(&b).unwrap(); deg[b] += 1; g[a].push(b); } let mut topo = vec![]; for i in 0..n { if deg[i] == 0 { topo.push(i); } } for i in 0..n { if i >= topo.len() { break; } let v = topo[i]; for &u in g[v].iter() { deg[u] -= 1; if deg[u] == 0 { topo.push(u); } } } if topo.len() == n { for e in topo.windows(2) { ans.push((z[e[0]], z[e[1]])); } } else { let f = z[0]; z.push(f); for z in z.windows(2) { ans.push((z[0], z[1])); } } } use std::io::Write; let out = std::io::stdout(); let mut out = std::io::BufWriter::new(out.lock()); writeln!(out, "{}", ans.len()).ok(); for a in ans { writeln!(out, "{} {}", a.0 + 1, a.1 + 1).ok(); } } fn main() { run(); }