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 ...
随机推荐
- 查漏补缺:Linux进程与线程的区别
1.概念的区别 进程:是具有独立功能的程序在一个数据集合上运行的过程,是系统进行资源分配的基本单位,也是调度运行的基本单位.一个进程中可以包含多个线程. 线程:是进程的一个实体,是CPU调度和分派的基 ...
- 京东Y事业部打造一体化质量管理平台
互联网企业质量管理的困惑 作为互联网时代的互联网企业,我们的研发模式和传统模式相比,最显著的不同在于发布节奏加快了,这个加快不是快了10%,20%,50%,而是加快了几倍,甚至几十倍,上百倍.面对加快 ...
- Node学习(二) --使用http和fs模块实现一个简单的服务器
1.创建一个www目录,存储静态文件1.html.1.jpg. * html文件内容如下: 12345678910111213 <html lang="en">< ...
- 杀死众筹的N种方法:没想到山寨大军也参与了
众筹作为当下创业者筹集资金,将创意变为现实的最重要手段之一,正面临着越来越多的困难,甚至衍生出杀死众筹的N种方法.甚至这些方法还分为了两类,就众筹本身看,杀死它们的主要方法是:创业者卷钱跑路. ...
- golang 统计系统测试覆盖率
golang 统计系统测试覆盖率 参考资料 https://www.elastic.co/blog/code-coverage-for-your-golang-system-tests https:/ ...
- python之面向对象02
在python中方法名如果是__xxxx__()的,那么就有特殊的功能,因此叫做“魔法”方法 1. __init__()方法 class Cat: def __init__(self,newname, ...
- 后渗透阶段之基于MSF的路由转发
目录 反弹MSF类型的Shell 添加内网路由 MSF的跳板功能是MSF框架中自带的一个路由转发功能,其实现过程就是MSF框架在已经获取的Meterpreter Shell的基础上添加一条去往“内网” ...
- C# 关于位运算的学习笔记
一.理解什么是位运算 程序中的所有内容在计算机内存中都是以二进制的形式储存的(即:0或1),简单来说位运算就是直接对在内存中的二进制数的每位进行运算操作. 二.学习前先了解一下有哪些运算,运算符都怎么 ...
- jdk1.5之后,classpath不需要再配置
1.5之后更智能了. 设置了classpath之后,当前目录失效(在classpath中没找到就报错,不会再在当前目录继续找),所以要把当前目录添加到classpath,永久环境变量添加 .,临时环境 ...
- Deepin环境下启动Pycharm没有启动图标解决办法
小伙伴们在deepin下运行pycharm时,是不是需要通过sh文件启动? 下面告诉大家如何将pycharm图标放在桌面上: 1.在桌面打开终端,输入命令: sudo gedit /usr/share ...