CF1381D.The Majestic Brown Tree Snake

NOI/NOI+/CTSC

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

There is an undirected tree of nn vertices, connected by n−1n-1 bidirectional edges. There is also a snake stuck inside of this tree. Its head is at vertex aa and its tail is at vertex bb. The snake's body occupies all vertices on the unique simple path between aa and bb.

The snake wants to know if it can reverse itself — that is, to move its head to where its tail started, and its tail to where its head started. Unfortunately, the snake's movements are restricted to the tree's structure.

In an operation, the snake can move its head to an adjacent vertex not currently occupied by the snake. When it does this, the tail moves one vertex closer to the head, so that the length of the snake remains unchanged. Similarly, the snake can also move its tail to an adjacent vertex not currently occupied by the snake. When it does this, the head moves one unit closer to the tail.

Let's denote a snake position by (h,t)(h,t), where hh is the index of the vertex with the snake's head, tt is the index of the vertex with the snake's tail. This snake can reverse itself with the movements (4,7)→(5,1)→(4,2)→(1,3)→(7,2)→(8,1)→(7,4)(4,7)\to (5,1)\to (4,2)\to (1, 3)\to (7,2)\to (8,1)\to (7,4).

Determine if it is possible to reverse the snake with some sequence of operations.

有一棵包含 nn 个顶点的无向树,由 n−1n-1 条双向边连接。树中还有一条蛇被困在其中:其头部位于顶点 aa,尾部位于顶点 bb。蛇的身体占据了 aa 与 bb 之间唯一简单路径上的所有顶点。

这条蛇希望判断自己能否完成“翻转”——即让头部移动至原先尾部所在的位置,同时让尾部移动至原先头部所在的位置。遗憾的是,蛇的移动受限于树的结构。

在一次操作中,蛇可将其头部移动至一个当前未被蛇身占据的相邻顶点;此时,尾部会向头部方向移动一个顶点,从而保证蛇的长度保持不变。类似地,蛇也可将其尾部移动至一个当前未被蛇身占据的相邻顶点;此时,头部会向尾部方向移动一个顶点。

我们用 (h,t)(h,t) 表示蛇的一个位置,其中 hh 是头部所在顶点的编号,tt 是尾部所在顶点的编号。图中所示的蛇可通过如下一系列移动实现翻转:(4,7)→(5,1)→(4,2)→(1,3)→(7,2)→(8,1)→(7,4)(4,7)\to (5,1)\to (4,2)\to (1, 3)\to (7,2)\to (8,1)\to (7,4)。

请判断:是否存在某种操作序列,使得该蛇能够完成翻转。

输入格式

The first line contains a single integer tt (1≤t≤1001\le t\le 100) — the number of test cases. The next lines contain descriptions of test cases.

The first line of each test case contains three integers n,a,bn,a,b (2≤n≤105,1≤a,b≤n,a≠b2\le n\le 10^5,1\le a,b\le n,a\ne b).

Each of the next n−1n-1 lines contains two integers ui,viu_i,v_i (1≤ui,vi≤n,ui≠vi1\le u_i,v_i\le n,u_i\ne v_i), indicating an edge between vertices uiu_i and viv_i. It is guaranteed that the given edges form a tree.

It is guaranteed that the sum of nn across all test cases does not exceed 10510^5.

第一行包含一个整数 tt(1≤t≤1001\le t\le 100),表示测试用例的数量。接下来的若干行包含各测试用例的描述。

每个测试用例的第一行包含三个整数 n,a,bn,a,b(2≤n≤105, 1≤a,b≤n, a≠b2\le n\le 10^5,\,1\le a,b\le n,\,a\ne b)。

接下来的 n−1n-1 行中,每行包含两个整数 ui,viu_i,v_i(1≤ui,vi≤n, ui≠vi1\le u_i,v_i\le n,\,u_i\ne v_i),表示顶点 uiu_i 与 viv_i 之间存在一条边。保证所给的边构成一棵树。

保证所有测试用例的 nn 值之和不超过 10510^5。

输出格式

For each test case, output "YES" if it is possible for the snake to reverse itself, or "NO" otherwise.

对于每个测试用例,如果蛇能够反转自身,则输出 “YES”,否则输出 “NO”。

输入输出样例

  • 输入#1

    4
    8 4 7
    1 2
    2 3
    1 4
    4 5
    4 6
    1 7
    7 8
    4 3 2
    4 3
    1 2
    2 3
    9 3 5
    1 2
    2 3
    3 4
    1 5
    5 6
    6 7
    1 8
    8 9
    16 15 12
    1 2
    2 3
    1 4
    4 5
    5 6
    6 7
    4 8
    8 9
    8 10
    10 11
    11 12
    11 13
    13 14
    10 15
    15 16

    输出#1

    YES
    NO
    NO
    YES

说明/提示

The first test case is pictured above.

In the second test case, the tree is a path. We can show that the snake cannot reverse itself.

In the third test case, we can show that the snake cannot reverse itself.

In the fourth test case, an example solution is:

(15,12)→(16,11)→(15,13)→(10,14)→(8,13)→(4,11)→(1,10)(15,12)\to (16,11)\to (15,13)\to (10,14)\to (8,13)\to (4,11)\to (1,10)

→(2,8)→(3,4)→(2,5)→(1,6)→(4,7)→(8,6)→(10,5)\to (2,8)\to (3,4)\to (2,5)\to (1,6)\to (4,7)\to (8,6)\to (10,5)

→(11,4)→(13,8)→(14,10)→(13,15)→(11,16)→(12,15)\to (11,4)\to (13,8)\to (14,10)\to (13,15)\to (11,16)\to (12,15).

第一个测试用例如上图所示。

在第二个测试用例中,该树是一条路径。我们可以证明蛇无法反转自身。

在第三个测试用例中,我们可以证明蛇无法反转自身。

在第四个测试用例中,一个可行的解为:

(15,12)→(16,11)→(15,13)→(10,14)→(8,13)→(4,11)→(1,10)(15,12)\to (16,11)\to (15,13)\to (10,14)\to (8,13)\to (4,11)\to (1,10)

→(2,8)→(3,4)→(2,5)→(1,6)→(4,7)→(8,6)→(10,5)\to (2,8)\to (3,4)\to (2,5)\to (1,6)\to (4,7)\to (8,6)\to (10,5)

→(11,4)→(13,8)→(14,10)→(13,15)→(11,16)→(12,15)\to (11,4)\to (13,8)\to (14,10)\to (13,15)\to (11,16)\to (12,15)。

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

首页