結果
問題 | No.806 木を道に |
ユーザー | koba-e964 |
提出日時 | 2019-03-23 00:06:59 |
言語 | Rust (1.77.0 + proconio) |
結果 |
AC
|
実行時間 | 46 ms / 2,000 ms |
コード長 | 4,329 bytes |
コンパイル時間 | 15,326 ms |
コンパイル使用メモリ | 378,360 KB |
実行使用メモリ | 15,232 KB |
最終ジャッジ日時 | 2024-09-19 07:10:51 |
合計ジャッジ時間 | 17,214 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
5,248 KB |
testcase_01 | AC | 1 ms
5,376 KB |
testcase_02 | AC | 1 ms
5,376 KB |
testcase_03 | AC | 2 ms
5,376 KB |
testcase_04 | AC | 1 ms
5,376 KB |
testcase_05 | AC | 1 ms
5,376 KB |
testcase_06 | AC | 1 ms
5,376 KB |
testcase_07 | AC | 1 ms
5,376 KB |
testcase_08 | AC | 2 ms
5,376 KB |
testcase_09 | AC | 2 ms
5,376 KB |
testcase_10 | AC | 11 ms
5,376 KB |
testcase_11 | AC | 9 ms
5,376 KB |
testcase_12 | AC | 39 ms
12,288 KB |
testcase_13 | AC | 25 ms
9,472 KB |
testcase_14 | AC | 36 ms
11,776 KB |
testcase_15 | AC | 40 ms
12,416 KB |
testcase_16 | AC | 14 ms
5,760 KB |
testcase_17 | AC | 33 ms
11,136 KB |
testcase_18 | AC | 4 ms
5,376 KB |
testcase_19 | AC | 11 ms
5,376 KB |
testcase_20 | AC | 39 ms
11,008 KB |
testcase_21 | AC | 20 ms
7,040 KB |
testcase_22 | AC | 45 ms
14,080 KB |
testcase_23 | AC | 46 ms
14,080 KB |
testcase_24 | AC | 17 ms
8,320 KB |
testcase_25 | AC | 25 ms
11,520 KB |
testcase_26 | AC | 11 ms
6,016 KB |
testcase_27 | AC | 32 ms
15,232 KB |
testcase_28 | AC | 2 ms
5,376 KB |
コンパイルメッセージ
warning: fields `n`, `x`, and `y` are never read --> src/main.rs:66:5 | 65 | struct Diameter { | -------- fields in this struct 66 | n: usize, | ^ 67 | edges: Vec<Vec<(usize, i64)>>, 68 | x: usize, | ^ 69 | y: usize, | ^ | = note: `#[warn(dead_code)]` on by default warning: methods `dfs`, `diameter`, and `farthest` are never used --> src/main.rs:73:8 | 72 | impl Diameter { | ------------- methods in this implementation 73 | fn dfs(&self, v: usize, dist: &mut [i64], p: Option<usize>, d: i64) { | ^^^ ... 92 | pub fn diameter(&mut self) -> (usize, usize) { | ^^^^^^^^ ... 118 | pub fn farthest(&mut self) -> Vec<i64> { | ^^^^^^^^
ソースコード
#[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") }; } /** * Manages a tree and calculates the diameter of it. * Verified by: NJPC 2017-E * (http://njpc2017.contest.atcoder.jp/submissions/1089492) */ struct Diameter { n: usize, edges: Vec<Vec<(usize, i64)>>, x: usize, y: usize, } impl Diameter { fn dfs(&self, v: usize, dist: &mut [i64], p: Option<usize>, d: i64) { dist[v] = d; for &(w, c) in self.edges[v].iter() { if Some(w) == p { continue; } self.dfs(w, dist, Some(v), d + c); } } pub fn new(n: usize) -> Self { Diameter { n: n, edges: vec![Vec::new(); n], x: n, y: n, } } pub fn add_edge(&mut self, a: usize, b: usize, c: i64) { self.edges[a].push((b, c)); self.edges[b].push((a, c)); } pub fn diameter(&mut self) -> (usize, usize) { let n = self.n; if self.x < n { return (self.x, self.y); } // farthest from 0 let mut dist = vec![-1; n]; self.dfs(0, &mut dist, None, 0); let mut maxi = 0; for i in 1 .. n { if dist[maxi] < dist[i] { maxi = i; } } self.x = maxi; // farthest from x self.dfs(maxi, &mut dist, None, 0); let mut maxi = 0; for i in 0 .. n { if dist[maxi] < dist[i] { maxi = i; } } self.y = maxi; (self.x, self.y) } pub fn farthest(&mut self) -> Vec<i64> { let n = self.n; if self.x >= n { self.diameter(); } let mut ret = vec![0; n]; let mut tmp = vec![-1; n]; /* For every vertex, the farthest point from it is either x or y. */ self.dfs(self.x, &mut ret, None, 0); self.dfs(self.y, &mut tmp, None, 0); for i in 0 .. n { ret[i] = max(ret[i], tmp[i]); } ret } } fn solve() { let out = std::io::stdout(); let mut out = BufWriter::new(out.lock()); macro_rules! puts { ($($format:tt)*) => (write!(out,$($format)*).unwrap()); } input! { n: usize, ab: [(usize1, usize1); n - 1], } let mut diam = Diameter::new(n); let mut deg = vec![0; n]; for (a, b) in ab { diam.add_edge(a, b, 1); deg[a] += 1; deg[b] += 1; } let mut tot = 0; for i in 0..n { if deg[i] >= 2 { tot += deg[i] - 2; } } puts!("{}\n", tot); } 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(); }