#4653. Simple path

0

Simple path

Simple path

题目描述

There is a tree TT with NN vertices. The ii-th edge (1≤i≤N−1)(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. 列出从顶点 XX 到顶点 YY 的简单路径上的所有顶点,按顺序,包括端点。 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 $X$ and $Y$ in a graph $G$, a path from vertex $X$ to vertex $Y$ is a sequence of vertices $v_1,v_2, \ldots, v_k$ such that $v_1=X$, $v_k=Y$, and $v_i$ and $v_{i+1}$ are connected by an edge for each $1\leq i\leq k-1$. Additionally, if all of $v_1,v_2, \ldots, v_k$ are distinct, the path is said to be a simple path from vertex $X$ to vertex $Y$.

输入格式

The input is given from Standard Input in the following format: NN XX YY U1U_1 V1V_1 U2U_2 V2V_2 ⋮\vdots UN−1U_{N-1} VN−1V_{N-1}

输出格式

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

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

提示

1≤N≤2×1051\leq N\leq 2\times 10^5 1≤X,Y≤N1\leq X,Y\leq N X≠YX\neq Y 1≤Ui,Vi≤N1\leq U_i,V_i\leq N All values in the input are integers. The given graph is a tree.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.
The tree TT is shown below.

标签: AtCoder|abc270C

来源

AtCoder|abc270C