C. Useful Decomposition
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

Input

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

Each of the next $$$n - 1$$$ lines contains two integers $$$a_i$$$ and $$$b_i$$$ ($$$1 \leq a_i, b_i \leq n$$$, $$$a_i \neq b_i$$$) — the edges of the tree. It is guaranteed that the given edges form a tree.

Output

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 $$$m$$$.

Each of the next $$$m$$$ lines should contain two integers $$$u_i$$$, $$$v_i$$$ ($$$1 \leq u_i, v_i \leq n$$$, $$$u_i \neq v_i$$$) denoting that one of the paths in the decomposition is the simple path between nodes $$$u_i$$$ and $$$v_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.

Examples
Input
4
1 2
2 3
3 4
Output
Yes
1
1 4
Input
6
1 2
2 3
3 4
2 5
3 6
Output
No
Input
5
1 2
1 3
1 4
1 5
Output
Yes
4
1 2
1 3
1 4
1 5
Note

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.

【题意】
给一个无向边的树,要求拆成若干条简单路径,并且这些路径都经过一个公共点。给出任意一个解决方案,如不存在输出No。

【分析】
所有的路径都有公共点,如果解决方案存在的话,那么倒着推回去,把公共点看成根节点,这棵树一定是所有的路径都从根结点出发,且都不分叉的,因为在满足这个性质时才能拆分,如果不满足,则一定有一条路径不经过根节点。
所以检查一棵树能否拆分,只用检查分叉点是否唯一就行了。而拆下来的路径,一端一定是根结点,而另一端就是这个树的所有叶子结点。

【代码】

#include<stdio.h>
#define N_max 100005 int cnt[N_max] = { 0 };//记录所有点的度数
int end[N_max] = { 0 }, ne=0;//记录所有端点序号 int main() {
int n;
scanf("%d", &n);
int a1, a2;
for (int i = 0; i < n - 1; ++i) {
scanf("%d %d", &a1, &a2);
cnt[a1]++;
cnt[a2]++;
}
//检查分叉处是否唯一,并记录到aim
int aim = -1;
for (int i = 1; i <= n; ++i) {
if (cnt[i] >= 3) {
if (aim == -1) aim = i;
else {
printf("No");
return 0;
}
}
//顺带记录端点
if (cnt[i] == 1)end[ne++]=i;
} printf("Yes\n");
//没有分叉,只有一条路径,直接输出两端
if (aim == -1) {
printf("1\n%d %d\n",end[0] ,end[1]);
return 0;
}
//将分叉点看成根节点,每一条边都是从根出发的,拆下来就行了
printf("%d\n", ne);
for (int t = 0; t < ne; ++t) {
printf("%d %d\n", aim, end[t]);
}
return 0;
}

codeforces 981 C.Useful Decomposition的更多相关文章

  1. Codeforces 981 D.Bookshelves(数位DP)

    Codeforces 981 D.Bookshelves 题目大意: 给n个数,将这n个数分为k段,(n,k<=50)分别对每一段求和,再将每个求和的结果做与运算(&).求最终结果的最大 ...

  2. Codeforces 981 E - Addition on Segments

    E - Addition on Segments 思路: dp dp[i]表示构成i的区间的右端点 先将询问按r排序 然后,对于每次询问,每次枚举 i 从 n-x 到 1,如果dp[i] >= ...

  3. Codeforces 981 共同点路径覆盖树构造 BFS/DP书架&最大值

    A /*Huyyt*/ #include<bits/stdc++.h> #define mem(a,b) memset(a,b,sizeof(a)) #define pb push_bac ...

  4. [CodeForces]981C Useful Decomposition

    李煜东dalao今天给我们讲课了QwQ ppt上一道题 英文题说一下题意吧,以后又看不懂了 将一棵树分割成多个简单路径,每个边只能在一条路径上,但至少有一个公共节点. 输出简单路径分割方法/No 由题 ...

  5. Codeforces Avito Code Challenge 2018 D. Bookshelves

    Codeforces Avito Code Challenge 2018 D. Bookshelves 题目连接: http://codeforces.com/contest/981/problem/ ...

  6. Codeforces 981D Bookshelves(按位贪心+二维DP)

    题目链接:http://codeforces.com/contest/981/problem/D 题目大意:给你n本书以及每本书的价值,现在让你把n本书放到k个书架上(只有连续的几本书可以放到一个书架 ...

  7. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  8. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  9. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

随机推荐

  1. python--模块之time,datetime时间模块

    time: 表示时间的三种方式:时间戳.格式化的时间字符串.元组时间戳是计算机能够识别的时间:时间字符串是我们能够看懂的时间:元组是用来操作时间: 导入时间模块import time 1,时间戳(ti ...

  2. 初识Tarjan

    Tarjan,一个十分有用的东西,可以求有向图的强连通分量,复杂度达到O(V+E). Tarjan算法是基于对图深度优先搜索的算法,每个强连通分量为搜索树中的一棵子树.搜索时,把当前搜索树中未处理的节 ...

  3. 使用Google Cloud Messaging (GCM),PHP 开发Android Push Notifications (安卓推送通知)

    什么是GCM? Google Cloud  Messaging (GCM) 是Google提供的一个服务,用来从服务端向安卓设备发送推送通知. GCM分为客户端和服务端开发. 这里我们只介绍服务端开发 ...

  4. Hadoop3.0新特性

    1. Hadoop3.0简介 Hadoop 2.0是基于JDK 1.7开发的,而JDK 1.7在2015年4月已停止更新,这直接迫使Hadoop社区基于JDK1.8重新发布一个新的Hadoop版本,而 ...

  5. AIX7.1删除大批量文件(百万级、千万级)

    假设/data/test目录下含有数百万上千万的文件需要删除,可以选择的方式如下: 1.如果文件名不包含空白符.引号等特殊字符,则可以使用如下命令: find /data/test -type f | ...

  6. 接口测试工具postman(八)上传文件接口

    涉及到选择文件的接口,在[Body]页签下,key选择File选项,会显示“选择文件”按钮,选择本地的文件

  7. 几个常见移动平台浏览器的User-Agent

    之前介绍的手机站跳转url的一片文稿中提到,依据User Agent判断终端的方法.(文章地址:http://www.cnblogs.com/dereksunok/p/3664169.html ) 若 ...

  8. [转载]Tensorflow中reduction_indices 的用法

    Tensorflow中reduction_indices 的用法 默认时None 压缩成一维

  9. HDU 4300 Clairewd’s message (next函数的应用)

    题意:给你一个明文对密文的字母表,在给你一段截获信息,截获信息前半段是密文,后半段是明文,但不清楚它们的分界点在哪里,密文一定是完整的,明文可能是残缺的,求完整的信息串(即完整的密文+明文串). 题解 ...

  10. Python运行的方式

    Python的运行方式多种多样,下面列举几种: 交互式 在命令行中输入python,然后在>>>提示符后面输入Python语句,这里需要注意: 1 语句前面不能有空格,否则会报错 2 ...