結果
問題 | No.1640 簡単な色塗り |
ユーザー | akakimidori |
提出日時 | 2021-08-06 23:05:21 |
言語 | Rust (1.77.0 + proconio) |
結果 |
AC
|
実行時間 | 220 ms / 2,000 ms |
コード長 | 4,681 bytes |
コンパイル時間 | 13,375 ms |
コンパイル使用メモリ | 384,804 KB |
実行使用メモリ | 16,000 KB |
最終ジャッジ日時 | 2024-06-29 16:11:09 |
合計ジャッジ時間 | 25,327 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
6,812 KB |
testcase_01 | AC | 1 ms
6,816 KB |
testcase_02 | AC | 1 ms
6,944 KB |
testcase_03 | AC | 1 ms
6,944 KB |
testcase_04 | AC | 169 ms
11,904 KB |
testcase_05 | AC | 179 ms
16,000 KB |
testcase_06 | AC | 0 ms
6,944 KB |
testcase_07 | AC | 1 ms
6,940 KB |
testcase_08 | AC | 0 ms
6,940 KB |
testcase_09 | AC | 1 ms
6,940 KB |
testcase_10 | AC | 130 ms
9,728 KB |
testcase_11 | AC | 98 ms
8,192 KB |
testcase_12 | AC | 86 ms
7,424 KB |
testcase_13 | AC | 190 ms
13,696 KB |
testcase_14 | AC | 192 ms
13,568 KB |
testcase_15 | AC | 63 ms
6,940 KB |
testcase_16 | AC | 77 ms
7,040 KB |
testcase_17 | AC | 155 ms
11,776 KB |
testcase_18 | AC | 14 ms
6,940 KB |
testcase_19 | AC | 86 ms
7,424 KB |
testcase_20 | AC | 121 ms
9,344 KB |
testcase_21 | AC | 97 ms
8,064 KB |
testcase_22 | AC | 10 ms
6,944 KB |
testcase_23 | AC | 133 ms
9,984 KB |
testcase_24 | AC | 34 ms
6,944 KB |
testcase_25 | AC | 87 ms
7,552 KB |
testcase_26 | AC | 142 ms
11,008 KB |
testcase_27 | AC | 60 ms
6,944 KB |
testcase_28 | AC | 191 ms
13,184 KB |
testcase_29 | AC | 141 ms
10,752 KB |
testcase_30 | AC | 7 ms
6,944 KB |
testcase_31 | AC | 54 ms
13,696 KB |
testcase_32 | AC | 46 ms
12,032 KB |
testcase_33 | AC | 30 ms
8,832 KB |
testcase_34 | AC | 37 ms
10,496 KB |
testcase_35 | AC | 29 ms
8,832 KB |
testcase_36 | AC | 7 ms
6,940 KB |
testcase_37 | AC | 9 ms
6,944 KB |
testcase_38 | AC | 48 ms
12,544 KB |
testcase_39 | AC | 18 ms
6,940 KB |
testcase_40 | AC | 19 ms
6,940 KB |
testcase_41 | AC | 39 ms
10,752 KB |
testcase_42 | AC | 24 ms
7,168 KB |
testcase_43 | AC | 24 ms
7,424 KB |
testcase_44 | AC | 23 ms
7,296 KB |
testcase_45 | AC | 17 ms
6,940 KB |
testcase_46 | AC | 8 ms
6,944 KB |
testcase_47 | AC | 5 ms
6,940 KB |
testcase_48 | AC | 50 ms
12,800 KB |
testcase_49 | AC | 2 ms
6,940 KB |
testcase_50 | AC | 0 ms
6,940 KB |
testcase_51 | AC | 1 ms
6,944 KB |
testcase_52 | AC | 216 ms
14,848 KB |
testcase_53 | AC | 220 ms
14,720 KB |
07_evil_01.txt | AC | 484 ms
28,416 KB |
07_evil_02.txt | AC | 745 ms
41,744 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 ---------- use std::collections::*; type Set<T> = BTreeSet<T>; fn run() { input! { n: usize, e: [(usize, usize); n], } let mut g = Set::new(); let mut deg = vec![0; n + 1]; let mut dsu = DSU::new(n + 1); let mut dp = vec![0; n + 1]; for (i, &(a, b)) in e.iter().enumerate() { g.insert((a, b, i)); g.insert((b, a, i)); deg[a] += 1; deg[b] += 1; if let Some((a, b)) = dsu.unite(a, b) { dp[a] += dp[b]; } dp[dsu.root(a)] += 1; } if (1..=n).any(|r| r == dsu.root(r) && dp[r] != dsu.size(r)) { println!("No"); return; } let mut dfs = vec![]; for i in 1..=n { if deg[i] == 1 { dfs.push(i); } } let mut ans = vec![0; n]; while let Some(v) = dfs.pop() { let (_, u, k) = *g.range((v, 0, 0)..).next().unwrap(); ans[k] = v; g.remove(&(v, u, k)); g.remove(&(u, v, k)); deg[u] -= 1; deg[v] -= 1; if deg[u] == 1 { dfs.push(u); } } for i in 1..=n { if deg[i] != 2 { continue; } let mut e = *g.range((i, 0, 0)..).next().unwrap(); if e.0 == e.1 { ans[e.2] = i; continue; } while let Some(&x) = g.range((e.1, 0, 0)..).take(2).find(|p| p.2 != e.2) { if ans[x.2] != 0 { break; } ans[x.2] = x.0; e = x; } } println!("Yes"); for a in ans { println!("{}", a); } } fn main() { run(); }