Codeforces Beta Round 29(Div. 2,Codeforces 格式)
D
树上的蚂蚁
Ant on the Tree
Codeforces
2000
原题
← CE →

树上的蚂蚁AI (CI翻译)

Connected undirected graph without cycles is called a tree. Trees is a class of graphs which is interesting not only for people, but for ants too.

无环的连通无向图称为树。树是一类不仅对人类有趣,对蚂蚁也同样有趣的图。AI (CI翻译)

An ant stands at the root of some tree. He sees that there are $*n*$ vertexes in the tree, and they are connected by $*n* - 1$ edges so that there is a path between any pair of vertexes. A leaf is a distinct from root vertex, which is connected with exactly one other vertex.

一只蚂蚁站在一棵树的根节点上。他看到这棵树有 $*n*$ 个顶点,并由 $*n* - 1$ 条边连接,使得任意两个顶点之间都存在一条路径。叶子是一个不为根节点、且恰好与另一个顶点相连的顶点。AI (CI翻译)

The ant wants to visit every vertex in the tree and return to the root, passing every edge twice. In addition, he wants to visit the leaves in a specific order. You are to find some possible route of the ant.

蚂蚁希望访问树中的每个顶点,并最终返回根节点,同时每条边恰好经过两次。此外,他还希望按照指定顺序访问所有叶子。请你找出蚂蚁的一条可行路线。AI (CI翻译)

Input

输入

AI (CI翻译)

The first line contains integer $*n*$ ($3 ≤ *n* ≤ 300$) — amount of vertexes in the tree. Next $*n* - 1$ lines describe edges. Each edge is described with two integers — indexes of vertexes which it connects. Each edge can be passed in any direction. Vertexes are numbered starting from $1$. The root of the tree has number $1$. The last line contains $*k*$ integers, where $*k*$ is amount of leaves in the tree. These numbers describe the order in which the leaves should be visited. It is guaranteed that each leaf appears in this order exactly once.

第一行包含一个整数 $*n*$($3 ≤ *n* ≤ 300$),表示树中顶点的数量。接下来的 $*n* - 1$ 行描述树的边。每条边由两个整数表示,即它所连接的两个顶点的编号。每条边都可以沿任意方向经过。顶点编号从 $1$ 开始。树的根节点编号为 $1$。最后一行包含 $*k*$ 个整数,其中 $*k*$ 是树中叶子的数量。这些数字描述了叶子应被访问的顺序。保证每个叶子在该顺序中恰好出现一次。AI (CI翻译)

Output

输出

AI (CI翻译)

If the required route doesn't exist, output -1. Otherwise, output $2*n* - 1$ numbers, describing the route. Every time the ant comes to a vertex, output it's index.

如果不存在满足要求的路线,输出 -1。否则,输出 $2*n* - 1$ 个数字,描述这条路线。每当蚂蚁到达一个顶点时,输出该顶点的编号。AI (CI翻译)

样例

Input #1
3
1 2
2 3
3
Output #1
1 2 3 2 1
InputCopy OutputCopy
Input #2
6
1 2
1 3
2 4
4 5
4 6
5 6 3
Output #2
1 2 4 5 4 6 4 2 1 3 1
InputCopy OutputCopy
Input #3
6
1 2
1 3
2 4
4 5
4 6
5 3 6
Output #3
-1
InputCopy OutputCopy
题目信息
题号
29D
时间限制
2 s
内存限制
256 MB
标签
构造
DFS 及类似
树

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(gpt-6.1-sol),如有出入请以原文为准。