Dongwoo, is his heart kind or cruel?
InterviewTime limit2sMemory limit1024 MB
Given a weighted tree of N villages with signed edge costs, find the minimum and maximum total path cost from one house point (on a village or along a road) to all villages.
- Level
Medium5 of 10
- Topics
- Tree, DFS, Dynamic programming
- Solved
- No attempts yet
Input and output
The Korean club MatKor in the Cyber Defense department at Korea University invites one outside lecturer to hold a seminar on a regular basis. Dongwoo has run several MatKor seminars and found that when he explains something to the whole group, most members stop paying attention, so he has to explain things to each person one by one. From this semester on, the seminars are one-on-one. Dongwoo also found that a single seminar takes a great deal of effort from the lecturer, so for the lecturer's convenience he decided to hold only one seminar per day.
MatKor has members, and each member lives in a different village. There are roads connecting the villages, and there is exactly one path between any two villages. Each road can be traveled in both directions. Whoever travels a road once has their hardship increased by . Some villages are unusual, so can be or a negative integer, and then the hardship goes down.
Dongwoo wants to build a new house for the lecturer. The house can be built on an existing village or on a point along a road. If the house is built on a road, the hardship increase from that point to each of the two end villages is proportional to the distance and to the road's value. For example, suppose a road between village 1 and village 2 increases hardship by 5 when traveled from one end to the other. If the house is built at the point that divides the road in the ratio , the hardship increase from that point to village 1 is , and the increase to village 2 is .
Each day, the lecturer goes to the village of one member, holds a one-on-one seminar, and returns to the house. On the way back, the hardship does not change, no matter which roads were used. This is done once for every member. The lecturer uses a route from the house to each village that never uses the same road twice. Moving within the same village does not change the hardship.
Dongwoo may be kind or unkind, so he wants to place the house at a point that minimizes or maximizes the total hardship the lecturer feels. Find the minimum and maximum hardship the lecturer feels during the seminars.
Input
The first line contains the integer (), the number of members.
From the second line to the -th line, each line gives information about a road. Each line contains two distinct integers () and (), the end villages of the road, and an integer (), the change in hardship when the road is traveled. These are separated by spaces.
Output
Print the minimum and maximum hardship the lecturer feels during the seminars, separated by a space.
If the answer is an integer, print it as is. If the answer is a rational number that is not an integer, reduce it to an irreducible fraction (, , ) and print it in the form /.
It can be proven that the answer is always rational.
Hint
Dongwoo is kind. That is the accepted view.