Jiry Matchings

시간 제한6초메모리 제한512 MB

요약
가중치가 있는 트리에서 각 k=1부터 n-1까지 정확히 k개의 간선을 고르는 매칭의 최대 총 가중치를 구하고, 불가능하면 "?"를 출력한다.
난이도

어려움10점 중 9점

유형
트리, 동적 계획법, 분할 정복, 그리디
정답자
아직 제출이 없습니다

문제

Ruyi Ji has a tree where the vertices are numbered by integers from 1 to n and each edge has a weight.

For each k ≤ (n − 1), he asked you to find the largest total weight of a matching with k edges if it exists.

입력

The first line of input contains one integer n: the number of vertices in the tree (2 ≤ n ≤ 200 000).

Each of the next n−1 lines contains three integers ui, vi, wi, describing an edge from ui to vi with weight wi in the tree (1 ≤ ui, vi ≤ n, ui ≠ vi, −109 ≤ wi ≤ 109).

It is guaranteed that the given graph is a tree.

출력

Output n − 1 integers: the largest weights of the matchings with 1, 2, . . . , n − 1 edges. If there is no such matching for the current k, print “?” instead.

힌트

In the first sample, with k = 1 you should take edge (2, 3) with weight 5. And with k = 2 you should take two edges, (2, 4) and (3, 5), with total weight 6. There are no matchings with a greater number of edges

예제3

  1. 예제 1

    입력
    5
    1 2 3
    2 3 5
    2 4 4
    3 5 2
    
    예상 출력
    5 6 ? ?
    
  2. 예제 2

    입력
    10
    2 8 -5
    5 10 5
    3 4 -5
    1 6 5
    3 9 5
    1 7 -3
    4 8 -5
    10 8 -5
    1 8 -3
    
    예상 출력
    5 10 15 10 ? ? ? ? ?
    
  3. 예제 3

    입력
    2
    1 2 35
    
    예상 출력
    35