atcoder#ABC270C. [ABC270C] Simple path
[ABC270C] Simple path
Score : points
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 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.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.
5 2 5
1 2
1 3
3 4
3 5
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.
6 1 2
3 1
2 5
1 2
4 1
2 6
1 2
The tree is shown below.