Yanggallae Rescue
InterviewTime limit1sMemory limit1024 MB
Given a tree rooted at room 1, find the minimum total thickness of vines to cut so that no leaf room stays connected to room 1.
- Level
Medium4 of 10
- Topics
- Tree, Dynamic programming, DFS
- Solved
- No attempts yet
Problem
The peaceful village of Yanggallae is in danger! Poison is spreading through the vines and moving toward the room where Yanggallae is.
The poison spreads from every room except room 1 that is connected to exactly one vine.
There is exactly one path between any pair of rooms, and there are vines in total. Each end of a vine is strongly attached to its room, so the vines must be cut to keep the poison from reaching the room where Yanggallae is.
Cutting a vine takes effort equal to its thickness . Shiyeon wants to cut the vines using as little total effort as possible.
Find the minimum total effort needed to cut the vines.
Input
The first line contains the number of rooms . ()
Each of the next lines contains three integers , , and . (, )
This means the vine connecting room and room has thickness .
The room where Yanggallae is tied is numbered .
Output
Print the minimum total effort needed to cut the vines.
Hint

In the figure, cutting the vine between 1 and 2, the vine between 3 and 6, and the vine between 3 and 7 gives the minimum total of 3+1+2 = 6.