結果
問題 | No.2998 Rainbow Christmas Tree |
ユーザー | akakimidori |
提出日時 | 2024-12-23 21:45:56 |
言語 | Rust (1.77.0 + proconio) |
結果 |
RE
|
実行時間 | - |
コード長 | 5,654 bytes |
コンパイル時間 | 16,076 ms |
コンパイル使用メモリ | 378,728 KB |
実行使用メモリ | 13,696 KB |
最終ジャッジ日時 | 2024-12-23 21:46:28 |
合計ジャッジ時間 | 25,302 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,248 KB |
testcase_02 | AC | 2 ms
5,248 KB |
testcase_03 | AC | 2 ms
5,248 KB |
testcase_04 | AC | 2 ms
5,248 KB |
testcase_05 | AC | 1 ms
5,248 KB |
testcase_06 | AC | 1 ms
5,248 KB |
testcase_07 | AC | 179 ms
13,440 KB |
testcase_08 | RE | - |
testcase_09 | AC | 33 ms
13,440 KB |
testcase_10 | AC | 36 ms
13,440 KB |
testcase_11 | AC | 34 ms
13,440 KB |
testcase_12 | AC | 32 ms
13,440 KB |
testcase_13 | AC | 31 ms
13,440 KB |
testcase_14 | AC | 32 ms
13,440 KB |
testcase_15 | RE | - |
testcase_16 | RE | - |
testcase_17 | AC | 33 ms
13,568 KB |
testcase_18 | AC | 50 ms
13,568 KB |
testcase_19 | AC | 31 ms
13,312 KB |
testcase_20 | RE | - |
testcase_21 | AC | 34 ms
13,696 KB |
testcase_22 | AC | 34 ms
13,696 KB |
testcase_23 | AC | 33 ms
13,568 KB |
testcase_24 | AC | 31 ms
13,568 KB |
testcase_25 | AC | 32 ms
13,568 KB |
testcase_26 | AC | 32 ms
13,568 KB |
testcase_27 | AC | 31 ms
13,568 KB |
testcase_28 | AC | 31 ms
13,568 KB |
testcase_29 | AC | 32 ms
13,568 KB |
testcase_30 | AC | 32 ms
13,568 KB |
testcase_31 | RE | - |
testcase_32 | AC | 33 ms
13,568 KB |
testcase_33 | AC | 37 ms
13,568 KB |
testcase_34 | RE | - |
testcase_35 | RE | - |
testcase_36 | AC | 34 ms
13,440 KB |
testcase_37 | AC | 34 ms
13,568 KB |
testcase_38 | AC | 33 ms
13,568 KB |
testcase_39 | RE | - |
testcase_40 | AC | 34 ms
13,568 KB |
testcase_41 | AC | 33 ms
13,568 KB |
testcase_42 | AC | 33 ms
13,568 KB |
testcase_43 | AC | 33 ms
13,440 KB |
testcase_44 | RE | - |
testcase_45 | AC | 1 ms
5,248 KB |
testcase_46 | AC | 1 ms
5,248 KB |
testcase_47 | AC | 37 ms
13,568 KB |
testcase_48 | AC | 33 ms
13,568 KB |
testcase_49 | AC | 31 ms
13,568 KB |
testcase_50 | AC | 33 ms
13,568 KB |
testcase_51 | AC | 31 ms
13,568 KB |
testcase_52 | AC | 32 ms
13,440 KB |
testcase_53 | AC | 32 ms
13,568 KB |
testcase_54 | AC | 31 ms
13,568 KB |
testcase_55 | AC | 62 ms
13,568 KB |
testcase_56 | AC | 31 ms
13,568 KB |
testcase_57 | RE | - |
ソースコード
fn main() { input! { n: usize, k: usize, pa: [[usize1; n]; n - 1], } let mut dsu = DSU::new(n); let mut parent = vec![n; n]; let mut root = n; 'outer: for (c, p) in pa.iter().enumerate() { let r = if c < k { n - 1 } else { 0 }; for (j, &p) in p.iter().enumerate() { if p < n && !dsu.same(p, j) && dsu.same(j, r) { dsu.unite(p, j); parent[j] = c; if dsu.same(0, n - 1) { root = p; let mut fix = vec![false; n]; fix[root] = true; parent[root] = n; for &x in [0, n - 1].iter() { let mut pos = x; while pos != root { fix[pos] = true; pos = pa[parent[pos]][pos]; } } for i in 0..n { if !fix[i] { parent[i] = n; } } break 'outer; } break; } } } assert!(dsu.same(0, n - 1)); dsu.init(); let mut used = vec![false; n - 1]; for (i, p) in parent.iter().enumerate() { if *p != n { dsu.unite(i, pa[*p][i]); used[*p] = true; } } assert!(dsu.same(0, n - 1)); for (i, pa) in pa.iter().enumerate() { if used[i] { continue; } for (j, &p) in pa.iter().enumerate() { if p < n && j != root && !dsu.same(p, j) && dsu.same(p, 0) { parent[j] = i; dsu.unite(p, j); break; } } } //assert!(dsu.size(0) == n); for p in parent.iter_mut() { *p += 1; } parent[root] = 0; println!("Yes"); use util::*; println!("{}", parent.iter().join(" ")); } // ---------- begin input macro ---------- // reference: https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 #[macro_export] 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_export] 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_export] 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 ---------- //---------- 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 ---------- mod util { pub trait Join { fn join(self, sep: &str) -> String; } impl<T, I> Join for I where I: Iterator<Item = T>, T: std::fmt::Display, { fn join(self, sep: &str) -> String { let mut s = String::new(); use std::fmt::*; for (i, v) in self.enumerate() { if i > 0 { write!(&mut s, "{}", sep).ok(); } write!(&mut s, "{}", v).ok(); } s } } }