306538: CF1211G. King's Path

Memory Limit:256 MB Time Limit:5 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

King's Path

题目描述

There are $ n $ cities and $ n-1 $ two-way roads in Treeland. Each road connects a pair of different cities. From any city you can drive to any other, moving only along the roads. Cities are numbered from $ 1 $ to $ n $ . Yes, of course, you recognized an undirected tree in this description. There is exactly one flag in each city, in the $ i $ -th city the flag color is $ c_i $ . The colors of the flags in different cities may be the same. If the King travels along the route $ [u_1, u_2, u_3, \dots, u_k] $ , then this means that he starts in the city $ u_1 $ , then moves to the city $ u_2 $ ( $ u_2 $ is connected by road with $ u_1 $ ), then from $ u_2 $ to $ u_3 $ ( $ u_3 $ is connected by road to $ u_2 $ ), and so on until he arrives in the city of $ u_k $ . It is possible that during this route the King will visit the same city more than once. In other words, the route $ [u_1, u_2, u_3, \dots, u_k] $ does not necessarily consist of different cities. In terms of graph theory — the King moves from $ u_1 $ to $ u_k $ along some path $ [u_1, u_2, u_3, \dots, u_k] $ , which is not necessarily simple (for all $ j $ from $ 1 $ to $ k-1 $ of the city $ u_j $ and $ u_{j+1} $ are connected by road). When the King moves from one city to another, city heads exchange flags as a sign of their friendship. ![](https://cdn.luogu.com.cn/upload/vjudge_pic/CF1211G/d695a88743c705b53ebb6d0c8763366c0983b769.png)Example of moving the King along the route $ [1, 4, 2, 6] $ . The color of the vertex matches the color of the flag at that vertex.For aesthetic reasons, the King wants the flag color in the city $ i $ to be equal to $ d_i $ for all $ i $ from $ 1 $ to $ n $ . Determine whether the King can choose some route and drive along it so that for each city the flag color in it turns out to be equal to the desired color $ d_i $ . Note that the King can choose (and drive) exactly one route. If yes, find the shortest possible route for the King. If the initial colors of the flags already match the King's requirements (i.e. $ c_i=d_i $ for all $ i $ ), then consider that the King makes a route of length $ k=0 $ .

输入输出格式

输入格式


The first line contains an integer $ t $ ( $ 1 \le t \le 10^5 $ ) — the number of test cases to solve. The following are the cases. Each case begins with a line containing an integer $ n $ ( $ 2 \le n \le 2\cdot10^5 $ ) — the number of cities in Treeland. The following is a line of $ n $ integers $ c_1, c_2, \dots, c_n $ ( $ 1 \le c_i \le 10^6 $ ), where $ c_i $ denotes the color of the flag at the $ i $ -th vertex before the King's journey. The following is a line of $ n $ integers $ d_1, d_2, \dots, d_n $ ( $ 1 \le d_i \le 10^6 $ ), where $ d_i $ denotes the required flag color at the $ i $ -th vertex after the completion of the King's journey. Further, in the $ n-1 $ line, the Treeland's roads are listed. Each road is given by a line containing two integers $ x_j, y_j $ ( $ 1 \le x_j, y_j \le n $ ) — numbers of cities that are connected by the $ j $ th road. It is guaranteed that from every city you can get to any other by road (in other words, the system of cities and roads forms an undirected tree). The sum of all $ n $ values ​​for all cases in one test does not exceed $ 2\cdot10^5 $ .

输出格式


Print the answers to all cases in the order of their appearance in the input data. Each answer must begin with a line containing "Yes" (in the case of a positive answer) or "No" (in the case that the required route does not exist). In the case of a positive answer, the following line must contain an integer $ k $ — the number of cities in the shortest possible route of the King. The next line should contain the required route $ u_1, u_2, \dots, u_k $ ( $ 1 \le u_i \le n $ ). You can skip the line if $ k=0 $ .

输入输出样例

输入样例 #1

1
7
2 3 2 7 1 1 3
7 1 2 3 1 2 3
1 7
4 1
2 6
2 3
2 4
5 4

输出样例 #1

Yes
4
1 4 2 6 

输入样例 #2

1
5
1 2 2 2 2
2 2 2 2 1
1 2
2 3
3 4
4 5

输出样例 #2

Yes
5
1 2 3 4 5 

输入样例 #3

3
4
10 20 10 20
20 10 20 10
1 2
1 3
1 4
2
1000000 1000000
1000000 1000000
1 2
10
4 2 2 4 2 4 1 2 3 4
4 2 4 4 3 2 1 2 4 2
5 8
6 9
10 5
1 10
7 10
3 4
5 9
3 10
2 4

输出样例 #3

No
Yes
0
Yes
5
3 10 5 9 6 

Input

题意翻译

### 题目描述 树之国有n个城市和n-1条双向道路。每条路连接着两个不同的城市。你可以从任何一个城市开车到另一个城市,只需要沿着道路行驶。城市的编号从1到n。当然,你在这个描述中认出了一棵无向树。 每个城市都有一面国旗,在第 i 个城市,国旗的颜色是$c_i$。不同城市的国旗颜色可能是一样的。 如果国王旅行沿途 [$u_1$,$u_2$,$u_3$,...,$u_k$] 那么这就意味着,他开始在城市 $u_1$ ,然后移动到城市$u_2$ ($u_2$与$u_1$之间有公路连接) ,然后从$u_2$到$u_3$ ($u_3$与$u_2$之间有公路连接),直到他到达城市$u_k$。在这条路线上,国王可能会多次访问同一个城市。换句话说,路线[$u_1$,$u_2$,$u_3$,...,$u_k$]不一定由不同的城市组成。在图论方面,国王沿着一些路径 [$u_1$,$u_2$,$u_3$,...,$u_k$] 从$u_1$移动到$u_k$,这并不一定简单(对于城市 $u_j$ 和$u_{j+1}$(所有 j 取于1到k-1)都是通过道路连接的)。 当国王从一个城市到另一个城市时,城市领导人交换旗帜作为他们友谊的标志。 ![](https://cdn.luogu.com.cn/upload/vjudge_pic/CF1211G/d695a88743c705b53ebb6d0c8763366c0983b769.png) 例: 移动国王沿路线[1,4,2,6]。顶点的颜色与该顶点的标志颜色相匹配。出于美观的原因,国王希望城市$d_i$(1$\le$i$\le$n)的旗帜颜色都是相同的。确定国王是否可以选择一些路线并沿着它行驶,以便每个城市的旗帜颜色都与期望的颜色相同。**注意,国王可以选择(并驾驶)一条路线。如果是,为国王找一条最短的路线。** 如果旗子的初始颜色已经符合国王的要求(即对于所有i,$c_i$=$d_i$),则认为国王的路线长度为k=0。 ### 输入格式 第一行包含一个整数 t (1$\le$t$\le$$10^5$)——要解决的测试用例的数量。 每一种情况先输入一个整数n (2$\le$n$\le$$2⋅10^5$)即树之国的城市数量。 下面输入n个整数,$c_1$,$c_2$,...,$c_n$(1$\le$$c_i$$\le$$10^6$),其中$c_i$表示国王旅行前第i个顶点的旗帜颜色。 下面是一行n个整数,$d_1$,$d_2$,...,$d_n$(1$\le$$d_i$$\le$$10^6$),其中$d_i$表示国王旅程完成后第i个顶点所需的旗帜颜色。 此外,在n−1行,列出树地的道路。每条道路连接两个整数点$x_j$,$y_j$(1$\le$$x_j$,$y_j$$\le$n) ——由第j条道路连接的城市数量。 它保证从每个城市你都可以通过道路到达任何其他城市(换句话说,城市和道路系统形成了一个无向树)。 在一次测试中,所有情况下所有n值的总和不超过$2⋅10 ^5$。 ### 输出格式 按输入数据中出现的顺序输出所有案例的答案。 每个答案必须以包含“Yes”(在肯定答案的情况下)或“No”(在所要求的路线不存在的情况下)。如果答案是肯定的,下面一行必须包含一个整数k——国王最短可能路线上的城市数量。下一行应该包含所需的路线$u_1$,$u_2$,...,$u_k$(1$\le$$u_i$$\le$n)。如果k=0,可以跳过这行。

加入题单

算法标签: