結果
問題 | No.2677 Minmax Independent Set |
ユーザー | koba-e964 |
提出日時 | 2024-03-16 09:06:12 |
言語 | Rust (1.77.0 + proconio) |
結果 |
AC
|
実行時間 | 477 ms / 2,000 ms |
コード長 | 5,684 bytes |
コンパイル時間 | 15,006 ms |
コンパイル使用メモリ | 379,428 KB |
実行使用メモリ | 139,392 KB |
最終ジャッジ日時 | 2024-09-30 03:54:17 |
合計ジャッジ時間 | 29,312 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
6,820 KB |
testcase_01 | AC | 2 ms
6,816 KB |
testcase_02 | AC | 2 ms
6,820 KB |
testcase_03 | AC | 2 ms
6,820 KB |
testcase_04 | AC | 2 ms
6,820 KB |
testcase_05 | AC | 436 ms
91,624 KB |
testcase_06 | AC | 427 ms
92,416 KB |
testcase_07 | AC | 399 ms
92,928 KB |
testcase_08 | AC | 390 ms
94,208 KB |
testcase_09 | AC | 383 ms
96,512 KB |
testcase_10 | AC | 324 ms
93,440 KB |
testcase_11 | AC | 322 ms
93,440 KB |
testcase_12 | AC | 309 ms
139,392 KB |
testcase_13 | AC | 383 ms
108,288 KB |
testcase_14 | AC | 362 ms
109,312 KB |
testcase_15 | AC | 380 ms
118,912 KB |
testcase_16 | AC | 451 ms
88,576 KB |
testcase_17 | AC | 457 ms
88,576 KB |
testcase_18 | AC | 282 ms
57,216 KB |
testcase_19 | AC | 327 ms
67,200 KB |
testcase_20 | AC | 88 ms
25,088 KB |
testcase_21 | AC | 379 ms
74,240 KB |
testcase_22 | AC | 20 ms
8,320 KB |
testcase_23 | AC | 2 ms
5,248 KB |
testcase_24 | AC | 1 ms
5,248 KB |
testcase_25 | AC | 1 ms
5,248 KB |
testcase_26 | AC | 1 ms
5,248 KB |
testcase_27 | AC | 2 ms
5,248 KB |
testcase_28 | AC | 309 ms
94,208 KB |
testcase_29 | AC | 398 ms
119,808 KB |
testcase_30 | AC | 1 ms
5,248 KB |
testcase_31 | AC | 1 ms
5,248 KB |
testcase_32 | AC | 1 ms
5,248 KB |
testcase_33 | AC | 1 ms
5,248 KB |
testcase_34 | AC | 1 ms
5,248 KB |
testcase_35 | AC | 1 ms
5,248 KB |
testcase_36 | AC | 1 ms
5,248 KB |
testcase_37 | AC | 2 ms
5,248 KB |
testcase_38 | AC | 2 ms
5,248 KB |
testcase_39 | AC | 3 ms
5,248 KB |
testcase_40 | AC | 4 ms
5,248 KB |
testcase_41 | AC | 8 ms
5,248 KB |
testcase_42 | AC | 17 ms
7,296 KB |
testcase_43 | AC | 37 ms
12,416 KB |
testcase_44 | AC | 84 ms
22,912 KB |
testcase_45 | AC | 203 ms
43,904 KB |
testcase_46 | AC | 456 ms
85,780 KB |
testcase_47 | AC | 474 ms
87,040 KB |
testcase_48 | AC | 477 ms
87,040 KB |
testcase_49 | AC | 455 ms
87,296 KB |
testcase_50 | AC | 102 ms
36,992 KB |
testcase_51 | AC | 7 ms
5,248 KB |
testcase_52 | AC | 257 ms
78,336 KB |
testcase_53 | AC | 232 ms
72,448 KB |
testcase_54 | AC | 6 ms
5,248 KB |
testcase_55 | AC | 393 ms
94,336 KB |
testcase_56 | AC | 392 ms
94,292 KB |
testcase_57 | AC | 386 ms
94,248 KB |
testcase_58 | AC | 384 ms
94,336 KB |
testcase_59 | AC | 393 ms
94,164 KB |
testcase_60 | AC | 186 ms
87,596 KB |
testcase_61 | AC | 236 ms
88,408 KB |
testcase_62 | AC | 201 ms
96,856 KB |
testcase_63 | AC | 238 ms
118,116 KB |
コンパイルメッセージ
warning: field `acc` is never read --> src/main.rs:45:5 | 44 | struct MonoidalReroot<M: Monoid> { | -------------- field in this struct 45 | acc: Vec<Vec<M::E>>, | ^^^ | = note: `#[warn(dead_code)]` on by default
ソースコード
// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 macro_rules! input { ($($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, usize1) => (read_value!($next, usize) - 1); ($next:expr, $t:ty) => ($next().parse::<$t>().expect("Parse error")); } trait Monoid { type V; type E: Clone; fn add(a: &Self::E, b: &Self::E) -> Self::E; fn e() -> Self::E; fn put_edge(a: &Self::V, eidx: EIdx) -> Self::E; fn put_vertex(a: &Self::E, vidx: usize) -> Self::V; } // Ported from https://trap.jp/post/1702/ struct MonoidalReroot<M: Monoid> { acc: Vec<Vec<M::E>>, acc_rev: Vec<Vec<M::E>>, } type EIdx = usize; impl<M: Monoid> MonoidalReroot<M> { pub fn new(g: &[Vec<(usize, EIdx)>]) -> Self { let n = g.len(); let mut acc = vec![vec![]; n]; let mut acc_rev = vec![vec![]; n]; let mut ch = vec![vec![]; n]; for i in 0..n { acc[i] = vec![M::e(); g[i].len() + 1]; acc_rev[i] = vec![M::e(); g[i].len() + 1]; } Self::dfs1(0, n, &g, &mut ch); for i in 0..n { let l = ch[i].len(); ch[i].sort_unstable_by_key(|&(eidx, _)| eidx); for j in 0..l { let val = M::add(&acc[i][j], &ch[i][j].1); acc[i][j + 1] = val; } for j in (0..l).rev() { let val = M::add(&acc_rev[i][j + 1], &ch[i][j].1); acc_rev[i][j] = val; } } Self::dfs2(0, n, &g, &mut ch, &acc, &acc_rev, M::e()); for i in 0..n { let l = ch[i].len(); ch[i].sort_unstable_by_key(|&(eidx, _)| eidx); for j in 0..l { let val = M::add(&acc[i][j], &ch[i][j].1); acc[i][j + 1] = val; } for j in (0..l).rev() { let val = M::add(&acc_rev[i][j + 1], &ch[i][j].1); acc_rev[i][j] = val; } } MonoidalReroot { acc: acc, acc_rev: acc_rev, } } fn dfs1( v: usize, par: usize, g: &[Vec<(usize, EIdx)>], ch: &mut [Vec<(EIdx, M::E)>], ) -> M::V { let mut me = M::e(); for i in 0..g[v].len() { let (w, eidx) = g[v][i]; if w == par { continue; } let chval = Self::dfs1(w, v, g, ch); let chval = M::put_edge(&chval, eidx); ch[v].push((eidx, chval.clone())); me = M::add(&me, &chval); } M::put_vertex(&me, v) } fn dfs2( v: usize, par: usize, g: &[Vec<(usize, EIdx)>], ch: &mut [Vec<(EIdx, M::E)>], acc: &[Vec<M::E>], acc_rev: &[Vec<M::E>], passed: M::E, ) { let mut parenteidx = None; for i in 0..g[v].len() { let (w, eidx) = g[v][i]; if w == par { parenteidx = Some(eidx); continue; } let j = ch[v].binary_search_by_key(&eidx, |&(eidx, _)| eidx).unwrap(); let inherited = M::add(&acc[v][j], &acc_rev[v][j + 1]); let inherited = if par >= g.len() { inherited } else { M::add(&inherited, &passed) }; let inherited = M::put_vertex(&inherited, v); let inherited = M::put_edge(&inherited, eidx); Self::dfs2(w, v, g, ch, acc, acc_rev, inherited); } if let Some(eidx) = parenteidx { ch[v].push((eidx, passed.clone())); } } #[inline] pub fn query(&self, v: usize) -> M::V { M::put_vertex(&self.acc_rev[v][0], v) } } enum IndepMonoid {} impl Monoid for IndepMonoid { type V = [i64; 2]; // [その頂点を使わない, 使う] type E = [i64; 2]; // [一番上を使わない, 使っても良い] fn add(a: &Self::E, b: &Self::E) -> Self::E { [a[0] + b[0], a[1] + b[1]] } fn e() -> Self::E { [0, 0] } fn put_edge(&a: &Self::V, _eidx: EIdx) -> Self::E { [a[0], std::cmp::max(a[0], a[1])] } fn put_vertex(a: &Self::E, _vidx: usize) -> Self::V { [a[1], a[0] + 1] } } fn main() { // In order to avoid potential stack overflow, spawn a new thread. let stack_size = 104_857_600; // 100 MB let thd = std::thread::Builder::new().stack_size(stack_size); thd.spawn(|| solve()).unwrap().join().unwrap(); } // Tags: rerooting fn solve() { input! { n: usize, uv: [(usize1, usize1); n - 1], } let mut g = vec![vec![]; n]; for i in 0..n - 1 { let (u, v) = uv[i]; g[u].push((v, i)); g[v].push((u, i + n)); } let rr = MonoidalReroot::<IndepMonoid>::new(&g); let mut ans = rr.query(0)[1]; for i in 1..n { ans = std::cmp::min(ans, rr.query(i)[1]); } println!("{}", ans); }