博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CF981C Useful Decomposition 树 dfs 二十三 *
阅读量:6935 次
发布时间:2019-06-27

本文共 3660 字,大约阅读时间需要 12 分钟。

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 nn (2n1052≤n≤105) the number of nodes in the tree.

Each of the next n1n − 1 lines contains two integers aiai and bibi (1ai,bin1≤ai,bi≤n, aibiai≠bi) — 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 mm.

Each of the next mm lines should contain two integers uiui, vivi (1ui,vin1≤ui,vi≤n, uiviui≠vi) denoting that one of the paths in the decomposition is the simple path between nodes uiui and vivi.

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
Copy
4 1 2 2 3 3 4
output
Copy
Yes 1 1 4
input
Copy
6 1 2 2 3 3 4 2 5 3 6
output
Copy
No
input
Copy
5 1 2 1 3 1 4 1 5
output
Copy
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.

 

 

 题解:当度数大于等于3的点最多只有一个时可以遍历,否则不行。一次dfs找出以那个点为根节点的所有叶子节点(配合vector遍历)
 
#include #include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define debug(a) cout << #a << " " << a << endlusing namespace std;const int maxn = 1e5 + 10;const int mod = 1e9 + 7;typedef long long ll;ll n, u, v, vis[maxn];vector
V[maxn], ans;struct node { ll num, val;}deg[maxn];bool cmp( node a, node b ) { return a.num > b.num;}void dfs( ll k, ll last ) { vis[k] = 1; if( V[k].size() == 1 && V[k][0] == last ) { ans.push_back(k); return ; } for( ll i = 0; i < V[k].size(); i ++ ) { if( !vis[V[k][i]] ) { dfs( V[k][i], k ) ; } } return ;}int main(){ std::ios::sync_with_stdio(false); cin >> n; memset( vis, 0, sizeof(vis) ); for( ll i = 1; i <= n; i ++ ) { deg[i].val = i; } for( ll i = 1; i < n; i ++ ) { cin >> u >> v; deg[u].num ++, deg[v].num ++; V[u].push_back(v), V[v].push_back(u); } sort( deg+1, deg+n+1, cmp ); if( deg[2].num >= 3 ) { cout << "No" << endl; } else { cout << "Yes" << endl; dfs( deg[1].val, -1e9 ); cout << ans.size() << endl; for( ll i = 0; i < ans.size(); i ++ ) { cout << deg[1].val << " " << ans[i] << endl; } } return 0;}

 

转载于:https://www.cnblogs.com/l609929321/p/9250275.html

你可能感兴趣的文章
VMware虚拟机上安装linux和克隆
查看>>
Python的open函数
查看>>
IDEA在debug时修改变量值
查看>>
Dell poweredge r210进BIOS改动磁盘控制器(SATA Controller)接口模式
查看>>
Go 1.5keyword搜索文件夹、文件、文件内容_修复一个小BUG
查看>>
20160205.CCPP体系具体解释(0015天)
查看>>
匈牙利算法解决二分图匹配
查看>>
.NET Core 2.0 单元测试中初识 IOptionsMonitor<T>
查看>>
关于内存中栈和堆的区别(非数据结构中的堆和栈,区别)
查看>>
redhat6.7在线安装postgresql9
查看>>
Advanced Installer 打包后,安装包在WIN10下重启后再次运行安装的解决办法
查看>>
js实现手机页面定位
查看>>
第三方Android 模拟器流畅速度快,适合开发人员
查看>>
UWP-消息提示(仿Android)
查看>>
控件UI性能调优 -- SizeChanged不是万能的
查看>>
【git】把本地项目和远程git仓库相连通
查看>>
028_rync和inotify实现实时备份
查看>>
JAVA 线程池之Callable返回结果
查看>>
Spring Boot 静态资源处理
查看>>
人际压力背后的原因是什么?(一)
查看>>