#P4281. Emergency Gathering

    ID: 17527 Type: Default 1000ms 256MiB

Emergency Gathering

Emergency Gathering

On Happy Island, a popular game called "Emergency Gathering" is played. There are \(n\) waiting points connected by \(n-1\) roads. Each road connects two waiting points and it is possible to travel between any two waiting points using these roads; each road travel costs 1 game coin.

Players participate in groups of three. Initially, each player is arbitrarily located at one of the waiting points (multiple players can be at the same point). When the gathering horn sounds, players communicate with their teammates and quickly decide on a gathering point among the \(n\) waiting points so that the total spending (i.e. the sum of coins paid along the traveled roads) is minimized. The group that manages to gather with the least total cost wins the game.

Your task is to help choose the optimal gathering point by computing the minimal total cost for the three team members. In a tree metric, it is a known fact that the minimum total cost is given by the formula:

[ \frac{d(a,b) + d(a,c) + d(b,c)}{2} ]

where \(d(x,y)\) denotes the number of roads (edges) between waiting points \(x\) and \(y\), and \(a\), \(b\), \(c\) are the starting points of the three players.

inputFormat

The input begins with a single integer \(n\) (\(3 \le n \le 10^5\)), the number of waiting points. The points are numbered from 1 to \(n\).

Then follow \(n-1\) lines, each containing two integers \(u\) and \(v\) (\(1 \le u,v \le n\)), indicating that there is a road connecting waiting points \(u\) and \(v\).

The last line contains three integers \(a\), \(b\), and \(c\) (each between 1 and \(n\)), which represent the initial positions of the three players.

outputFormat

Output a single integer denoting the minimum total cost needed for all three players to gather at the optimal waiting point.

sample

4
1 2
2 3
2 4
1 3 4
3

</p>