Add on a Tree
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.

You are given a tree with nn nodes. In the beginning, 00 is written on all edges. In one operation, you can choose any 22 distinct leaves uu, vvand any real number xx and add xx to values written on all edges on the simple path between uu and vv.

For example, on the picture below you can see the result of applying two operations to the graph: adding 22 on the path from 77 to 66, and then adding −0.5−0.5 on the path from 44 to 55.

Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?

Leaf is a node of a tree of degree 11. Simple path is a path that doesn't contain any node twice.

Input

The first line contains a single integer nn (2≤n≤1052≤n≤105) — the number of nodes.

Each of the next n−1n−1 lines contains two integers uu and vv (1≤u,v≤n1≤u,v≤n, u≠vu≠v), meaning that there is an edge between nodes uu and vv. It is guaranteed that these edges form a tree.

Output

If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output "NO".

Otherwise, output "YES".

You can print each letter in any case (upper or lower).

Examples
input

Copy
2
1 2
output

Copy
YES
input

Copy
3
1 2
2 3
output

Copy
NO
input

Copy
5
1 2
1 3
1 4
2 5
output

Copy
NO
input

Copy
6
1 2
1 3
1 4
2 5
2 6
output

Copy
YES
Note

In the first example, we can add any real xx to the value written on the only edge (1,2)(1,2).

In the second example, one of configurations that we can't reach is 00 written on (1,2)(1,2) and 11 written on (2,3)(2,3).

Below you can see graphs from examples 33, 44:

题意:给一颗n个节点边权都为0的树,现在有一种操作可以任意选择这颗树上的两个叶子节点(度数为1的节点)使得这两个节点简单路径(没有重复节点的路径)上的边权加上一个任意实数,
问给定节点的连接关系形成一颗树,能否有限次使用上述操作使得树上的边权可以为任意实数(可能每一条边都不一样)
思路:样例2表明,如果存在一个节点恰好只连接了两个节点,则其只连了两条边,形成一条链,则条链必定会在两个根节点的简单路径上,而简单路径上的边权是同时加一个实数的,所以这个节点的两条边必定同时加一个实数且两条边的值必定相同
故这两条边无法在有限次操作内到达权值不同的情况,应输出NO,其他情况都可以通过某些边加上一些值,某些边减去一些值得到,输出YES

 #include<bits/stdc++.h>
using namespace std;
const int amn=1e5+;
vector<int> a[amn];
int main(){
int n,f=,u,v;
cin>>n;
for(int i=;i<n;i++){
cin>>u>>v;
a[u].push_back(v);
a[v].push_back(u);
}
for(int i=;i<=n;i++){
if(a[i].size()==){ ///若存在一个点度数为2,则输出NO
f=;
break;
}
}
if(f)printf("YES\n"); ///否则输出YES
else printf("NO\n");
}
/***
给一颗n个节点边权都为0的树,现在有一种操作可以任意选择这颗树上的两个叶子节点(度数为1的节点)使得这两个节点简单路径(没有重复节点的路径)上的边权加上一个任意实数,
问给定节点的连接关系形成一颗树,能否有限次使用上述操作使得树上的边权可以为任意实数(可能每一条边都不一样)
样例2表明,如果存在一个节点恰好只连接了两个节点,则其只连了两条边,形成一条链,则条链必定会在两个根节点的简单路径上,而简单路径上的边权是同时加一个实数的,所以这个节点的两条边必定同时加一个实数且两条边的值必定相同
故这两条边无法在有限次操作内到达权值不同的情况,应输出NO,其他情况都可以通过某些边加上一些值,某些边减去一些值得到,输出YES
***/

