This page is still under construction.

Parts of this page are still being built. What you see may change.

Cow at Large (Platinum)

Time limit4sMemory limit512 MB

Summary
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 NN barns (2≤N≤7×1042 \leq N \leq 7 \times 10^4) and N−1N-1 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 NN 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 NN. Each of the next N−1N-1 lines contains two integers between 11 and NN, describing a tunnel between those two barns.

Output

Print NN lines. The iith line holds the minimum number of farmers needed to catch Bessie if she surfaced at barn ii.

Examples1

  1. Example 1

    Input
    7
    1 2
    1 3
    3 4
    3 5
    4 6
    5 7
    
    Expected output
    3
    1
    3
    3
    3
    1
    1