codeforces 981 C.Useful Decomposition
1 second
256 megabytes
standard input
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.
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.
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.
4
1 2
2 3
3 4
Yes
1
1 4
6
1 2
2 3
3 4
2 5
3 6
No
5
1 2
1 3
1 4
1 5
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.
【题意】
给一个无向边的树,要求拆成若干条简单路径,并且这些路径都经过一个公共点。给出任意一个解决方案,如不存在输出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的更多相关文章
- Codeforces 981 D.Bookshelves(数位DP)
Codeforces 981 D.Bookshelves 题目大意: 给n个数,将这n个数分为k段,(n,k<=50)分别对每一段求和,再将每个求和的结果做与运算(&).求最终结果的最大 ...
- Codeforces 981 E - Addition on Segments
E - Addition on Segments 思路: dp dp[i]表示构成i的区间的右端点 先将询问按r排序 然后,对于每次询问,每次枚举 i 从 n-x 到 1,如果dp[i] >= ...
- Codeforces 981 共同点路径覆盖树构造 BFS/DP书架&最大值
A /*Huyyt*/ #include<bits/stdc++.h> #define mem(a,b) memset(a,b,sizeof(a)) #define pb push_bac ...
- [CodeForces]981C Useful Decomposition
李煜东dalao今天给我们讲课了QwQ ppt上一道题 英文题说一下题意吧,以后又看不懂了 将一棵树分割成多个简单路径,每个边只能在一条路径上,但至少有一个公共节点. 输出简单路径分割方法/No 由题 ...
- Codeforces Avito Code Challenge 2018 D. Bookshelves
Codeforces Avito Code Challenge 2018 D. Bookshelves 题目连接: http://codeforces.com/contest/981/problem/ ...
- Codeforces 981D Bookshelves(按位贪心+二维DP)
题目链接:http://codeforces.com/contest/981/problem/D 题目大意:给你n本书以及每本书的价值,现在让你把n本书放到k个书架上(只有连续的几本书可以放到一个书架 ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
随机推荐
- java 第七章 面向对象高级特性
一.类的继承 (一)继承的含义 1.在Java中定义一个类时,让该类通过关键字extends继承一个已有的类,这就是类的继承(泛化). 2.被继承的类称为父类(超类,基类),新的类称为子类(派生类). ...
- 【Hutool】Hutool工具类之Http工具——HttpUtil
最简单最直接的上手可以参见参考文档:http://hutool.mydoc.io/?t=216015 Http协议的介绍,请参考web随笔:http://www.cnblogs.com/jiang ...
- ROS Twist和Odometry消息类型使用(Python)
消息类型: 1. Twist - 线速度角速度 通常被用于发送到/cmd_vel话题,被base controller节点监听,控制机器人运动 geometry_msgs/Twist geometry ...
- netty之管道处理流程
1.我们在使用netty的是有都会存在将channelBuffer的数据处理成相应的String或者自定义数据.而这里主要是介绍管道里面存在的上行和下行的数据处理方式 2.通过一张图片来看一下具体管道 ...
- 韩国KT软件NB-IOT开发记录V150(2)FOTA差分包生成
1. 生成差分包
- Redis系列四 Redis常见配置
redis.conf常见配置 参数说明redis.conf 配置项说明如下:1. Redis默认不是以守护进程的方式运行,可以通过该配置项修改,使用yes启用守护进程 daemonize no2. ...
- 抽样分布(2) t分布
定义 t分布 设X ~ N(0,1),Y ~ χ2(n),且X,Y相互独立,则称随机变量 服从自由度为n的t分布(学生氏分布) 记为 t~t(n),其概率密度为 由于tn(x)是偶函数,其图形关于y轴 ...
- CentOS 7.2安装11g Grid Infrastructure
Preface Oracle claimed that 11g RAC is supported on Redhat Linux 7 and above version,but the ...
- 利用JSON Schema校验JSON数据格式
最近笔者在工作中需要监控一批http接口,并对返回的JSON数据进行校验.正好之前在某前端大神的分享中得知这个神器的存在,调研一番之后应用在该项目中,并取得了不错的效果,特地在此分享给各位读者. 什么 ...
- Selenium(Python)驱动Firefox浏览器
我的版本是Firefox Setup 52.7.0.exe+geckodriver-v0.15.0-win64.zip, 把驱动geckodriver.exe放到Python安装目录下, 也可以指定驱 ...