QOJ.ac

QOJ

Time Limit: 1.0 s Memory Limit: 1024 MB Total points: 100 Hackable ✓

#18031. I am a Vampire

Statistics

"I'm a vampire, first come over here!"

Hatsune Pickle is a very famous vampire.

One day, for her research day, Pickle decided to turn everyone in the Science Research City (SRC) into vampires!

The SRC consists of $N$ laboratories and $N-1$ corridors connecting them, such that there is always a unique shortest path between any two laboratories. In other words, the SRC has a tree structure. Each laboratory is assigned a unique number from $1$ to $N$. Pickle is in laboratory $P$, and there is one student conducting research in every laboratory except for laboratory $P$.

To realize this plan, Pickle has made thorough preparations: she has gathered enough power to turn $M$ students into vampires instantly!

When the plan begins, Pickle chooses $M$ students and turns them into vampires instantly. The time at the start of the plan is 1.

After every $1$ unit of time passes, every vampire moves one corridor along the shortest path from their current laboratory toward laboratory $P$. Students who are not vampires do not move because they are engrossed in their research. Once a vampire reaches laboratory $P$, it does not move further.

At this point, if a vampire and a non-vampire student are in the same laboratory, the vampire immediately bites the student, and the student turns into a vampire.

Pickle wants to turn all students into vampires as quickly as possible to continue her research. Help her find the minimum time required for all students to become vampires by choosing the $M$ students appropriately.

Input

The first line contains the number of laboratories $N$, the number of students that can be turned into vampires instantly $M$, and the laboratory $P$ where Pickle is located, in that order.

From the second line, $N-1$ lines follow, each containing $a_i, b_i$ separated by a space. This means the $i$-th corridor connects laboratory $a_i$ and laboratory $b_i$.

Constraints

$2 \leq N \leq 100,000$

$1 \leq M \leq N-1$

$1 \leq P \leq N$

$1 \leq a_i, b_i \leq N, a_i \neq b_i$

Output

If it is impossible for all students to become vampires, output -1. Otherwise, output the minimum time required.

Subtasks

  1. For all $i$ $(1 \leq i < N)$, there is a corridor connecting laboratory $i$ and laboratory $i+1$, and $P = 1$ (18 points).

  2. For all $i$ $(1 \leq i < N)$, there is a corridor connecting laboratory $i$ and laboratory $i+1$ (12 points).

  3. $N \le 5000$ (30 points).

  4. No additional constraints (40 points).

Examples

Input 1

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

Output 1

2

Input 2

5 1 2
2 1
2 3
1 4
2 5

Output 2

-1

Input 3

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

Output 3

2

Note

https://www.youtube.com/watch?v=e1xCOsgWG0M

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.