CF1827E.Bus Routes

NOI/NOI+/CTSC

通过率:0%

时间限制:2.50s

内存限制:1024MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

There is a country consisting of nn cities and n−1n - 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 mm bus routes connecting the cities together. A bus route between city xx and city yy allows you to travel between any two cities in the simple path between xx and yy with this route.

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

有一个由 nn 座城市和 n−1n - 1 条双向道路组成的国家,这些道路连接所有城市,使得任意两座城市之间均可通过道路互相到达。换言之,这些城市与道路构成一棵树。

共有 mm 条公交线路连接这些城市。一条从城市 xx 到城市 yy 的公交线路,允许你仅使用该线路在 xx 与 yy 之间的简单路径上的任意两座城市间通行。

请判断:对于每一对城市 uu 和 vv,是否均能至多使用两条公交线路从 uu 到达 vv。

输入格式

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

The first line of each test case contains two integers nn and mm (2≤n≤5⋅105,0≤m≤5⋅1052 \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−1n - 1 lines follow. Each line contains two integers uu and vv denoting a road connecting city uu and city vv (1≤u,v≤n,u≠v1 \le u, v \le n, u \neq v). It is guaranteed that these cities and roads form a tree.

Then mm lines follow. Each line contains two integers xx and yy denoting a bus route between city xx and city yy (1≤x,y≤n1 \le x, y \le n).

It is guaranteed that the sum of nn over all test cases does not exceed 5⋅1055 \cdot 10^5 and the sum of mm over all test cases does not exceed 5⋅1055 \cdot 10^5.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤1041 \le t \le 10^4)。随后是各测试用例的描述。

每个测试用例的第一行包含两个整数 nn 和 mm(2≤n≤5⋅105, 0≤m≤5⋅1052 \le n \le 5 \cdot 10^5,\ 0 \le m \le 5 \cdot 10^5)——分别表示城市的数量和公交线路的数量。

接下来是 n−1n - 1 行。每行包含两个整数 uu 和 vv,表示一条连接城市 uu 和城市 vv 的道路(1≤u,v≤n, u≠v1 \le u, v \le n,\ u \neq v)。保证这些城市与道路构成一棵树。

然后是 mm 行。每行包含两个整数 xx 和 yy,表示一条连接城市 xx 和城市 yy 的公交线路(1≤x,y≤n1 \le x, y \le n)。

保证所有测试用例的 nn 之和不超过 5⋅1055 \cdot 10^5,且所有测试用例的 mm 之和不超过 5⋅1055 \cdot 10^5。

输出格式

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 xx and yy (1≤x,y≤n1 \le x, y \le n) such that it is impossible to reach city yy from city xx 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.

对于每个测试用例,如果可以通过至多两条公交线路在任意两个城市之间通行,则输出 "YES"。

否则,输出 "NO"。在下一行输出两个城市 xx 和 yy(1≤x,y≤n1 \le x, y \le n),使得无法通过至多两条公交线路从城市 xx 到达城市 yy。

你可以以任意大小写形式输出答案(大写或小写均可)。例如,字符串 "yEs"、"yes"、"Yes" 和 "YES" 均会被识别为肯定回答。

输入输出样例

  • 输入#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

    输出#1

    YES
    NO
    1 3
    NO
    1 2
    NO
    1 6

说明/提示

Here are the graphs of test case 11, 22, and 44:

Sample 1

Sample 2

Sample 4

以下是测试用例 11、22 和 44 的图示:

样例 1

样例 2

样例 4

输入解题思路,AI测评打分。不知道怎么写?

首页