結果
問題 | No.2677 Minmax Independent Set |
ユーザー | koba-e964 |
提出日時 | 2024-03-16 09:02:28 |
言語 | Rust (1.77.0 + proconio) |
結果 |
AC
|
実行時間 | 373 ms / 2,000 ms |
コード長 | 5,988 bytes |
コンパイル時間 | 13,960 ms |
コンパイル使用メモリ | 382,444 KB |
実行使用メモリ | 139,548 KB |
最終ジャッジ日時 | 2024-09-30 03:53:45 |
合計ジャッジ時間 | 24,630 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
6,816 KB |
testcase_01 | AC | 1 ms
6,816 KB |
testcase_02 | AC | 1 ms
6,816 KB |
testcase_03 | AC | 1 ms
6,816 KB |
testcase_04 | AC | 1 ms
6,820 KB |
testcase_05 | AC | 310 ms
92,580 KB |
testcase_06 | AC | 311 ms
92,628 KB |
testcase_07 | AC | 311 ms
92,980 KB |
testcase_08 | AC | 305 ms
94,724 KB |
testcase_09 | AC | 314 ms
96,752 KB |
testcase_10 | AC | 256 ms
93,284 KB |
testcase_11 | AC | 264 ms
93,336 KB |
testcase_12 | AC | 266 ms
139,548 KB |
testcase_13 | AC | 312 ms
108,608 KB |
testcase_14 | AC | 298 ms
110,240 KB |
testcase_15 | AC | 320 ms
119,820 KB |
testcase_16 | AC | 373 ms
88,476 KB |
testcase_17 | AC | 356 ms
88,576 KB |
testcase_18 | AC | 194 ms
57,148 KB |
testcase_19 | AC | 245 ms
67,368 KB |
testcase_20 | AC | 65 ms
24,960 KB |
testcase_21 | AC | 288 ms
74,328 KB |
testcase_22 | AC | 17 ms
8,320 KB |
testcase_23 | AC | 2 ms
6,820 KB |
testcase_24 | AC | 1 ms
6,820 KB |
testcase_25 | AC | 1 ms
6,820 KB |
testcase_26 | AC | 1 ms
6,820 KB |
testcase_27 | AC | 1 ms
6,816 KB |
testcase_28 | AC | 246 ms
94,208 KB |
testcase_29 | AC | 311 ms
119,780 KB |
testcase_30 | AC | 1 ms
5,248 KB |
testcase_31 | AC | 0 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 | 1 ms
5,248 KB |
testcase_38 | AC | 2 ms
5,248 KB |
testcase_39 | AC | 2 ms
5,248 KB |
testcase_40 | AC | 4 ms
5,248 KB |
testcase_41 | AC | 6 ms
5,248 KB |
testcase_42 | AC | 14 ms
7,168 KB |
testcase_43 | AC | 27 ms
12,416 KB |
testcase_44 | AC | 60 ms
22,912 KB |
testcase_45 | AC | 140 ms
43,872 KB |
testcase_46 | AC | 353 ms
85,680 KB |
testcase_47 | AC | 363 ms
87,168 KB |
testcase_48 | AC | 362 ms
87,040 KB |
testcase_49 | AC | 366 ms
87,168 KB |
testcase_50 | AC | 82 ms
36,992 KB |
testcase_51 | AC | 7 ms
5,248 KB |
testcase_52 | AC | 209 ms
78,464 KB |
testcase_53 | AC | 192 ms
74,000 KB |
testcase_54 | AC | 6 ms
6,816 KB |
testcase_55 | AC | 309 ms
94,236 KB |
testcase_56 | AC | 300 ms
94,380 KB |
testcase_57 | AC | 306 ms
94,200 KB |
testcase_58 | AC | 298 ms
94,188 KB |
testcase_59 | AC | 314 ms
94,240 KB |
testcase_60 | AC | 167 ms
87,664 KB |
testcase_61 | AC | 210 ms
88,332 KB |
testcase_62 | AC | 180 ms
97,192 KB |
testcase_63 | AC | 211 ms
119,084 KB |
コンパイルメッセージ
warning: unused import: `BufWriter` --> src/main.rs:5:22 | 5 | use std::io::{Write, BufWriter}; | ^^^^^^^^^ | = note: `#[warn(unused_imports)]` on by default warning: unused import: `Write` --> src/main.rs:5:15 | 5 | use std::io::{Write, BufWriter}; | ^^^^^
ソースコード
#[allow(unused_imports)] use std::cmp::*; #[allow(unused_imports)] use std::collections::*; use std::io::{Write, BufWriter}; // 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, chars) => { read_value!($next, String).chars().collect::<Vec<char>>() }; ($next:expr, usize1) => (read_value!($next, usize) - 1); ($next:expr, [ $t:tt ]) => {{ let len = read_value!($next, usize); read_value!($next, [$t; len]) }}; ($next:expr, $t:ty) => ($next().parse::<$t>().expect("Parse error")); } trait Monoid { type V: Clone; 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> { #[allow(unused)] pub acc: Vec<Vec<M::E>>, #[allow(unused)] pub 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())); } } 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); }