1776.The Tag Game

Time Limit: 1s Memory Limit: 256MB

Alice got tired of playing the tag game by the usual rules so she offered Bob a little modification to it. Now the game should be played on an undirected rooted tree of n vertices. Vertex 1 is the root of the tree.Alice starts at vertex 1 and Bob starts at vertex x (x \neq 1). The moves are made in turns, Bob goes first. In one move one can either stay at the current vertex or travel to the neighbouring one.The game ends when Alice goes to the same vertex where Bob is standing. Alice wants to minimize the total number of moves and Bob wants to maximize it.You should write a program which will determine how many moves will the game last.

Input Format(From the terminal/stdin)

Input The first line contains two integer numbers n and x (2 \le n \le 2 \cdot 105, 2 \le x \le n).Each of the next n-1 lines contains two integer numbers a and b (1 \le a,b \le n) - edges of the tree. It is guaranteed that the edges form a valid tree.

Output Format(To the terminal/stdout)

Output Print the total number of moves Alice and Bob will make.

Sample Input 1

Copy
4 3
1 2
2 3
2 4
 · \n
 · \n
 · \n
 · \n

Sample Output 1

Copy
4
 \n

Sample Input 2

Copy
5 2
1 2
2 3
3 4
2 5
 · \n
 · \n
 · \n
 · \n
 · \n

Sample Output 2

Copy
6
 \n

Hints

In the first example the tree looks like this: 1776_1.png The red vertex is Alice's starting position, the blue one is Bob's. Bob will make the game run the longest by standing at the vertex 3 during all the game. So here are the moves:B: stay at vertex 3A: go to vertex 2B: stay at vertex 3A: go to vertex 3In the second example the tree looks like this: 1776_2.png The moves in the optimal strategy are:B: go to vertex 3A: go to vertex 2B: go to vertex 4A: go to vertex 3B: stay at vertex 4A: go to vertex 4

Submit

请先 登录

© 2025 FAQs