On a tree of N barns, find the minimum number of farmers placed at exits so they catch Bessie, who starts at node K and runs to any exit.
Hard8TreeBFSDFSNo attempts yetTime limit2sMemory limit512 MBBessie is cornered at last and has gone to ground in a remote farm. The farm has N barns (2≤N≤105) and N−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 Bessie surfaces, the law pinpoints her location. Several farmers then start at exit barns and try to catch her. The farmers move at the same speed as Bessie, so in each time step every farmer moves from one barn to an adjacent barn. The farmers know where Bessie is at all times, and Bessie knows where the farmers are at all times. The farmers catch Bessie if at any instant a farmer is in the same barn as Bessie, or is crossing the same tunnel as Bessie. Bessie escapes if she reaches an exit barn before any farmer catches her. Bessie may surface at an exit barn. In that case a farmer who starts at that barn is in the same barn as Bessie the instant she surfaces, so she is caught.
Whether Bessie gets away depends on the number of farmers the law is able to deploy. Bessie surfaces at barn K. Find the minimum number of farmers needed to catch her, assuming the farmers distribute themselves optimally among the exit barns.
The first line contains N and K (1≤K≤N). Each of the following N−1 lines contains two integers in the range 1…N, describing one tunnel between two barns.
Output the minimum number of farmers needed to be sure of catching Bessie.