A15947 | The Fox and the Complete Tree Traversal
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
The fox Yae climbed the tree of the Sacred Sakura. A tree is a connected undirected graph that does not contain cycles.
The fox uses her magical powers to move around the tree. Yae can jump from vertex $v$ to another vertex $u$ if and only if the distance between these vertices does not exceed $2$ . In other words, in one jump Yae can jump from vertex $v$ to vertex $u$ if vertices $v$ and $u$ are connected by an edge, or if there exists such vertex $w$ that vertices $v$ and $w$ are connected by an edge, and also vertices $u$ and $w$ are connected by an edge.
After Yae was able to get the sakura petal, she wondered if there was a cyclic route in the tree $v_1, v_2, \ldots, v_n$ such that:
- the fox can jump from vertex $v_i$ to vertex $v_{i + 1}$ ,
- the fox can jump from vertex $v_n$ to vertex $v_1$ ,
- all $v_i$ are pairwise distinct.
Help the fox determine if the required traversal exists.
The fox uses her magical powers to move around the tree. Yae can jump from vertex $v$ to another vertex $u$ if and only if the distance between these vertices does not exceed $2$ . In other words, in one jump Yae can jump from vertex $v$ to vertex $u$ if vertices $v$ and $u$ are connected by an edge, or if there exists such vertex $w$ that vertices $v$ and $w$ are connected by an edge, and also vertices $u$ and $w$ are connected by an edge.
After Yae was able to get the sakura petal, she wondered if there was a cyclic route in the tree $v_1, v_2, \ldots, v_n$ such that:
- the fox can jump from vertex $v_i$ to vertex $v_{i + 1}$ ,
- the fox can jump from vertex $v_n$ to vertex $v_1$ ,
- all $v_i$ are pairwise distinct.
Help the fox determine if the required traversal exists.
输入格式
The first line contains one integer $n$ ( $2 \le n \le 2 \cdot 10^5$ ) —the number of vertices of the tree.
Each of the following $n - 1$ lines contains two integers $u$ and $v$ ( $1 \le u, v \le n$ , $u \ne v$ ) — vertices connected by an edge. It is guaranteed that these edges form a tree.
Each of the following $n - 1$ lines contains two integers $u$ and $v$ ( $1 \le u, v \le n$ , $u \ne v$ ) — vertices connected by an edge. It is guaranteed that these edges form a tree.
输出格式
On the first line, print "Yes" (without quotes) if the required route of the tree exists, or "No" (without quotes) otherwise.
If the required tree traversal exists, on the second line print $n$ integers of different integers $v_1, v_2, \ldots, v_n$ ( $1 \le v_i \le n$ ) — the vertices of the tree in traversal order.
If there are several correct traversals, output any of them.
If the required tree traversal exists, on the second line print $n$ integers of different integers $v_1, v_2, \ldots, v_n$ ( $1 \le v_i \le n$ ) — the vertices of the tree in traversal order.
If there are several correct traversals, output any of them.
输入输出样例
输入 #1
5 1 2 1 3 3 4 3 5
输出 #1
Yes 4 5 1 2 3
输入 #2
3 1 2 1 3
输出 #2
Yes 1 2 3
输入 #3
15 1 2 1 3 2 4 2 5 3 6 3 7 4 8 4 9 5 10 5 11 6 12 6 13 7 14 7 15
输出 #3
No
The tree from the first example is shown below. The bold arrows indicate the fox's route.
In the second example, any sequence of three different vertices is a correct route, because the fox can jump from any vertex to any vertex.
The tree from the third example is shown below. It can be shown that there is no required route for it.

In the second example, any sequence of three different vertices is a correct route, because the fox can jump from any vertex to any vertex.
The tree from the third example is shown below. It can be shown that there is no required route for it.

C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted