#P4654. [CEOI 2017] Mousetrap

[CEOI 2017] Mousetrap

Description

There is a maze with nn rooms and n1n-1 corridors. It is guaranteed that any two rooms are connected by corridors; in other words, the maze structure is a tree.

A mouse is put into the maze, and the maze manager decides to play a game with the mouse.

At the beginning, one room contains a trap, and the mouse appears in another room. The mouse can move to other rooms through corridors, but it will dirty every corridor it passes through. The mouse does not want to go through dirty corridors.

At each moment, the manager may perform one operation: block a corridor so that the mouse cannot pass through it, or clean a corridor so that the mouse can pass through it. Then the mouse will move through one corridor that is clean and not blocked to another room. Only if there is no such corridor will the mouse stay still. Initially, all corridors are clean. The manager cannot unblock a corridor once it has been blocked.

Now the manager wants to drive the mouse into the room with the trap using as few operations as possible, while the mouse wants the manager to use as many operations as possible. Compute the number of operations the manager needs when both sides play optimally.

Note: The manager may choose to do nothing at some moments.

Input Format

The first line contains three space-separated positive integers n,t,mn,t,m, representing the number of rooms, the index of the trap room, and the index of the mouse’s starting room.

The next n1n-1 lines each contain two space-separated integers ai,bia_i,b_i, meaning there is a corridor connecting rooms aia_i and bib_i.

Output Format

Output one line containing one integer, indicating the number of operations the manager needs when both sides play optimally.

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

Hint

Sample Explanation

  • The manager first blocks the corridor between rooms 44 and 77.
  • The mouse moves to room 66. The corridor between rooms 44 and 66 is now dirty.
  • The manager blocks the corridor between rooms 66 and 88.
  • The mouse cannot move.
  • The manager cleans the corridor between rooms 44 and 66. The corridor between rooms 44 and 66 is now clean.
  • The mouse moves to room 44. The corridor between rooms 44 and 66 is now dirty.
  • The manager blocks the corridor between rooms 22 and 33.
  • The mouse moves to room 22. The corridor between rooms 22 and 44 is now dirty.
  • The manager performs no operation.
  • The mouse moves to room 11.

In this process, the manager performed a total of 44 operations.

Constraints

For all testdata, 1n1061 \le n \le 10^6.

Translated by ChatGPT 5