結果
問題 | No.1769 Don't Stop the Game |
ユーザー | koba-e964 |
提出日時 | 2021-11-30 13:57:08 |
言語 | Rust (1.77.0 + proconio) |
結果 |
AC
|
実行時間 | 315 ms / 3,000 ms |
コード長 | 3,510 bytes |
コンパイル時間 | 22,703 ms |
コンパイル使用メモリ | 403,868 KB |
実行使用メモリ | 84,180 KB |
最終ジャッジ日時 | 2024-07-03 08:27:51 |
合計ジャッジ時間 | 21,570 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
6,812 KB |
testcase_01 | AC | 1 ms
6,812 KB |
testcase_02 | AC | 1 ms
6,944 KB |
testcase_03 | AC | 2 ms
6,944 KB |
testcase_04 | AC | 1 ms
6,940 KB |
testcase_05 | AC | 1 ms
6,940 KB |
testcase_06 | AC | 2 ms
6,944 KB |
testcase_07 | AC | 1 ms
6,940 KB |
testcase_08 | AC | 182 ms
30,828 KB |
testcase_09 | AC | 137 ms
22,912 KB |
testcase_10 | AC | 210 ms
34,256 KB |
testcase_11 | AC | 120 ms
22,528 KB |
testcase_12 | AC | 238 ms
39,444 KB |
testcase_13 | AC | 236 ms
40,108 KB |
testcase_14 | AC | 232 ms
38,840 KB |
testcase_15 | AC | 215 ms
37,868 KB |
testcase_16 | AC | 248 ms
37,504 KB |
testcase_17 | AC | 292 ms
38,748 KB |
testcase_18 | AC | 292 ms
45,688 KB |
testcase_19 | AC | 294 ms
49,672 KB |
testcase_20 | AC | 311 ms
50,408 KB |
testcase_21 | AC | 315 ms
50,608 KB |
testcase_22 | AC | 313 ms
50,404 KB |
testcase_23 | AC | 240 ms
44,224 KB |
testcase_24 | AC | 228 ms
41,724 KB |
testcase_25 | AC | 95 ms
38,992 KB |
testcase_26 | AC | 198 ms
52,800 KB |
testcase_27 | AC | 135 ms
79,052 KB |
testcase_28 | AC | 213 ms
84,180 KB |
testcase_29 | AC | 209 ms
68,152 KB |
testcase_30 | AC | 215 ms
66,844 KB |
ソースコード
// 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")); } 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(); } fn dfs1(v: usize, par: usize, g: &[Vec<(usize, u32)>], dp: &mut [u32], x: u32) { dp[v] = x; for &(w, c) in &g[v] { if par == w { continue; } dfs1(w, v, g, dp, x ^ c); } } fn dfs2(v: usize, par: usize, g: &[Vec<(usize, u32)>], coo: &[u32], dp: &[u32], pre: &mut [usize], ch: &mut [Vec<usize>], root: &mut [Vec<usize>]) { let idx = coo.binary_search(&dp[v]).unwrap(); let n = g.len(); if pre[idx] == n { root[idx].push(v); } else { ch[pre[idx]].push(v); } let oldpre = pre[idx]; for &(w, _) in &g[v] { if par == w { continue; } pre[idx] = w; dfs2(w, v, g, coo, dp, pre, ch, root); } pre[idx] = oldpre; } fn dfs3(v: usize, par: usize, g: &[Vec<(usize, u32)>], sz: &mut [i32]) { let mut s = 1; for &(w, _) in &g[v] { if par == w { continue; } dfs3(w, v, g, sz); s += sz[w]; } sz[v] = s; } fn solve() { input! { n: usize, abc: [(usize1, usize1, u32); n - 1], } let mut g = vec![vec![]; n]; for &(a, b, c) in &abc { g[a].push((b, c)); g[b].push((a, c)); } let mut dp = vec![0; n]; dfs1(0, n, &g, &mut dp, 0); let mut coo = dp.clone(); coo.sort(); coo.dedup(); let m = coo.len(); let mut pre = vec![n; m]; let mut ch = vec![vec![]; n]; let mut root = vec![vec![]; m]; dfs2(0, n, &g, &coo, &dp, &mut pre, &mut ch, &mut root); let mut sz = vec![0; n]; dfs3(0, n, &g, &mut sz); let mut ans = 0i64; for i in 0..m { let mut cs = n as i32; let mut t = 0; for &v in &root[i] { cs -= sz[v]; t += 1; } ans += cs as i64 * t; } for i in 1..n { let mut cs = sz[i]; let mut t = 1; for &v in &ch[i] { cs -= sz[v]; t += 1; } ans += cs as i64 * t; } println!("{}", ans); }