結果
問題 | No.912 赤黒木 |
ユーザー | koba-e964 |
提出日時 | 2020-03-05 12:40:10 |
言語 | Rust (1.77.0 + proconio) |
結果 |
WA
|
実行時間 | - |
コード長 | 3,390 bytes |
コンパイル時間 | 13,671 ms |
コンパイル使用メモリ | 377,840 KB |
実行使用メモリ | 66,048 KB |
最終ジャッジ日時 | 2024-10-14 00:57:44 |
合計ジャッジ時間 | 19,584 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
5,248 KB |
testcase_01 | AC | 1 ms
5,248 KB |
testcase_02 | WA | - |
testcase_03 | WA | - |
testcase_04 | WA | - |
testcase_05 | WA | - |
testcase_06 | WA | - |
testcase_07 | WA | - |
testcase_08 | WA | - |
testcase_09 | WA | - |
testcase_10 | WA | - |
testcase_11 | WA | - |
testcase_12 | WA | - |
testcase_13 | WA | - |
testcase_14 | WA | - |
testcase_15 | WA | - |
testcase_16 | WA | - |
testcase_17 | WA | - |
testcase_18 | WA | - |
testcase_19 | WA | - |
testcase_20 | WA | - |
testcase_21 | WA | - |
testcase_22 | AC | 125 ms
27,608 KB |
testcase_23 | AC | 128 ms
27,724 KB |
testcase_24 | AC | 125 ms
27,648 KB |
testcase_25 | AC | 143 ms
23,936 KB |
testcase_26 | AC | 151 ms
24,704 KB |
testcase_27 | AC | 150 ms
23,620 KB |
testcase_28 | AC | 167 ms
55,424 KB |
testcase_29 | AC | 170 ms
55,040 KB |
testcase_30 | AC | 208 ms
66,048 KB |
testcase_31 | AC | 191 ms
62,080 KB |
ソースコード
#[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); (0..len).map(|_| read_value!($next, $t)).collect::<Vec<_>>() }}; ($next:expr, $t:ty) => { $next().parse::<$t>().expect("Parse error") }; } #[allow(unused)] macro_rules! debug { ($($format:tt)*) => (write!(std::io::stderr(), $($format)*).unwrap()); } #[allow(unused)] macro_rules! debugln { ($($format:tt)*) => (writeln!(std::io::stderr(), $($format)*).unwrap()); } fn dfs(v: usize, par: usize, g: &[Vec<usize>], vs: &[i64]) -> (i64, i64, Option<i64>) { let mut rem = vec![]; let mut tot = 0; let mut ma = 0; if vs[v] > 0 { rem.push(0); } for &w in &g[v] { if par == w { continue; } let (a, tmpma, s) = dfs(w, v, g, vs); ma = max(ma, tmpma); if let Some(s) = s { rem.push(s + 1); tot += 1; } tot += a; } rem.sort(); rem.reverse(); if rem.len() >= 2 { ma = max(ma, rem[0] + rem[1]); } (tot, ma, if rem.len() % 2 == 1 { Some(rem[0]) } else { None }) } fn solve() { let out = std::io::stdout(); let mut out = BufWriter::new(out.lock()); macro_rules! puts { ($($format:tt)*) => (let _ = write!(out,$($format)*);); } input! { n: usize, ab: [(usize1, usize1); n - 1], cd: [(usize1, usize1); n - 1], } let mut cddeg = vec![0i32; n]; for &(c, d) in &cd { cddeg[c] += 1; cddeg[d] += 1; } let mut vs = vec![0; n]; for i in 0..n { for _ in 0..(cddeg[i] - 2).abs() % 2 { vs[i] += 1; } } let mut g = vec![vec![]; n]; for &(a, b) in &ab { g[a].push(b); g[b].push(a); } let (tot, ma, _) = dfs(0, n, &g, &vs); puts!("{}\n", tot - ma + n as i64 - 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(); }