output_description: Print the diameter of the tree in a line.
input_description: 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.
attempt: import sys
  
  input = sys.stdin.readline
  
  
  class Tree:
      def __init__(self, n, edge):
          self.n = n
          self.tree = [[] for _ in range(n)]
          for e in edge:
              self.tree[e[0]].append((e[1], e[2]))
              self.tree[e[1]].append((e[0], e[2]))
  
      def setroot(self, root):
          self.root = root
          self.parent = [None for _ in range(self.n)]
          self.parent[root] = -1
          self.depth = [None for _ in range(self.n)]
          self.depth[root] = 0
          self.distance = [None for _ in range(self.n)]
          self.distance[root] = 0
          stack = [root]
          while stack:
              node = stack.pop()
              for adj, cost in self.tree[node]:
                  if self.parent[adj] is None:
                      self.parent[adj] = node
                      self.depth[adj] = self.depth[node] + 1
                      self.distance[adj] = self.distance[node] + cost
                      stack.append(adj)
  
      def diam(self):
          u = self.distance.index(max(self.distance))
          dist_u = [None for _ in range(N)]
          dist_u[u] = 0
          stack = [u]
          while stack:
              node = stack.pop()
              for adj, cost in self.tree[node]:
                  if dist_u[adj] is None:
                      dist_u[adj] = dist_u[node] + cost
                      stack.append(adj)
          return max(dist_u)
  
  
  N = int(input())
  E = [list(map(int, input().split())) for _ in range(N - 1)]
  
  t = Tree(N, E)
  t.setroot(0)
  
  print(t.diam())
  
problem_description: 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.
samples: [{"input": "4\n    0 1 2\n    1 2 1\n    1 3 3", "output": "5"}, {"input": "4\n    0 1 1\n    1 2 2\n    2 3 4", "output": "7"}]