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 ...
 
随机推荐
- 年薪5w和50w的人,区别到底在哪?
			
年薪5w和50w的人,区别到底在哪? 2017-02-22 阿青 360投资圈 文/ 阿青 许多人在职场摸爬滚打很多年并不顺利,薪酬一直上不去.职场鸡汤喝了不少,也掌握了不少职场技能,工作经验也颇为丰 ...
 - 容易出错的JavaScript题目集锦
			
容易出错的JavaScript题目集锦 1.typeof(null) 会得到什么?object,在JavaScript中null被认为是一个对象. 2.下列代码将输出控制台的是什么?为什么? 1234 ...
 - 京东Y事业部打造一体化质量管理平台
			
互联网企业质量管理的困惑 作为互联网时代的互联网企业,我们的研发模式和传统模式相比,最显著的不同在于发布节奏加快了,这个加快不是快了10%,20%,50%,而是加快了几倍,甚至几十倍,上百倍.面对加快 ...
 - 通过pl/sql连接远程Oracle数据库
			
通过PL/SQL连接远程数据库,简单的方式就是安装Oracle客户端,还有一种方式就是不安装客户端,但是需要自己创建必要的配置文件,下面主要对安装客户端的过程简单做一下记录. 网上一个不安装客户端的教 ...
 - 量化投资学习笔记34——《Python机器学习应用》课程笔记08
			
岭回归 解决某些训练样本线性相关,导致回归结果不稳定的情况. 它是一种用于共线性数据分析的有偏估计回归方法.是一种改良的最小二乘估计法. 在sklearn中使用sklearn.linear_model ...
 - (原)人体姿态识别PyraNet
			
转载请注明出处: https://www.cnblogs.com/darkknightzh/p/12424767.html 论文: Learning Feature Pyramids for Huma ...
 - 图解MySQL索引(上)—MySQL有中“8种”索引?
			
关于MySQL索引相关的内容,一直是一个让人头疼的问题,尤其是对于初学者来说.笔者曾在很长一段时间内深陷其中,无法分清"覆盖索引,辅助索引,唯一索引,Hash索引,B-Tree索引--&qu ...
 - OpenCV图像增强(python)
			
为了得到更加清晰的图像我们需要通过技术对图像进行处理,比如使用对比度增强的方法来处理图像,对比度增强就是对图像输出的灰度级放大到指定的程度,获得图像质量的提升.本文主要通过代码的方式,通过OpenCV ...
 - PAT资料,持续更新中~~~愿诸君共勉
			
<算法笔记>胡凡著,<算法笔记-上机实战训练指南>胡凡著 <经典算法大全> <C陷阱与缺陷> <C程序设计语言-K&R> 链接:ht ...
 - VMware虚拟机从安装到激活再到创建虚拟机解决黑屏、卡、死机系列问题教程第二篇
			
第二篇:在VMware中创建一个虚拟机(黑屏死机卡在最下面简单说一下你就懂了) 1.我们要打开我们已经安装好的VMware,然后点击创建新的虚拟机 2.然后选择自定义 3.下面这个默认,直接下一步 4 ...