#![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::>()};($iter:expr, chars) => {read_value!($iter, String).chars().collect::>()};($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::>()};($next:expr, chars) => {read_value!($next, String).chars().collect::>()};($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() -> Vec { 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> } 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{ 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(); }