Jiry Matchings
Time limit6sMemory limit512 MB
Given a weighted tree, find the maximum total weight of a matching with k edges for each k from 1 to n-1, or print ? if none exists.
- Level
Hard8 of 10
- Topics
- Tree, Dynamic programming, Heap
- Solved
- No attempts yet
Problem
Ruyi Ji has a tree with vertices, numbered from to . Each edge has a weight.
For each , find the largest total weight of a matching with edges. If no matching with edges exists, print "?" instead.
Input
The first line contains one integer (), the number of vertices.
Each of the next lines contains three integers , , (, , ), describing an edge between and with weight . The given graph is guaranteed to be a tree.
Output
Print integers: the largest total weight of a matching with edges. Print "?" for each that has no such matching.
Hint
In the first sample, with , take the edge with weight . With , take the two edges and , for a total weight of . No matching has more edges than that.