PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#SRD0000146. Bò Bessie bị bao vây (Cow at Large)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Bò Bessie bị bao vây (Cow at Large)

Cow at Large

Source: USACO

Version: Phuoc Hung OJ Extended

Problem Statement

A farm has nn barns connected by n−1n-1 bidirectional tunnels, forming a tree. Every barn with exactly one tunnel is an exit.

Bessie appears at a chosen barn ss and tries to reach an exit. Farmers immediately learn her location and may start simultaneously from exits of their choice. Both Bessie and the farmers move at the same speed, traversing one edge per time step, and each knows the other's position.

Bessie is caught if a farmer and she occupy the same barn at the same time or traverse the same tunnel in opposite directions. Otherwise she escapes if she reaches an exit without being caught.

For each starting barn s=1,…,ns=1,\ldots,n, determine the minimum number of farmers who can guarantee capture, with optimal play.

Input

  • The first line contains nn.
  • Each of the next n−1n-1 lines contains u,vu,v describing a bidirectional tunnel. Barns use one-based indices.

Output

Print nn lines. Line ss is the minimum number of farmers required if Bessie starts at barn ss.

Subtasks

  • Subtask 1 (20 points): 2≤n≤1002\le n\le 100; n−1n-1 undirected edges; vertices in [1,n][1,n]; the graph is connected and acyclic.
  • Subtask 2 (30 points): 2≤n≤20002\le n\le 2000; n−1n-1 undirected edges; vertices in [1,n][1,n]; the graph is connected and acyclic.
  • Subtask 3 (50 points): 2≤n≤700002\le n\le 70000; n−1n-1 undirected edges; vertices in [1,n][1,n]; the graph is connected and acyclic.

Examples

Example 1

Input

7
1 2
1 3
3 4
3 5
4 6
5 7

Output

3
1
3
3
3
1
1

Explanation

Barns 2,6,72,6,7 are exits; a farmer at the same exit catches Bessie immediately, so the corresponding outputs are 11. Starting at barn 11, the three escape branches toward leaves 2,6,72,6,7 require three intercepting farmers, one at each exit. The same minimum 33 holds for starts at barns 3,4,53,4,5.