Problem:
Diameter of a Tree

Given a tree T with non-negative weight, find the diameter of the tree.

The diameter of a tree is the maximum distance between two nodes in a tree.

Input

n
s1 t1 w1
s2 t2 w2
:
sn-1 tn-1 wn-1

The first line consists of an integer n which represents the number of nodes in the tree. Every node has a unique ID from 0 to n-1 respectively.

In the following n-1 lines, edges of the tree are given.

si and ti represent end-points of the i-th edge (undirected) and wi represents the weight (distance) of the i-th edge.

Output

Print the diameter of the tree in a line.

Constraints

1 ≤ n ≤ 100,000

0 ≤ wi ≤ 1,000

Sample Input 1

4
0 1 2
1 2 1
1 3 3

Sample Output 1

5

Sample Input 2

4
0 1 1
1 2 2
2 3 4

Sample Output 2

7

Solution in Rust (judge verdict: Accepted)

macro_rules ! input { ( source = $ s : expr , $ ( $ r : tt ) * ) => { let mut iter = $ s . split_whitespace ( ) ; input_inner ! { iter , $ ( $ r ) * } } ; ( iter = $ iter : ident , $ ( $ r : tt ) * ) => { let 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 ) * } } ; ( $ ( $ r : tt ) * ) => { let 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 , mut $ var : ident : $ t : tt $ ( $ r : tt ) * ) => { let mut $ var = read_value ! ( $ iter , $ t ) ; input_inner ! { $ iter $ ( $ r ) * } } ; ( $ iter : expr , mut $ var : ident $ ( $ r : tt ) * ) => { input_inner ! { $ iter , mut $ var : usize $ ( $ r ) * } } ; ( $ iter : expr , $ var : ident : $ t : tt $ ( $ r : tt ) * ) => { let $ var = read_value ! ( $ iter , $ t ) ; input_inner ! { $ iter $ ( $ r ) * } } ; ( $ iter : expr , $ var : ident $ ( $ r : tt ) * ) => { input_inner ! { $ iter , $ var : usize $ ( $ 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 : $ base : expr } ) => { read_value ! ( $ iter , String ) . chars ( ) . map ( | c | ( c as u8 - $ base as u8 ) as usize ) . collect ::< Vec < usize >> ( ) } ; ( $ iter : expr , { char : $ base : expr } ) => { read_value ! ( $ iter , { chars : $ base } ) [ 0 ] } ; ( $ iter : expr , chars ) => { read_value ! ( $ iter , String ) . chars ( ) . collect ::< Vec < char >> ( ) } ; ( $ iter : expr , char ) => { read_value ! ( $ iter , chars ) [ 0 ] } ; ( $ iter : expr , usize1 ) => { read_value ! ( $ iter , usize ) - 1 } ; ( $ iter : expr , $ t : ty ) => { $ iter . next ( ) . unwrap ( ) . parse ::<$ t > ( ) . unwrap ( ) } ; }
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct Adjacent {
    pub to: usize,
    pub id: usize,
}
impl Adjacent {
    pub fn new(to: usize, id: usize) -> Adjacent {
        Adjacent { to: to, id: id }
    }
}
#[derive(Clone, Debug, Default)]
pub struct Graph {
    pub vsize: usize,
    pub esize: usize,
    pub graph: Vec<Vec<Adjacent>>,
}
impl Graph {
    pub fn new(vsize: usize) -> Graph {
        Graph {
            vsize: vsize,
            esize: 0,
            graph: vec![vec![]; vsize],
        }
    }
    pub fn add_edge(&mut self, from: usize, to: usize) {
        self.graph[from].push(Adjacent::new(to, self.esize));
        self.esize += 1;
    }
    pub fn add_undirected_edge(&mut self, u: usize, v: usize) {
        self.graph[u].push(Adjacent::new(v, self.esize));
        self.graph[v].push(Adjacent::new(u, self.esize));
        self.esize += 1;
    }
    pub fn vertexes(&self) -> std::ops::Range<usize> {
        0..self.vsize
    }
    pub fn adjacency(&self, from: usize) -> &Vec<Adjacent> {
        &self.graph[from]
    }
}
pub trait Monoid {
    type T: Clone + PartialEq;
    fn mempty(&self) -> Self::T;
    fn mappend(&self, x: &Self::T, y: &Self::T) -> Self::T;
}
#[derive(Clone, Debug)]
pub struct ReRooting<M: Monoid, F: Fn(&M::T, usize, Option<usize>) -> M::T> {
    n: usize,
    monoid: M,
    dp: Vec<M::T>,
    ep: Vec<M::T>,
    rooting: F,
}
impl<M: Monoid, F: Fn(&M::T, usize, Option<usize>) -> M::T> ReRooting<M, F> {
    pub fn new(n: usize, monoid: M, rooting: F) -> Self {
        let dp = vec![monoid.mempty(); n];
        let ep = vec![monoid.mempty(); n * 2];
        ReRooting {
            n: n,
            monoid: monoid,
            dp: dp,
            ep: ep,
            rooting: rooting,
        }
    }
}
impl<M: Monoid, F: Fn(&M::T, usize, Option<usize>) -> M::T> ReRooting<M, F> {
    #[inline]
    pub fn eidx(&self, u: usize, a: &Adjacent) -> usize {
        a.id + (self.n - 1) * (u > a.to) as usize
    }
    #[inline]
    pub fn reidx(&self, u: usize, a: &Adjacent) -> usize {
        a.id + (self.n - 1) * (u < a.to) as usize
    }
    #[inline]
    pub fn merge(&self, x: &M::T, y: &M::T) -> M::T {
        self.monoid.mappend(x, y)
    }
    #[inline]
    pub fn add_subroot(&self, x: &M::T, vid: usize, eid: usize) -> M::T {
        (self.rooting)(x, vid, Some(eid))
    }
    #[inline]
    pub fn add_root(&self, x: &M::T, vid: usize) -> M::T {
        (self.rooting)(x, vid, None)
    }
    pub fn dfs(&mut self, pa: &Adjacent, p: usize, graph: &Graph) {
        let u = pa.to;
        let pi = self.eidx(p, pa);
        for a in graph.adjacency(u) {
            let i = self.eidx(u, a);
            if a.to != p {
                self.dfs(a, u, graph);
                self.ep[pi] = self.merge(&self.ep[pi], &self.ep[i]);
            }
        }
        self.ep[pi] = self.add_subroot(&self.ep[pi], u, pa.id);
    }
    pub fn efs(&mut self, u: usize, p: usize, graph: &Graph) {
        let adjacency = graph.adjacency(u);
        let m = adjacency.len();
        let mut left = vec![self.monoid.mempty(); m + 1];
        let mut right = vec![self.monoid.mempty(); m + 1];
        for (k, a) in adjacency.iter().enumerate() {
            let i = self.eidx(u, a);
            left[k + 1] = self.merge(&left[k], &self.ep[i]);
        }
        for (k, a) in adjacency.iter().enumerate().rev() {
            let i = self.eidx(u, a);
            right[k] = self.merge(&right[k + 1], &self.ep[i]);
        }
        self.dp[u] = self.add_root(&left[m], u);
        for (k, a) in adjacency.iter().enumerate() {
            if a.to != p {
                let i = self.reidx(u, a);
                self.ep[i] = self.merge(&left[k], &right[k + 1]);
                self.ep[i] = self.add_subroot(&self.ep[i], u, a.id);
                self.efs(a.to, u, graph);
            }
        }
    }
    pub fn rerooting(&mut self, graph: &Graph) {
        let n = self.n;
        for a in graph.adjacency(0) {
            self.dfs(a, 0, graph);
        }
        self.efs(0, n, graph);
    }
}
#[derive(Clone, Debug)]
pub struct MaxMonoid<T: Ord + Clone> {
    min: T,
}
impl<T: Ord + Clone> MaxMonoid<T> {
    pub fn new(min: T) -> MaxMonoid<T> {
        MaxMonoid { min: min }
    }
}
impl<T: Ord + Clone> Monoid for MaxMonoid<T> {
    type T = T;
    #[inline]
    fn mempty(&self) -> T {
        self.min.clone()
    }
    #[inline]
    fn mappend(&self, x: &T, y: &T) -> T {
        std::cmp::max(x, y).clone()
    }
}
fn main() {
    input! { n, edges: [(usize, usize, usize); n-1] };
    let mut g = Graph::new(n);
    for &(u, v, _) in &edges {
        g.add_undirected_edge(u, v);
    }
    let mut r = ReRooting::new(n, MaxMonoid::new(0usize), |&x, _, ei| {
        x + ei.map(|i| edges[i].2).unwrap_or(0)
    });
    r.rerooting(&g);
    let ans = r.dp.into_iter().max().unwrap_or(0);
    println!("{}", ans);
}