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≤n≤105

) the number of nodes in the tree.

Each of the next n − 1

lines contains two integers ai and bi (1≤ai,bi≤n, ai≠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 m

.

Each of the next m

lines should contain two integers ui, vi (1≤ui,vi≤n, ui≠vi) denoting that one of the paths in the decomposition is the simple path between nodes ui and vi

.

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.

 
题意:给出n个结点成一颗树,问是否存在一些过一个公共结点的简单路径,如果存在则输出Yes并输出这些路径,否则输出No
思路:简单路径就是在一个路径中同一个边只能出现一次.如果存在一些过一个公共结点的简单路径则最多只能有一个结点的度数大于2
我们选一个度数最大的结点作为根结点,根结点到叶子结点的简单路径就是合法的简单路径,dfs求一下就完事了,然后就test5超时了.
其实我们要输出根结点和叶子结点,而叶子结点是度数为1的结点,所以我们统计所有度数为1的结点,把它和根节点一起输出就好了

 #include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
typedef long long ll;
const int amn=1e5+;
int n,ans,idx[amn],root;
vector<int> eg[amn];
struct node{
int i,val;
}cnt[amn];
bool cmp(node a,node b){
if(a.val==b.val)return a.i<b.i;
return a.val>b.val;
}
int main(){
scanf("%d",&n);
int x,y;
for(int i=;i<=n-;i++){
scanf("%d%d",&x,&y);
eg[x].push_back(y);
eg[y].push_back(x);
}
for(int i=;i<=n;i++){
cnt[i].val=eg[i].size();
cnt[i].i=i;
}
sort(cnt+,cnt++n,cmp);
if(cnt[].val>&&cnt[].val>){
printf("No\n");
}
else{
if(cnt[].val>)root=cnt[].i;
else{
for(int i=;i<=n;i++){
if(cnt[i].val<){
root=cnt[i].i;
break;
}
}
}
memset(idx,,sizeof idx);
printf("Yes\n%d\n",eg[root].size());
int st;
for(int i=n;i>=;i--){
if(cnt[i].val>)break;
st=i;
}
for(int i=st;i<=n;i++){
if(cnt[i].i==root)continue;
printf("%d ",root);
printf("%d\n",cnt[i].i);
}
}
}
/**
题意:给出n个结点成一颗树,问是否存在一些过一个公共结点的简单路径,如果存在则输出Yes并输出这些路径,否则输出No
思路:简单路径就是在一个路径中同一个边只能出现一次.如果存在一些过一个公共结点的简单路径则最多只能有一个结点的度数大于2
我们选一个度数最大的结点作为根结点,根结点到叶子结点的简单路径就是合法的简单路径,dfs求一下就完事了,然后就test5超时了.
其实我们要输出根结点和叶子结点,而叶子结点是度数为1的结点,所以我们统计所有度数为1的结点,把它和根节点一起输出就好了
**/

[简单路径] Useful Decomposition的更多相关文章

  1. 【Leetcode】二叉树简单路径最大和问题

    问题一:二叉树任意两个叶子间简单路径最大和 示例: -100 /   \ 2   100 /  \ 10   20 思路:这个问题适用于递归思路. 首先,将问题简单化:假设包含最大和summax的简单 ...

  2. 输出图中顶点i到顶点j之间的所有简单路径

    简单路径(不包括环) DFS遍历以及回溯得到结果 void dfs(ALGraph graph, int v, int end, bool visit[], int path[], int cnt) ...

  3. ZOJ 3213 Beautiful Meadow 简单路径 插头DP

    简单路径的题目,其实就是在状态后面多记了有多少个独立插头. 分类讨论独立插头: 1.只存在上插头或者左插头,可以选择作为独立插头. 2.都不存在上插头和左插头,选择作为独立插头的同时要标号为新的连通块 ...

  4. _DataStructure_C_Impl:求图G中从顶点u到顶点v的一条简单路径

    #pragma once #include<stdio.h> #include<stdlib.h> #define StackSize 100 typedef int Data ...

  5. 基于邻接表的长度为k的简单路径的求解

    描述 一个连通图采用邻接表作为存储结构.设计一个算法,判断无向图中任意给定的两点是否存在一条长度为k的简单路径. 输入 多组数据,每组m+3数据行.第一行有两个数字n,m和k,代表有n个顶点,m条边和 ...

  6. javascript输出图的简单路径

    <script> //图的构建 function vnode() { this.visited=0; this.vertex=0; this.arcs=new Array(); } fun ...

  7. LeetCode 简单 - 路径总和(112)

    给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和. 说明: 叶子节点是指没有子节点的节点. 示例: 给定如下二叉树,以及目标和 sum = 22 ...

  8. struts2、jsp的简单路径的简单拦截

    <filter> <filter-name>UsersFilter</filter-name> <filter-class>com.web.UsersF ...

  9. seller vue配置路径相对路径【组件 只写简单路径】

    在[webpack.base.conf.js]配置 'components': path.resolve(__dirname, '../src/components')

随机推荐

  1. pymongo bugfix后记

    有网友反馈py-mongo-sync同步异常,检查发现curosr[0]取查询结果第一个文档时报错"no such item for Cursor instance". 这里的逻辑 ...

  2. ThinkPHP判断更新是否成功的正确方法

    如何判断一个更新操作是否成功 $Model = D('Blog'); $data['id'] = 10; $data['name'] = 'update name'; $result = $Model ...

  3. JVM性能优化系列-(7) 深入了解性能优化

    7. 深入了解性能优化 7.1 影响系统性能的方方面面 影响系统性能的因素有很多,以下列举了常见的一些系统性能优化的方向: 7.2 常用的性能评价和测试指标 响应时间 提交请求和返回该请求的响应之间使 ...

  4. PostgreSQL没有认证密码就登陆了缘由

    上午同事爆出这样的问题,使用正确的用户名和错误的密码连接了postgresql数据库,竟然连上了.这不是故意这样神操作,不小心密码写错了,咋一看这样怎么能行,随便输入一个密码都能登陆上.自己测试也是同 ...

  5. 前端每日实战:147# 视频演示如何用纯 CSS 创作透视按钮的悬停特效

    效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/qJEdKb 可交互视频 此视频是可 ...

  6. VueX状态管理器 的应用

    VueX状态管理器 cnpm i vuex axios -S 1 创建Vuex 仓库 import Vue from 'vue' import Vuex from 'vuex' vue.use(Vue ...

  7. webpack基础配置(一)

    第一次写博客,有点小小的兴奋,也有一点点的慌张--- 我是一个小白,仅记录自己的学习过程,内容仅供参考,如果有问题的地方,还希望各位大牛多多指教,我菜,菜是原罪,但是我可以学-- 1.最基本的:如何使 ...

  8. java并发编程基础概念

    本次内容主要讲进程和线程.CPU核心数和线程数.CPU时间片轮转机制.上下文切换,并行和并发的基本概念以及并发编程的好处和注意事项,为java并发编程打下扎实基础. 1.什么是进程和线程 1.1 进程 ...

  9. 【WPF学习】第五十八章 理解逻辑树和可视化树

    在前面章节中,花费大量时间分析了窗口的内容模型——换句话说,研究了如何在其他元素中嵌套元素,进而构建完整的窗口. 例如,考虑下图中显示的一个非常简单的窗口,该窗口包含两个按钮.为创建该按钮,在窗口中嵌 ...

  10. 用libvlc 播放指定缓冲区中的视频流

    有时候,我们需要播放别的模块传输过来的视频流,VLC提供了这样的机制,但一般很少这样用,下面的例子实现了这样的功能. 其中用到一个关键的模块 imem.  vlc提供多种创建媒体的方式,如要从指定缓存 ...