CF1819C.The Fox and the Complete Tree Traversal
提高+/省选-
通过率:0%
时间限制:1.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
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 v1,v2,…,vn such that:
- the fox can jump from vertex vi to vertex vi+1,
- the fox can jump from vertex vn to vertex v1,
- all vi are pairwise distinct.
Help the fox determine if the required traversal exists.
狐狸八重爬上了神圣樱花树。树是一种连通的无向图,且不含环。
八重利用她的魔法能力在树上移动。当且仅当两个顶点之间的距离不超过 2 时,八重才能从顶点 v 跳跃到另一顶点 u。换言之,一次跳跃中,八重可以从顶点 v 跳到顶点 u,当且仅当:
- 顶点 v 和 u 由一条边直接相连;或者
- 存在某个顶点 w,使得 v 与 w 相邻,且 u 与 w 也相邻。
在八重成功取得樱花花瓣后,她思考:该树中是否存在一个环形路径 v1,v2,…,vn,满足以下条件:
- 八重能从顶点 vi 跳跃到顶点 vi+1(对所有 1≤i<n);
- 八重能从顶点 vn 跳跃到顶点 v1;
- 所有顶点 vi 两两互异。
请帮助八重判断所需的遍历路径是否存在。
输入格式
The first line contains one integer n (2≤n≤2⋅105) —the number of vertices of the tree.
Each of the following n−1 lines contains two integers u and v (1≤u,v≤n, u=v) — vertices connected by an edge. It is guaranteed that these edges form a tree.
第一行包含一个整数 n(2≤n≤2⋅105)——树的顶点数。
接下来的 n−1 行中,每行包含两个整数 u 和 v(1≤u,v≤n,u=v)——由一条边相连的两个顶点。保证这些边构成一棵树。
输出格式
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 v1,v2,…,vn (1≤vi≤n) — the vertices of the tree in traversal order.
If there are several correct traversals, output any of them.
第一行,如果所需的树遍历路径存在,则输出 "Yes"(不带引号);否则输出 "No"(不带引号)。
如果所需的树遍历存在,则在第二行输出 n 个互不相同的整数 v1,v2,…,vn(其中 1≤vi≤n),表示遍历顺序中的树的顶点。
若存在多种正确的遍历方式,输出任意一种即可。
输入输出样例
输入#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.

第一个示例中的树如下图所示。粗箭头表示狐狸的路径。

在第二个示例中,任意三个互不相同的顶点构成的序列均为合法路径,因为狐狸可以从任意顶点跳至任意其他顶点。
第三个示例中的树如下图所示。可以证明,该树不存在满足要求的路径。

输入解题思路,AI测评打分。不知道怎么写?