Codeforces Add on a Tree的更多相关文章

  1. Codeforces 280C Game on tree【概率DP】

    Codeforces 280C Game on tree LINK 题目大意:给你一棵树,1号节点是根,每次等概率选择没有被染黑的一个节点染黑其所有子树中的节点,问染黑所有节点的期望次数 #inclu ...

  2. Codeforces A. Game on Tree(期望dfs)

    题目描述: Game on Tree time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  3. Codeforces 461B Appleman and Tree(木dp)

    题目链接:Codeforces 461B Appleman and Tree 题目大意:一棵树,以0节点为根节点,给定每一个节点的父亲节点,以及每一个点的颜色(0表示白色,1表示黑色),切断这棵树的k ...

  4. Codeforces 1129 E.Legendary Tree

    Codeforces 1129 E.Legendary Tree 解题思路: 这题好厉害,我来复读一下官方题解,顺便补充几句. 首先,可以通过询问 \(n-1​\) 次 \((S=\{1\},T=\{ ...

  5. Codeforces Round #781(C. Tree Infection)

    Codeforces Round #781 C. Tree Infection time limit per test 1 second memory limit per test 256 megab ...

  6. Codeforces 1189D2. Add on a Tree: Revolution

    传送门 首先可以证明一颗树合法的充分必要条件是不存在某个节点的度数为 $2$ 首先它是必要的,考虑任意一条边连接的两点如果存在某一点 $x$ 度数为 $2$ ,那么说明 $x$ 还有连一条边出去,那么 ...

  7. CodeForces 396C On Changing Tree

    On Changing Tree Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces ...

  8. Codeforces 734E. Anton and Tree 搜索

    E. Anton and Tree time limit per test: 3 seconds memory limit per test :256 megabytes input:standard ...

  9. codeforces 161D Distance in Tree 树形dp

    题目链接: http://codeforces.com/contest/161/problem/D D. Distance in Tree time limit per test 3 secondsm ...

随机推荐

  1. Jetson TX2镜像刷板法

    使用Nvidia官方自带的脚本,备份镜像.恢复镜像,快速在新板子中部署DL环境 在之前的一篇博客中,详细介绍了使用JetPack刷系统以及使用离线包部署DL环境(cuda.cudnn.opencv.c ...

  2. webpack插件

    插件 plugins:[ new ExtractTextPlugin.extrct({ }) //创建html new HtmlWebpackPlugin({ title:"first pa ...

  3. github浏览器无法访问,并且idea无法push项目

    github浏览器无法访问,并且idea无法push项目 原因:前一晚还能正常访问github,今天就无法提交项目了.前一步的操作为删库,然后改库.估计是因为dns出现了问题,具体问题不知道. 网上一 ...

  4. SVN版本控制说明与相关指令

    SVN版本控制说明 目的 多个版本中并行开发,提高开发效率: 保证各个版本和各个环境(开发.测试.主干)的独立,避免相互影响: 通过分支与主干的合并,这样主干永远是最新.最高版本,并且都在后面的测试中 ...

  5. text-decoration与color属性

    text-decoration属性值 如果指定某个标签的text-decoration属性时,希望为其添加多个样式(比如:上划线.下划线.删除线),那么需要把所有的值合并到一个规则中才会生效 p{ t ...

  6. 微信h5页面audio标签在ios下不能自动播放

    背景介绍:在一个h5页面中,当用户提交表单到后台,后台返回的结果成功的话,开始自动播放背景音乐 出现的问题:在安卓手机上正常,iOS中没有反应 后来网上一番搜索后了解到时因为iOS不允许自动播放音乐, ...

  7. SpringBoot学习笔记(一)入门简介

    一.SpringBoot 入门简介 整体讲解内容概况: 1.1 简介 简化Spring应用开发的一个框架: 整个Spring技术栈的一个大整合: J2EE开发的一站式解决方案. Spring Boot ...

  8. (数据科学学习手札79)基于geopandas的空间数据分析——深入浅出分层设色

    本文对应代码和数据已上传至我的Github仓库https://github.com/CNFeffery/DataScienceStudyNotes 1 简介 通过前面的文章,我们已经对geopanda ...

  9. java线程间的协作

    本次内容主要讲等待/通知机制以及用等待/通知机制手写一个数据库连接池. 1.为什么线程之间需要协作 线程之间相互配合,完成某项工作,比如:一个线程修改了一个对象的值,而另一个线程感知到了变化,然后进行 ...

  10. php不用第三个变量,交换两个数的值

    //字符串版本 结合使用substr,strlen两个方法实现 $a="a"; $b="b"; echo '交换前 $a:'.$a.',$b:'.$b.'< ...