Codeforces Add on a Tree
1 second
256 megabytes
standard input
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.
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.
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).
2
1 2
YES
3
1 2
2 3
NO
5
1 2
1 3
1 4
2 5
NO
6
1 2
1 3
1 4
2 5
2 6
YES
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的更多相关文章
- Codeforces 280C Game on tree【概率DP】
Codeforces 280C Game on tree LINK 题目大意:给你一棵树,1号节点是根,每次等概率选择没有被染黑的一个节点染黑其所有子树中的节点,问染黑所有节点的期望次数 #inclu ...
- Codeforces A. Game on Tree(期望dfs)
题目描述: Game on Tree time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- Codeforces 461B Appleman and Tree(木dp)
题目链接:Codeforces 461B Appleman and Tree 题目大意:一棵树,以0节点为根节点,给定每一个节点的父亲节点,以及每一个点的颜色(0表示白色,1表示黑色),切断这棵树的k ...
- Codeforces 1129 E.Legendary Tree
Codeforces 1129 E.Legendary Tree 解题思路: 这题好厉害,我来复读一下官方题解,顺便补充几句. 首先,可以通过询问 \(n-1\) 次 \((S=\{1\},T=\{ ...
- Codeforces Round #781(C. Tree Infection)
Codeforces Round #781 C. Tree Infection time limit per test 1 second memory limit per test 256 megab ...
- Codeforces 1189D2. Add on a Tree: Revolution
传送门 首先可以证明一颗树合法的充分必要条件是不存在某个节点的度数为 $2$ 首先它是必要的,考虑任意一条边连接的两点如果存在某一点 $x$ 度数为 $2$ ,那么说明 $x$ 还有连一条边出去,那么 ...
- CodeForces 396C On Changing Tree
On Changing Tree Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces ...
- Codeforces 734E. Anton and Tree 搜索
E. Anton and Tree time limit per test: 3 seconds memory limit per test :256 megabytes input:standard ...
- codeforces 161D Distance in Tree 树形dp
题目链接: http://codeforces.com/contest/161/problem/D D. Distance in Tree time limit per test 3 secondsm ...
随机推荐
- 让git push命令不再需要密码
最近利用jekyll写博客,为的就是博客管理方便,但是在上传博客的时候使用git push命令每次都得输入github帐号和密码特别的不方便,于是就搜了一下. 在这篇文章里提到,GitHub获得远程库 ...
- Linux上centOs6+安装mysql5.7详细教程 - 前端小鱼塘
https://coyhom.github.io/ 人类的本质是复读机,作为一个非linux专业人员学习linux最好的办法是重复 环境centos6.5 版本5.7 1: 检测系统是否自带安装mys ...
- Magic Methods 5
描述符 : 将某种特殊类型的类的实例指派给另一个类的属性. 特殊类型为以下方法的1-3个 : __get__(self, instance, owner):用于访问属性,它返回属性的值 __set__ ...
- windows7 64位系统下无法运行ipython
windows7 64位系统下无法运行ipython | Creator 导航 导航 博客 分类 标签 友链 关于 大专栏 windows7 64位系统下无法运行ipythontent-post L ...
- resourcequota分析(一)-evaluator-v1.5.2
什么是evaluator 大家都知道,Kubernetes中使用resourcequota对配额进行管理.配额的管理涉及两个步骤:1.计算请求所需要的资源:2.比较并更新配额.所以解读resource ...
- 【转载】Oracle Spatial中SDO_Geometry详细说明
转载只供个人学习参考,查看请前往原出处:http://www.cnblogs.com/upDOoGIS/archive/2009/05/20/1469871.html 相关微博:oracle 创建SD ...
- CSS中怎么设置元素水平垂直居中?
记录怎么使用text-align与vertical-align属性设置元素在容器中垂直居中对齐.text-align与vertical-align虽然都是设置元素内部对齐方式的,但两者的用法还是有略微 ...
- TCP可靠传输的工作原理
TCP可靠传输的工作原理 一.停止等待协议 1.1.简介 在发送完一个分组后,必须暂时保留已发送的分组的副本. 分组和确认分组都必须进行编号. 超时计时器的重传时间应当比数据在分组传输的平均往返时间更 ...
- 痞子衡嵌入式:恩智浦i.MX RT1xxx系列MCU启动那些事(11.2)- FlexSPI NOR连接方式大全(RT1060/1064(SIP))
大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家介绍的是恩智浦i.MX RT1060/1064(SIP)两款MCU的FlexSPI NOR启动的连接方式. 上一篇文章<FlexSPI N ...
- <select>标签,不要在select标签中写value属性!!!
<select> select标签,一个选择框标签,在开发中很多时候会用到这个标签,例如选择生日19**年,或者在segmentfault中编辑文章时选择'原创','转载',还是'翻译'等 ...