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 ...
随机推荐
- linux 网络编程 2---(TCP编程)
流程 服务器:server 创建套接字 socket( ) 填充服务器网络信息结构体 sockaddr_in 将套接字与服务器网络信息结构体绑定 bind( ) 将套接字设置为被动监听状态 liste ...
- 20154327 Exp2 后门原理与实践
实践内容 使用netcat和socat.msf-meterpreter等工具获得主机权限,并进行一些恶意行为,如监控摄像头.记录键盘输入.截屏等. 详情见实验指导书 实践过程 netcat netca ...
- 【转】odoo学习之:Environment
Environment类提供了对ORM对象的封装,同时提供了对注册类的访问,记录集的缓存,以及管理重计算的数据结构. 对于继承了Model的类来说可以直接通过self.env对Environment进 ...
- 北京Uber优步司机奖励政策(1月25日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 成都Uber优步司机奖励政策(3月3日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 生产环境 tidb部署实践
TiDB 简介 TiDB 是 PingCAP 公司受 Google Spanner / F1 论文启发而设计的开源分布式 HTAP (Hybrid Transactional and Analytic ...
- JS dataTables
原文地址: http://www.cnblogs.com/haogj/archive/2011/03/04/1971328.html 数据来源有四种: 1. 网页DOM对象 $(document) ...
- VIN码识别,车架号识别,OCR扫描工具
近年二手车交易市场火爆,对二手车估值需要了详细解二手车的历史状况,车架号(VIN码)是车辆唯一的身份标识,也是了解二手车车况的入口,车商和二手车平台会频繁的进行车况查询,VIN码扫描识别技术给车辆估值 ...
- Linux命令应用大词典-第3章 文本编辑器
3.1 vi:文本编辑器 3.2 nano:编辑器 3.3 view:文办编辑器 3.4 ex:文本编辑器 3.5 ed:文本编辑器 3.6 red:文本编辑器 3.1 vi:文本编辑器 1.对文本创 ...
- 第五模块:WEB开发基础 第3章·BootStrap&JQuery开发
01-JQuery介绍 02-jQuery文件引入和加载的区别 03-jQuery的基础选择器 04-jQuery的层级选择器 05-jQuery的基本过滤选择器 06-jQuery的属性选择器 07 ...