Cow at Large (Platinum)
Time limit4sMemory limit512 MB
In a tree, for each barn find the minimum number of farmers placed at exits needed to catch Bessie, who starts there and runs for the nearest exit at equal speed.
- Level
Hard8 of 10
- Topics
- Tree, DFS, Greedy, Dynamic programming
- Solved
- No attempts yet
Problem
Cornered at last, Bessie has gone to ground in a remote farm. The farm has barns () and bidirectional tunnels between barns, so there is exactly one path between every pair of barns. A barn with only one tunnel is an exit.
When morning comes, Bessie surfaces at some barn and tries to reach an exit. The moment she surfaces, the law pinpoints her location. Farmers then start at various exit barns and try to catch her. The farmers move at the same speed as Bessie, so in each time step each farmer can move from one barn to an adjacent barn, and so can Bessie. The farmers always know where Bessie is, and Bessie always knows where the farmers are. The farmers catch Bessie if at any instant a farmer is in the same barn as Bessie, or crossing the same tunnel as Bessie. Bessie escapes if she reaches an exit barn strictly before any farmer catches her.
Bessie has not decided where to surface. For each of the barns, find the minimum number of farmers needed to catch Bessie if she surfaced there, assuming the farmers distribute themselves optimally among the exit barns.
Input
The first line contains . Each of the next lines contains two integers between and , describing a tunnel between those two barns.
Output
Print lines. The th line holds the minimum number of farmers needed to catch Bessie if she surfaced at barn .