output_description: Print N lines. The i-th line must contain the niceness when the headquarters
  are set up in Vertex i.
  
  * * *
input_description: Input is given from Standard Input in the following format:
  
      
      
      N
      a_1 b_1 c_1
      :
      a_{N-1} b_{N-1} c_{N-1}
attempt: import sys
  
  sys.setrecursionlimit(10**7)
  
  
  def solve():
      n = int(sys.stdin.readline())
      adj = [[] for i in range(n + 1)]
  
      dp = [dict() for i in range(n + 1)]
      cnt = [dict() for i in range(n + 1)]
  
      for i in range(n - 1):
          ai, bi, ci = map(int, sys.stdin.readline().split())
  
          if ci > 2:
              return
  
          adj[ai].append((bi, ci))
          adj[bi].append((ai, ci))
  
      def cnt_node(v, p):
          if p in cnt[v]:
              return cnt[v][p]
  
          cnt[v][p] = 1
  
          for u, c in adj[v]:
              if u == p:
                  continue
  
              cnt[v][p] += cnt_node(u, v)
  
          return cnt[v][p]
  
      def dfs(v, p):
          if p in dp[v]:
              return dp[v][p]
  
          dp[v][p] = 0
  
          for u, c in adj[v]:
              if u == p:
                  continue
  
              if c == 1:
                  dp[v][p] += cnt_node(u, v)
              else:
                  dp[v][p] += dfs(u, v) + 2
  
          return dp[v][p]
  
      for i in range(1, n + 1):
          # print(dp)
          # print(cnt)
          ans = dfs(i, -1)
          print(ans)
  
  
  if __name__ == "__main__":
      solve()
  
problem_description: Statement
  
  Snuke Festival 2017 will be held in a tree with N vertices numbered 1,2,
  ...,N. The i-th edge connects Vertex a_i and b_i, and has _joyfulness_ c_i.
  
  The staff is Snuke and N-1 black cats. Snuke will set up the headquarters in
  some vertex, and from there he will deploy a cat to each of the other N-1
  vertices.
  
  For each vertex, calculate the _niceness_ when the headquarters are set up in
  that vertex. The niceness when the headquarters are set up in Vertex i is
  calculated as follows:
  
    * Let X=0.
    * For each integer j between 1 and N (inclusive) except i, do the following:
      * Add c to X, where c is the smallest joyfulness of an edge on the path from Vertex i to Vertex j.
    * The niceness is the final value of X.
samples: [{"input": "3\n    1 2 10\n    2 3 20", "output": "20\n    30\n    30\n    \n\n  * The figure below shows the case when headquarters are set up in each of the vertices 1, 2 and 3.\n  * The number on top of an edge denotes the joyfulness of the edge, and the number below an vertex denotes the smallest joyfulness of an edge on the path from the headquarters to that vertex.\n\n![1ee10aa2a1bf5e43e05161f37d88bdc1.png](https://atcoder.jp/img/asaporo2/1ee10aa2a1bf5e43e05161f37d88bdc1.png)\n\n* * *"}, {"input": "15\n    6 3 2\n    13 3 1\n    1 13 2\n    7 1 2\n    8 1 1\n    2 8 2\n    2 12 2\n    5 2 2\n    2 11 2\n    10 2 2\n    10 9 1\n    9 14 2\n    4 14 1\n    11 15 2", "output": "16\n    20\n    15\n    14\n    20\n    15\n    16\n    20\n    15\n    20\n    20\n    20\n    16\n    15\n    20\n    \n\n* * *"}, {"input": "19\n    19 14 48\n    11 19 23\n    17 14 30\n    7 11 15\n    2 19 15\n    2 18 21\n    19 10 43\n    12 11 25\n    3 11 4\n    5 19 50\n    4 11 19\n    9 12 29\n    14 13 3\n    14 6 12\n    14 15 14\n    5 1 6\n    8 18 13\n    7 16 14", "output": "103\n    237\n    71\n    263\n    370\n    193\n    231\n    207\n    299\n    358\n    295\n    299\n    54\n    368\n    220\n    220\n    319\n    237\n    370"}]