use proconio::{fastout, input, marker::Usize1}; const DIRS: [(isize, isize); 4] = [(0, 1), (1, 0), (0, -1), (-1, 0)]; trait OrdExt { fn chmax(&mut self, other: Self) -> bool; fn chmin(&mut self, other: Self) -> bool; } impl OrdExt for T { fn chmax(&mut self, other: Self) -> bool { if *self < other { *self = other; true } else { false } } fn chmin(&mut self, other: Self) -> bool { if *self > other { *self = other; true } else { false } } } #[fastout] fn main() { input! { N: usize, edges: [(Usize1, Usize1, isize); N-1], } let mut graph = vec![vec![]; N]; for (a, b, c) in edges { graph[a].push((b, c)); graph[b].push((a, c)); } println!("{}", rec(&graph, 0, N).1); } fn rec(graph: &Vec>, v: usize, p: usize) -> (isize, isize) { let mut paths = vec![0, 0]; let mut max2 = 0; for &(w, d) in &graph[v] { if w == p { continue; } let (d1, d2) = rec(graph, w, v); paths.push(d1 + d); max2.chmax(d2); } paths.sort_by(|a, b| b.cmp(a)); max2.chmax(paths[0] + paths[1]); (paths[0], max2) }