#P15899. [TOPC 2025] Gamer Bafuko
[TOPC 2025] Gamer Bafuko
Description
:::align{right}

Picture of Bafuko for reference (X
Bafuko loves gaming! She is currently playing the latest title, Young Bracelet – Light of the Saint Grass. As an experienced gamer, she wants to explore every location on the map.
The map can be represented as a tree with vertices numbered from to . Each edge of the tree has weight . In addition, there is a special portal connecting two designated distinct vertices and . This portal can be used any number of times and allows instantaneous travel between and at zero cost.
Bafuko wants to visit every vertex of the tree at least once by following a tour. A tour is a sequence of adjacent vertices that begins at a chosen vertex, ends at a chosen vertex (possibly the same one), and visits every vertex of the tree at least once. She may freely choose the starting and ending vertices. During the tour, both edges and the portal may be traversed multiple times if necessary. The cost of the tour is defined as the sum of the costs of all traversed edges and portal uses, where repeated traversals are counted multiple times.
Your task is to determine the minimum total cost of such a tour.
Input Format
Each test contains multiple test cases. The first line contains the number of test cases . The description of the test cases follows.
The first line of each test case contains three integers , , and , representing the number of vertices in the tree and the two vertices connected by the portal.
Followed by lines, the -th of which contains two integers , representing an edge between vertices and .
Output Format
For each test case, print a single integer in a new line, representing the minimum total cost of a tour that visits every vertex of the tree at least once.
3
4 1 3
1 2
2 3
2 4
8 1 3
1 2
2 3
2 4
4 7
7 8
4 5
5 6
8 1 2
1 2
2 3
3 4
4 5
3 6
6 7
7 8
2
8
7
Hint
- and .
- and .
- It is guaranteed that the given edges form a tree.
- It is guaranteed that the sum of over all test cases does not exceed .
京公网安备 11011102002149号