Berland Football Union
InterviewTime limit2sMemory limit1024 MB
Given a weighted tree, find every vertex where the sum of squared distances to all other vertices is minimal.
- Level
Medium6 of 10
- Topics
- Tree, Dynamic programming, DFS
- Solved
- No attempts yet
Statement
The third division of the Berland football championship has teams from different cities. Teams travel only along special roads built by the Berland Football Union. Each road connects two different cities and can be used in both directions. Each road has a length in kilometers.
Any two cities are connected by exactly one path made of special roads that visits each city at most once. Officials and experts also travel only along these roads.
The union wants to build its headquarters in one of these cities. Each month, experts leave the headquarters, one for each city, to check the quality of fields and stadiums. An expert who travels to a city kilometers from the headquarters receives a bonus of Berland rubles.
The union head is very thrifty. He wants the headquarters in a city where the total bonus paid to the experts is minimal. Output the list of candidate cities where the headquarters can be built.
Input
The first line contains an integer (), the number of cities. Each of the next lines contains three integers , , and (, , ): cities and are connected by a road of length kilometers.
Output
Print the number of candidate cities that satisfy the condition on the first line. On the second line, print the city numbers separated by spaces, in any order.