#P3363. Cool loves jiaoyi
Cool loves jiaoyi
Description
Cool’s trading partners form a tree.
For each trade, there is a start and an end. The path from the start to the end on the tree is called the trade chain. All trading partners (nodes) on the trade chain join this trade, and the cost of the trade equals the number of trading partners on the chain.
Now there are trades. Cool will designate trades such that there exists a mysterious trading partner who participates in all these trades, and he wants to minimize the maximum difference of costs among these trades (i.e., minimize max cost min cost).
Input Format
The input consists of several lines.
- The first line contains three integers , representing the number of trading partners, the number of trades, and the designated .
- Each of the next lines contains two integers , indicating that trading partners and are connected in the trading tree.
- Each of the next lines contains two integers , representing the start and end of a trade (the start and end may coincide).
Output Format
Output a single integer, the minimal possible value of the maximum cost difference among the designated trades. If no such set of trades exists, output .
5 4 3
1 2
1 3
1 4
4 5
2 3
3 5
2 5
4 5
1
Hint
Constraints
Sample Explanation
Choose trades . Trading partner participates in all three trades, and the three costs are . Their maximum cost difference is . This is optimal.
Translated by ChatGPT 5
京公网安备 11011102002149号