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 Python3 (judge verdict: Accepted)

# -*- coding: utf-8 -*-

import sys
import os
import pprint
from queue import Queue

"""Diameter of Tree"""

#fd = os.open('GRL_5_A.txt', os.O_RDONLY)
#os.dup2(fd, sys.stdin.fileno())


class Edge:
    def __init__(self, to, weight):
        self.to = to
        self.weight = weight

    def __str__(self):
        return '{} {}'.format(self.to, self.weight)


N = int(input())
G = [[] for i in range(N)]
for i in range(N-1):
    start, end, weight = list(map(int, input().split()))

    # ??????
    edge = Edge(end, weight)
    G[start].append(edge)

    # ??°???
    edge = Edge(start, weight)
    G[end].append(edge)


# ???i??????1????????????????±???????
def bfs(index):
    d = [float('inf')] * N
    visited = [False] * N
    queue = Queue()
    queue.put(index)
    d[index] = 0

    while not queue.empty():
        u = queue.get()
        visited[u] = True

        for edge in G[u]:
            if d[u] + edge.weight < d[edge.to]:
                d[edge.to] = d[u] + edge.weight
            if not visited[edge.to]:
                queue.put(edge.to)

    max_i = 0
    max_d = 0
    for i in range(N):
        if max_d < d[i]:
            max_i = i
            max_d = d[i]
    #print('farthest i {} d {}'.format(max_i, max_d))
    return max_i, max_d

# ?§???????????????§????????????????????????1???????????????????????????
temp_i, temp_d = bfs(0)
# ?¬????????????????1???????????????????????????
temp_i, temp_d = bfs(temp_i)

# diameter
print(temp_d)