Back To School '19: Heist Night

View as PDF

Submit solution

Points: 15 (partial)
Time limit: 1.0s
Java 1.8s
Memory limit: 256M

Problem types

The famous thief Matthew is robbing a jewellery store and has his hands on N jewels numbered from 1 to N. These jewels have all been attached together into one heavy clump of N jewels with N-1 titanium wires to prevent anyone from stealing them all at once.

Fortunately, the thief has a specialized device that lets him separate exactly one jewel from the rest by cutting the connected wires. Through this process, the thief will split the heavy clump of jewels into some number of lighter clumps, which allows him to steal the rest of the jewels with the wires intact.

Out of each lighter clump, he will make exactly one beautiful necklace. A segment of the clump is a path where every jewel is used at most once. The thief values the beauty of the clump (and thus, the beauty of the necklace) as the number of jewels in the longest segment of jewels in that clump. Note that the longest segment may be the entire clump, in which case the thief will value the clump as the number of jewels in the clump.

Given the structure of the original clump of jewels, can you determine the k^\text{th} most beautiful necklace given that the thief separated jewel j? The thief is curious, and will ask Q of these questions.

Input Specification

The first line will contain two integers, N, Q (1 \le N, Q \le 10^5), the number of jewels, and the number of questions the thief will ask.

The next N-1 lines will each contain two integers, u_i, v_i (1 \le u_i, v_i \le N), indicating that jewels u_i and v_i are connected by a titanium wire. It is guaranteed that all the jewels are attached together with titanium wires.

The next Q lines will each contain two integers, j, k (1 \le j, k \le N).

Output Specification

For each question, print out one integer on its own line: The beauty of the k^\text{th} most beautiful necklace if the thief separated jewel j. If there are less than k necklaces that are made if jewel j is separated, output -1.


Subtask 1 [5%]

N, Q \le 10^3

k = 1

Subtask 2 [30%]

k = 1

Subtask 3 [65%]

No additional constraints.

Sample Input 1

4 3
1 2
2 3
2 4
2 1
3 1
4 1

Sample Output 1


Sample Input 2

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

Sample Output 2



There are no comments at the moment.