結果
問題 | No.1553 Lovely City |
ユーザー | ziita |
提出日時 | 2021-09-10 14:37:35 |
言語 | Rust (1.77.0 + proconio) |
結果 |
AC
|
実行時間 | 386 ms / 2,000 ms |
コード長 | 4,534 bytes |
コンパイル時間 | 14,723 ms |
コンパイル使用メモリ | 380,024 KB |
実行使用メモリ | 34,044 KB |
最終ジャッジ日時 | 2024-06-11 03:20:20 |
合計ジャッジ時間 | 22,939 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
5,248 KB |
testcase_01 | AC | 1 ms
5,376 KB |
testcase_02 | AC | 29 ms
25,832 KB |
testcase_03 | AC | 2 ms
5,376 KB |
testcase_04 | AC | 2 ms
5,376 KB |
testcase_05 | AC | 2 ms
5,376 KB |
testcase_06 | AC | 2 ms
5,376 KB |
testcase_07 | AC | 2 ms
5,376 KB |
testcase_08 | AC | 277 ms
20,448 KB |
testcase_09 | AC | 218 ms
23,564 KB |
testcase_10 | AC | 246 ms
28,488 KB |
testcase_11 | AC | 176 ms
18,652 KB |
testcase_12 | AC | 286 ms
29,128 KB |
testcase_13 | AC | 175 ms
18,868 KB |
testcase_14 | AC | 210 ms
20,744 KB |
testcase_15 | AC | 192 ms
21,060 KB |
testcase_16 | AC | 234 ms
24,392 KB |
testcase_17 | AC | 284 ms
22,360 KB |
testcase_18 | AC | 370 ms
33,796 KB |
testcase_19 | AC | 386 ms
33,932 KB |
testcase_20 | AC | 377 ms
33,924 KB |
testcase_21 | AC | 349 ms
33,776 KB |
testcase_22 | AC | 328 ms
33,800 KB |
testcase_23 | AC | 327 ms
33,904 KB |
testcase_24 | AC | 313 ms
34,044 KB |
testcase_25 | AC | 316 ms
33,936 KB |
testcase_26 | AC | 315 ms
33,652 KB |
testcase_27 | AC | 347 ms
33,796 KB |
ソースコード
#![allow(unused_imports)] #![allow(non_snake_case, unused)] // ---------- begin input macro ---------- // https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 より macro_rules! input { (source = $s:expr, $($r:tt)*) => { let mut iter = $s.split_whitespace(); let mut next = || { iter.next().unwrap() }; input_inner!{next, $($r)*} }; ($($r:tt)*) => { let stdin = std::io::stdin(); let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock())); let mut next = move || -> String{ bytes .by_ref() .map(|r|r.unwrap() as char) .skip_while(|c|c.is_whitespace()) .take_while(|c|!c.is_whitespace()) .collect() }; input_inner!{next, $($r)*} }; } macro_rules! input_inner { ($next:expr) => {}; ($next:expr, ) => {}; ($next:expr, $var:ident : $t:tt $($r:tt)*) => { let $var = read_value!($next, $t); input_inner!{$next $($r)*} }; } macro_rules! read_value { ($next:expr, ( $($t:tt),* )) => { ( $(read_value!($next, $t)),* ) }; ($next:expr, [ $t:tt ; $len:expr ]) => { (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>() }; ($next:expr, chars) => { read_value!($next, String).chars().collect::<Vec<char>>() }; ($next:expr, usize1) => { read_value!($next, usize) - 1 }; ($next:expr, $t:ty) => { $next().parse::<$t>().expect("Parse error") }; } // ---------- end input macro ---------- use std::cmp::*; use std::collections::*; use std::ops::*; use std::marker::*; const INF: i64 = std::i64::MAX/100; const MOD: i64 = 1_000_000_007; // const MOD: i64 = 998_244_353; pub struct UnionFind { data: Vec<i32>, } impl UnionFind { pub fn new(n: usize) -> Self{ UnionFind { data: vec![-1;n] } } pub fn root(&mut self, x: usize) -> usize{ if self.data[x]<0 { x } else { let p = self.data[x] as usize; self.data[x] = self.root(p) as i32; self.data[x] as usize } } pub fn unite(&mut self, x: usize, y: usize){ let rx = self.root(x); let ry = self.root(y); if rx!=ry { let (rx,ry) = if self.data[rx]<=self.data[ry] { (rx,ry) } else{ (ry,rx) }; self.data[rx] += self.data[ry]; self.data[ry] = rx as i32; } } pub fn same(&mut self, x: usize, y: usize) -> bool{ self.root(x) == self.root(y) } pub fn size(&mut self, x: usize) -> i32 { let r = self.root(x); - self.data[r] } } fn main() { input! { n: usize, m: usize, uv: [(usize,usize);m], } let mut uf = UnionFind::new(n); let mut e = vec![vec![];n]; for &(u,v) in &uv { uf.unite(u-1,v-1); e[u-1].push(v-1); } let mut count = 0; let mut map = HashMap::new(); for i in 0..n { if uf.root(i)==i { map.insert(i,count); count += 1; } } let mut edge = vec![vec![];count]; let mut set = vec![HashSet::new();count]; for &(u,v) in &uv { let idx = *map.get(&(uf.root(u-1))).unwrap(); edge[idx].push((u-1,v-1)); set[idx].insert(u-1); set[idx].insert(v-1); } let mut ans = vec![]; let mut inner = vec![0;n]; for i in 0..count { let mut ret = vec![]; if edge[i].len()==0 { continue; } for &(u,v) in &edge[i] { inner[v] += 1; } let mut q = VecDeque::new(); let mut l = vec![]; for &s in set[i].iter() { l.push(s); if inner[s]==0 { q.push_back(s); } } while let Some(v) = q.pop_front() { ret.push(v); for &e in &e[v] { inner[e] -= 1; if inner[e]==0 { q.push_back(e); } } } if ret.len()==set[i].len() { for j in 0..ret.len()-1 { ans.push((ret[j],ret[j+1])); } } else { for j in 0..l.len() { ans.push((l[j],l[(j+1)%l.len()])); } } } println!("{}",ans.len()); for &(s,t) in &ans { println!("{} {}",s+1,t+1); } }