Logo
(追記) (追記ここまで)

34590번 - Gamer Bafuko 다국어

시간 제한메모리 제한제출정답맞힌 사람정답 비율
2 초 2048 MB186633.333%

문제

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 n vertices numbered from 1ドル$ to $n$. Each edge of the tree has weight 1ドル$. In addition, there is a special portal connecting two designated distinct vertices $x$ and $y$. This portal can be used any number of times and allows instantaneous travel between $x$ and $y$ 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.

입력

Each test contains multiple test cases. The first line contains the number of test cases $t$. The description of the test cases follows.

The first line of each test case contains three integers $n,ドル $x,ドル and $y,ドル representing the number of vertices in the tree and the two vertices connected by the portal.

Followed by $n − 1$ lines, the $i$-th of which contains two integers $u_i,ドル $v_i,ドル representing an edge between vertices $u_i$ and $v_i$.

출력

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.

제한

  • 1ドル ≤ t ≤ 10^4$
  • 2ドル ≤ n ≤ 5 \times 10^5$
  • 1ドル ≤ x, y ≤ n$ and $x \ne y$.
  • 1ドル ≤ u_i , v_i ≤ n$ and $u_i \ne v_i$.
  • It is guaranteed that the given edges form a tree.
  • It is guaranteed that the sum of $n$ over all test cases does not exceed 5ドル \times 10^5$.

예제 입력 1

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

예제 출력 1

2
8
7

노트

출처

ICPC > Regionals > Asia Pacific > Taiwan > Taiwan Online Programming Contest > TOPC 2025 G번

(追記) (追記ここまで)

출처

대학교 대회

  • 사업자 등록 번호: 541-88-00682
  • 대표자명: 최백준
  • 주소: 서울시 서초구 서초대로74길 29 서초파라곤 412호
  • 전화번호: 02-521-0487 (이메일로 연락 주세요)
  • 이메일: contacts@startlink.io
  • 통신판매신고번호: 제 2017-서울서초-2193 호

AltStyle によって変換されたページ (->オリジナル) /