CF981C.Useful Decomposition

普及/提高-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Ramesses knows a lot about problems involving trees (undirected connected graphs without cycles)!

He created a new useful tree decomposition, but he does not know how to construct it, so he asked you for help!

The decomposition is the splitting the edges of the tree in some simple paths in such a way that each two paths have at least one common vertex. Each edge of the tree should be in exactly one path.

Help Remesses, find such a decomposition of the tree or derermine that there is no such decomposition.

拉美西斯对涉及树(即无环的无向连通图)的问题非常了解!

他提出了一种新的、有用的树分解方法,但他不知道如何构造它,因此向你求助!

该分解要求将树的边划分为若干条简单路径,使得任意两条路径至少有一个公共顶点。树的每条边必须恰好属于其中一条路径。

请帮助拉美西斯,找出树的这样一种分解,或判定这样的分解不存在。

输入格式

The first line contains a single integer nn (2≤n≤1052 \leq n \leq 10^{5}) the number of nodes in the tree.

Each of the next n − 1n - 1 lines contains two integers aia_i and bib_i (1≤ai,bi≤n1 \leq a_i, b_i \leq n, ai≠bia_i \neq b_i) — the edges of the tree. It is guaranteed that the given edges form a tree.

第一行包含一个整数 nn(2≤n≤1052 \leq n \leq 10^{5}),表示树中节点的数量。

接下来的 n − 1n - 1 行,每行包含两个整数 aia_i 和 bib_i(1≤ai,bi≤n1 \leq a_i, b_i \leq n,且 ai≠bia_i \neq b_i),表示树的一条边。保证所给的边构成一棵树。

输出格式

If there are no decompositions, print the only line containing "No".

Otherwise in the first line print "Yes", and in the second line print the number of paths in the decomposition mm.

Each of the next mm lines should contain two integers uiu_i, viv_i (1≤ui,vi≤n1 \leq u_i, v_i \leq n, ui≠viu_i \neq v_i) denoting that one of the paths in the decomposition is the simple path between nodes uiu_i and viv_i.

Each pair of paths in the decomposition should have at least one common vertex, and each edge of the tree should be presented in exactly one path. You can print the paths and the ends of each path in arbitrary order.

If there are multiple decompositions, print any.

如果不存在这样的分解方案,请输出唯一一行“No”。

否则,第一行输出“Yes”,第二行输出分解中路径的数量 mm。

接下来的 mm 行中,每行应包含两个整数 uiu_i、viv_i(1≤ui,vi≤n1 \leq u_i, v_i \leq n,且 ui≠viu_i \neq v_i),表示分解中的一条路径为节点 uiu_i 与 viv_i 之间的简单路径。

分解中的任意两条路径至少应有一个公共顶点,且树中的每条边必须恰好出现在一条路径中。你可以以任意顺序输出各条路径,以及每条路径两个端点的顺序。

若存在多种可行的分解方案,输出任意一种即可。

输入输出样例

  • 输入#1

    4
    1 2
    2 3
    3 4

    输出#1

    Yes
    1
    1 4
  • 输入#2

    6
    1 2
    2 3
    3 4
    2 5
    3 6

    输出#2

    No
  • 输入#3

    5
    1 2
    1 3
    1 4
    1 5

    输出#3

    Yes
    4
    1 2
    1 3
    1 4
    1 5

说明/提示

The tree from the first example is shown on the picture below: The number next to each edge corresponds to the path number in the decomposition. It is easy to see that this decomposition suits the required conditions.

The tree from the second example is shown on the picture below: We can show that there are no valid decompositions of this tree.

The tree from the third example is shown on the picture below: The number next to each edge corresponds to the path number in the decomposition. It is easy to see that this decomposition suits the required conditions.

第一个样例中的树如下图所示: 每条边上标注的数字对应于该边在路径分解中所属的路径编号。容易看出,该分解满足题目所要求的条件。

第二个样例中的树如下图所示: 我们可以证明,该树不存在合法的路径分解。

第三个样例中的树如下图所示: 每条边上标注的数字对应于该边在路径分解中所属的路径编号。容易看出,该分解满足题目所要求的条件。

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

首页