#abc270c. C - Simple path
C - Simple path
Score : points
问题描述
存在一棵包含 个顶点的树 。第 条边()连接顶点 和顶点 。
给定树 中的两个不同顶点 和 。按顺序列出从顶点 到顶点 的简单路径上的所有顶点,包括端点。
可以证明,在任何一棵树中,对于任意两个不同的顶点 和 ,都存在一条唯一的简单路径从 到 。
什么是简单路径?在图 中,对于顶点 和 ,从顶点 到顶点 的路径是一系列顶点 ,满足 、 且对于每个 ,顶点 和 由一条边相连。另外,如果 全部不相同,则称这条路径是从顶点 到顶点 的简单路径。
以上为通义千问 qwen-max 翻译,仅供参考。
Problem Statement
There is a tree with vertices. The -th edge connects vertex and vertex .
You are given two different vertices and in . List all vertices along the simple path from vertex to vertex in order, including endpoints.
It can be proved that, for any two different vertices and in a tree, there is a unique simple path from to .
What is a simple path? For vertices and in a graph , a path from vertex to vertex is a sequence of vertices such that , , and and are connected by an edge for each . Additionally, if all of are distinct, the path is said to be a simple path from vertex to vertex .
Constraints
- 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:
Output
Print the indices of all vertices along the simple path from vertex to vertex 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 is shown below. The simple path from vertex to vertex is .
Thus, 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 is shown below.
update @ 2024/3/10 11:23:14