Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

3 7 8 QAQ has a tree with n vertices. Initially, all vertices are white. There are two chess pieces called PA and PB on

378QAQ has a tree with n
vertices. Initially, all vertices are white.
There are two chess pieces called PA
and PB
on the tree. PA
and PB
are initially located on vertices a
and b
respectively. In one step, 378QAQ will do the following in order:
Move PA
to a neighboring vertex. If the target vertex is white, this vertex will be painted red.
Move PB
to a neighboring vertex. If the target vertex is colored in red, this vertex will be painted blue.
Initially, the vertex a
is painted red. If a=b
, the vertex a
is painted blue instead. Note that both the chess pieces must be moved in each step. Two pieces can be on the same vertex at any given time.
378QAQ wants to know the minimum number of steps to paint all vertices blue.
Input
Each test contains multiple test cases. The first line contains the number of test cases t
(1<=t<=104
). The description of the test cases follows.
The first line of each test case contains one integer n
(1<=n<=2105
).
The second line of each test case contains two integers a
and b
(1<=a,b<=n
).
Then n1
lines follow, each line contains two integers xi
and yi
(1<=xi,yi<=n
), indicating an edge between vertices xi
and yi
. It is guaranteed that these edges form a tree.
It is guaranteed that the sum of n
over all test cases does not exceed 2105
.
Output
For each test case, output the minimum number of steps to paint all vertices blue.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions