結果
問題 | No.806 木を道に |
ユーザー | kei |
提出日時 | 2019-08-28 21:18:04 |
言語 | Rust (1.77.0 + proconio) |
結果 |
WA
|
実行時間 | - |
コード長 | 5,045 bytes |
コンパイル時間 | 10,300 ms |
コンパイル使用メモリ | 401,092 KB |
実行使用メモリ | 20,412 KB |
最終ジャッジ日時 | 2024-11-17 16:33:39 |
合計ジャッジ時間 | 12,435 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
5,248 KB |
testcase_01 | AC | 1 ms
5,248 KB |
testcase_02 | WA | - |
testcase_03 | WA | - |
testcase_04 | AC | 1 ms
5,248 KB |
testcase_05 | WA | - |
testcase_06 | WA | - |
testcase_07 | AC | 1 ms
5,248 KB |
testcase_08 | AC | 0 ms
5,248 KB |
testcase_09 | AC | 1 ms
5,248 KB |
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 | WA | - |
testcase_23 | WA | - |
testcase_24 | AC | 15 ms
9,728 KB |
testcase_25 | AC | 23 ms
13,696 KB |
testcase_26 | AC | 15 ms
7,640 KB |
testcase_27 | AC | 49 ms
20,412 KB |
testcase_28 | AC | 2 ms
5,248 KB |
ソースコード
#![allow(unused_mut, non_snake_case, unused_imports, dead_code,unused_macros)] use std::iter; use std::cmp::{max, min, Ordering}; use std::mem::swap; use std::collections::{HashMap, BTreeMap, HashSet, BTreeSet, BinaryHeap, VecDeque}; use std::iter::FromIterator; // 高速 EOF要 //macro_rules! input {(source = $s:expr, $($r:tt)*) => {let mut iter = $s.split_whitespace();input_inner!{iter, $($r)*}};($($r:tt)*) => {let mut s = {use std::io::Read;let mut s = String::new();std::io::stdin().read_to_string(&mut s).unwrap();s};let mut iter = s.split_whitespace();input_inner!{iter, $($r)*}};} //macro_rules! input_inner {($iter:expr) => {};($iter:expr, ) => {};($iter:expr, $var:ident : $t:tt $($r:tt)*) => {let $var = read_value!($iter, $t);input_inner!{$iter $($r)*}};} //macro_rules! read_value {($iter:expr, ( $($t:tt),* )) => {( $(read_value!($iter, $t)),* )};($iter:expr, [ $t:tt ; $len:expr ]) => {(0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()};($iter:expr, chars) => {read_value!($iter, String).chars().collect::<Vec<char>>()};($iter:expr, usize1) => {read_value!($iter, usize) - 1};($iter:expr, $t:ty) => {$iter.next().unwrap().parse::<$t>().expect("Parse error")};} // 低速 EOF不要 macro_rules! input {(source = $s:expr, $($r:tt)*) => {let mut iter = $s.split_whitespace();let mut next = || { iter.next().unwrap() };input_inner!{next, $($r)*}};($($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! mut_input {(source = $s:expr, $($r:tt)*) => {let mut iter = $s.split_whitespace();let mut next = || { iter.next().unwrap() };mut_input_inner!{next, $($r)*}};($($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()};mut_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! mut_input_inner {($next:expr) => {};($next:expr, ) => {};($next:expr, $var:ident : $t:tt $($r:tt)*) => {let mut $var = read_value!($next, $t);mut_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:ty) => {$next().parse::<$t>().expect("Parse error")};} // 非常時 fn read_line() -> String { let mut s = String::new(); std::io::stdin().read_line(&mut s).unwrap(); s.trim().to_string() } fn read_vec<T: std::str::FromStr>() -> Vec<T> { let mut s = String::new(); std::io::stdin().read_line(&mut s).ok(); s.trim().split_whitespace() .map(|e| e.parse().ok().unwrap()).collect() } #[derive(Copy, Clone)] pub struct Edge{ from:usize, to:usize, cost:i64 } #[derive(Clone)] pub struct Graph{ N:usize, Edges:Vec<Vec<Edge>> } impl Graph{ pub fn new(n:usize) -> Graph{ Graph{N:n,Edges:vec![vec![];n]} } pub fn add_edge(&mut self,u:usize,v:usize,cost:i64,undirected:bool){ self.Edges[u].push(Edge{from:u,to:v,cost}); if undirected { self.Edges[v].push(Edge{from:v,to:u,cost}) } } pub fn Dijkstra(&self,root:usize) -> Vec<i64>{ let mut dist = vec![i64::max_value() as i64;self.N]; dist[root] = 0; let mut pq = BinaryHeap::new(); pq.push((0_i64,root)); while !pq.is_empty(){ let s = pq.pop().unwrap(); let (d,n) = (-s.0,s.1); if dist[n] < d { continue; } for e in &self.Edges[n]{ if dist[e.to] > dist[e.from] + e.cost{ dist[e.to] = dist[e.from] + e.cost; pq.push((-dist[e.to],e.to)); } } } return dist; } } fn solve(){ input!(N:usize,E:[(usize,usize);N-1]); let E:Vec<(usize,usize)> = E; let N:usize = N; let mut G = Graph::new(N); for e in E{ G.add_edge(e.0-1,e.1-1,1,true); } let dist1 = G.Dijkstra(0); let s = dist1.into_iter().enumerate().max_by(|l,r| (*l).1.cmp(&r.1)).unwrap().0; let dist2 = G.Dijkstra(s); let distance = dist2.into_iter().max().unwrap(); println!("{}",N as i64 - 1 - distance); } fn main() { let stack_size = 104_857_600; // 100 MB let thd = std::thread::Builder::new().stack_size(stack_size); thd.spawn(|| solve()).unwrap().join().unwrap(); }