#abc270c. C - Simple path

C - Simple path

Score : 300300 points

问题描述

存在一棵包含 NN 个顶点的树 TT。第 ii 条边(1iN11\leq i\leq N-1)连接顶点 UiU_i 和顶点 ViV_i

给定树 TT 中的两个不同顶点 XXYY。按顺序列出从顶点 XX 到顶点 YY 的简单路径上的所有顶点,包括端点。

可以证明,在任何一棵树中,对于任意两个不同的顶点 aabb,都存在一条唯一的简单路径从 aabb

什么是简单路径?在图 GG 中,对于顶点 XXYY,从顶点 XX 到顶点 YY路径是一系列顶点 v1,v2,,vkv_1, v_2, \ldots, v_k,满足 v1=Xv_1=Xvk=Yv_k=Y 且对于每个 1ik11\leq i\leq k-1,顶点 viv_ivi+1v_{i+1} 由一条边相连。另外,如果 v1,v2,,vkv_1, v_2, \ldots, v_k 全部不相同,则称这条路径是从顶点 XX 到顶点 YY简单路径

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

There is a tree TT with NN vertices. The ii-th edge (1iN1)(1\leq i\leq N-1) connects vertex UiU_i and vertex ViV_i.

You are given two different vertices XX and YY in TT. List all vertices along the simple path from vertex XX to vertex YY in order, including endpoints.

It can be proved that, for any two different vertices aa and bb in a tree, there is a unique simple path from aa to bb.

What is a simple path? For vertices XX and YY in a graph GG, a path from vertex XX to vertex YY is a sequence of vertices v1,v2,,vkv_1,v_2, \ldots, v_k such that v1=Xv_1=X, vk=Yv_k=Y, and viv_i and vi+1v_{i+1} are connected by an edge for each 1ik11\leq i\leq k-1. Additionally, if all of v1,v2,,vkv_1,v_2, \ldots, v_k are distinct, the path is said to be a simple path from vertex XX to vertex YY.

Constraints

  • 1N2×1051\leq N\leq 2\times 10^5
  • 1X,YN1\leq X,Y\leq N
  • XYX\neq Y
  • 1Ui,ViN1\leq U_i,V_i\leq N
  • All values in the input are integers.
  • The given graph is a tree.

Input

The input is given from Standard Input in the following format:

NN XX YY

U1U_1 V1V_1

U2U_2 V2V_2

\vdots

UN1U_{N-1} VN1V_{N-1}

Output

Print the indices of all vertices along the simple path from vertex XX to vertex YY in order, with spaces in between.

Sample Input 1

5 2 5
1 2
1 3
3 4
3 5

Sample Output 1

2 1 3 5

The tree TT is shown below. The simple path from vertex 22 to vertex 55 is 22 \to 11 \to 33 \to 55.
Thus, 2,1,3,52,1,3,5 should be printed in this order, with spaces in between.

Sample Input 2

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

Sample Output 2

1 2

The tree TT is shown below.

update @ 2024/3/10 11:23:14