K-step Tree

Time limit: 0.8s Memory limit: 128MB Input: Output:

Task

The final round of IIOT is approaching and after solving so many tree problems during the previous edition of the contest, you start to wonder what the next tree problem is going to be. Your instincts were spot-on, finally here is a tree problem for you to solve!

You are given a tree graph with NN nodes, rooted at vertex 00.
We define the KK-th ancestor of a vertex as the node we reach if we move KK steps up the tree, in the direction of the root.
If we reach the root before doing KK steps, then we say that the KK-th ancestor does not exist.

For a fixed integer KK, your task is to find the KK-th ancestor of each node in the tree, or report that it doesn't exist.

Input data

The input file consists of:

  • a line containing integers NN, KK.
  • N1N - 1 lines, the ii-th of which consisting of integers XiX_{i}, YiY_{i}, representing an edge between nodes XiX_{i} and YiY_{i}.

Output data

The output file must contain a single line consisting of the NN integers, representing the KK-th ancestors of each node, or 1-1 if it does not exist.

Constraints and clarifications

  • 1K<N1061 \le K < N \le 10^6.
  • 0Xi,Yi<N0 \le X_i, Y_i < N and XiYiX_i \neq Y_i for each i=0N2i=0\ldots N-2.
# Score Constraints
1 0 Examples.
2 25 N2000N \le 2000.
3 16 N100000N \le 100\,000.
4 59 No additional limitations.

Example

stdin

11 2
0 3
1 2
0 4
0 1
2 6
4 5
5 7
2 8
8 9
8 10

stdout

-1 -1 0 -1 -1 0 1 4 1 2 2

Explanation

The tree in the sample test case is displayed in the following figure.

Log in or sign up to be able to send submissions!