Codeforces Round 873 (Div. 1)
E
Bus Routes
Codeforces
3400
原题
← DF →
翻译中…

There is a country consisting of $n$ cities and $n - 1$ bidirectional roads connecting them such that we can travel between any two cities using these roads. In other words, these cities and roads form a tree.

翻译中…

There are $m$ bus routes connecting the cities together. A bus route between city $x$ and city $y$ allows you to travel between any two cities in the simple path between $x$ and $y$ with this route.

翻译中…

Determine if for every pair of cities $u$ and $v$, you can travel from $u$ to $v$ using at most two bus routes.

翻译中…

Input

翻译中…

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

翻译中…

The first line of each test case contains two integers $n$ and $m$ ($2 \le n \le 5 \cdot 10^5, 0 \le m \le 5 \cdot 10^5$) — the number of cities and the number of bus routes.

翻译中…

Then $n - 1$ lines follow. Each line contains two integers $u$ and $v$ denoting a road connecting city $u$ and city $v$ ($1 \le u, v \le n, u \neq v$). It is guaranteed that these cities and roads form a tree.

翻译中…

Then $m$ lines follow. Each line contains two integers $x$ and $y$ denoting a bus route between city $x$ and city $y$ ($1 \le x, y \le n$).

翻译中…

It is guaranteed that the sum of $n$ over all test cases does not exceed $5 \cdot 10^5$ and the sum of $m$ over all test cases does not exceed $5 \cdot 10^5$.

翻译中…

Output

翻译中…

For each test case, output "YES" if you can travel between any pair of cities using at most two bus routes.

翻译中…

Otherwise, output "NO". In the next line, output two cities $x$ and $y$ ($1 \le x, y \le n$) such that it is impossible to reach city $y$ from city $x$ using at most two bus routes.

翻译中…

You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.

翻译中…

Note

翻译中…

Here are the graphs of test case $1$, $2$, and $4$:

翻译中…

image Sample 1

翻译中…

image Sample 2

翻译中…

image Sample 4

翻译中…

样例

Input #1
4
5 2
1 2
2 3
3 4
2 5
1 4
5 2
5 1
1 2
2 3
3 4
2 5
1 5
2 0
1 2
6 3
1 2
2 3
3 4
4 5
5 6
1 3
2 5
4 6
Output #1
YES
NO
1 3
NO
1 2
NO
1 6
InputCopy OutputCopy
题目信息
题号
1827E
时间限制
2.5 s
内存限制
1024 MB
标签
二分
构造
DFS 及类似
贪心
树

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。