結果
問題 | No.2638 Initial fare |
ユーザー | naut3 |
提出日時 | 2024-02-19 22:24:13 |
言語 | Rust (1.77.0 + proconio) |
結果 |
AC
|
実行時間 | 169 ms / 2,000 ms |
コード長 | 2,732 bytes |
コンパイル時間 | 13,737 ms |
コンパイル使用メモリ | 391,124 KB |
実行使用メモリ | 26,144 KB |
最終ジャッジ日時 | 2024-09-29 02:17:53 |
合計ジャッジ時間 | 17,929 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
6,820 KB |
testcase_01 | AC | 1 ms
6,820 KB |
testcase_02 | AC | 1 ms
6,820 KB |
testcase_03 | AC | 1 ms
6,816 KB |
testcase_04 | AC | 136 ms
22,836 KB |
testcase_05 | AC | 146 ms
24,044 KB |
testcase_06 | AC | 1 ms
6,816 KB |
testcase_07 | AC | 122 ms
24,792 KB |
testcase_08 | AC | 76 ms
21,556 KB |
testcase_09 | AC | 96 ms
26,144 KB |
testcase_10 | AC | 85 ms
23,340 KB |
testcase_11 | AC | 1 ms
6,820 KB |
testcase_12 | AC | 126 ms
24,400 KB |
testcase_13 | AC | 94 ms
20,404 KB |
testcase_14 | AC | 117 ms
25,572 KB |
testcase_15 | AC | 98 ms
22,812 KB |
testcase_16 | AC | 1 ms
6,820 KB |
testcase_17 | AC | 129 ms
23,992 KB |
testcase_18 | AC | 113 ms
21,252 KB |
testcase_19 | AC | 132 ms
24,064 KB |
testcase_20 | AC | 133 ms
23,748 KB |
testcase_21 | AC | 134 ms
23,220 KB |
testcase_22 | AC | 0 ms
6,816 KB |
testcase_23 | AC | 161 ms
22,512 KB |
testcase_24 | AC | 169 ms
23,152 KB |
testcase_25 | AC | 1 ms
6,820 KB |
testcase_26 | AC | 162 ms
23,264 KB |
testcase_27 | AC | 153 ms
23,116 KB |
ソースコード
#![allow(non_snake_case, unused_imports, unused_must_use)] use std::io::{self, prelude::*}; use std::str; fn main() { let (stdin, stdout) = (io::stdin(), io::stdout()); let mut scan = Scanner::new(stdin.lock()); let mut out = io::BufWriter::new(stdout.lock()); macro_rules! input { ($T: ty) => { scan.token::<$T>() }; ($T: ty, $N: expr) => { (0..$N).map(|_| scan.token::<$T>()).collect::<Vec<_>>() }; } let N = input!(usize); let mut graph = vec![vec![]; N]; for _ in 0..N - 1 { let (u, v) = (input!(usize) - 1, input!(usize) - 1); graph[u].push(v); graph[v].push(u); } let r = 0; let mut dist = vec![0; N]; let mut seen = vec![false; N]; let mut q = std::collections::VecDeque::new(); q.push_back(r); seen[r] = true; while let Some(now) = q.pop_front() { for &nxt in graph[now].iter() { if seen[nxt] { continue; } q.push_back(nxt); dist[nxt] = dist[now] + 1; seen[nxt] = true; } } let mut ans = 0; let mut dp1 = vec![0; N]; let mut dp2 = vec![0; N]; let mut dp3 = vec![0; N]; let mut order = (0..N).map(|i| i).collect::<Vec<_>>(); order.sort_by_key(|&i| dist[i]); order.reverse(); for v in order { if v != r { dp1[v] = graph[v].len() - 1; } else { dp1[v] = graph[v].len(); } for &nxt in graph[v].iter() { if dist[nxt] <= dist[v] { continue; } dp2[v] += dp1[nxt]; dp3[v] += dp2[nxt]; } ans += dp1[v] + dp2[v] + dp3[v]; if dp1[v] != 0 { ans += (dp1[v] - 1) * dp2[v] + (dp1[v] - 1) * dp1[v] / 2; } } writeln!(out, "{}", ans); } struct Scanner<R> { reader: R, buf_str: Vec<u8>, buf_iter: str::SplitWhitespace<'static>, } impl<R: BufRead> Scanner<R> { fn new(reader: R) -> Self { Self { reader, buf_str: vec![], buf_iter: "".split_whitespace(), } } fn token<T: str::FromStr>(&mut self) -> T { loop { if let Some(token) = self.buf_iter.next() { return token.parse().ok().expect("Failed parse"); } self.buf_str.clear(); self.reader .read_until(b'\n', &mut self.buf_str) .expect("Failed read"); self.buf_iter = unsafe { let slice = str::from_utf8_unchecked(&self.buf_str); std::mem::transmute(slice.split_whitespace()) } } } }