CF682C Alyona and the Tree
题意翻译
题目描述
给你一棵树,边与节点都有权值,根节点为1,现不停删除叶子节点形成新树,问最少删掉几个点,能使得最后剩下的树内,∀v与其子树内∀u间边权的和小于点u权值
输入输出格式
输入格式:
第一行,节点个数n(1≤n≤1e5)
第二行,n个整数——各节点的权值ai(1≤ai≤1e9)
接下来的n-1行,每行两个整数pi与ci(1≤pi≤n,−1e9≤ci≤1e9),分别表示编号为i+1的节点的父节点以及该边的边权
输出格式:
一个整数,最少需要删除的点的个数
输入输出样例
9
88 22 83 14 95 91 98 53 11
3 24
7 -8
1 67
1 64
9 65
5 12
6 -80
3 8
5
代码
思维题。
首先可以转化成保留多少个节点。
然后每次累加取max(0,sum+e[i].val)
比如说我们v[u]=10,而此时sum=-1000,而∑e[i].val=20,显然这种情况是不可法的
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+;
int v[maxn],head[maxn];
struct edge
{
int to,next,val;
}e[maxn];
int cnt=;
int size=;
inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=(x<<)+(x<<)+ch-'';ch=getchar();}
return x*f;
}
void addedge(int u,int v,int w)
{
e[++size].to=v;e[size].val=w;e[size].next=head[u];head[u]=size;
}
void dfs(int u,int sum)
{
if(sum>v[u])return;
cnt++;
for(int i=head[u];i;i=e[i].next)
{
int to=e[i].to;
dfs(to,max(,sum+e[i].val));
}
}
int main()
{
int n=read();
for(int i=;i<=n;i++)
v[i]=read();
for(int i=;i<=n;i++)
{
int v=read(),w=read();
addedge(v,i,w);
}
dfs(,);
printf("%d",n-cnt);
return ;
}
CF682C Alyona and the Tree的更多相关文章
- Codeforces Round #381 (Div. 2)D. Alyona and a tree(树+二分+dfs)
D. Alyona and a tree Problem Description: Alyona has a tree with n vertices. The root of the tree is ...
- codeforces 381 D Alyona and a tree(倍增)(前缀数组)
Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- Codeforces Round #381 (Div. 1) B. Alyona and a tree dfs序 二分 前缀和
B. Alyona and a tree 题目连接: http://codeforces.com/contest/739/problem/B Description Alyona has a tree ...
- Codeforces Round #381 (Div. 2) D. Alyona and a tree 树上二分+前缀和思想
题目链接: http://codeforces.com/contest/740/problem/D D. Alyona and a tree time limit per test2 secondsm ...
- CodeForces 682C Alyona and the Tree (树+dfs)
Alyona and the Tree 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/C Description Alyona ...
- Alyona and a tree
Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- Codeforces Round #358 (Div. 2) C. Alyona and the Tree 水题
C. Alyona and the Tree 题目连接: http://www.codeforces.com/contest/682/problem/C Description Alyona deci ...
- Codeforces Round #381 (Div. 2) D. Alyona and a tree dfs序+树状数组
D. Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- Codeforces Round #358 (Div. 2) C. Alyona and the Tree dfs
C. Alyona and the Tree time limit per test 1 second memory limit per test 256 megabytes input standa ...
随机推荐
- 【shell】awk的next用法
awk的next相当于循环中continue的作用,next后面的语句将不再执行. 例如,下面的例子中,包含数字3的那行的print语句没有被执行. [root]$ seq | awk '{print ...
- 几道好玩的JS题目,看看你能答中几道。
1,for (var i = 0; i < 5; i++) { console.log(i); } 这几行代码会输出什么? 输出 0 1 2 3 4 2,for (var i = 0; i &l ...
- 开发一个chrome插件:将百度搜索热点屏蔽掉!
每次百度搜索,搜索结果的右边总是出现些乱七八糟的搜索热点(推的都是些什么玩意,高校替课和我有毛关系,几个悲伤的热点我用星号顶掉了). 强迫症想把它隐藏掉,我用的是chrome浏览器,受adblock( ...
- 【java工具类】POI导出excel
POI的maven依赖:<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi&l ...
- Maven手动命令行导入ojdbc6
Maven项目中导入Oracle的驱动包时,可能会出现像我一样下载资源不成功的情况,如下图所示: 出现这种情况的原因其实是因为Oracle的授权问题,这样的话,我们在需要使用Oracle的驱动包时, ...
- const与#define的区别
1.const (1)为什么需要const成员函数? C中常用:“ #define 变量名 变量值”定义一个值替代,然而却有个致命缺点:缺乏类型检测机制,这样预处理在C++中成为可能引发错误的隐患,于 ...
- Vue CLI4.0版本正式发布了!一起来看看有哪些新的变化吧
Vue CLI4.0版本正式发布 这个主要的版本更新主要关注底层工具的必要版本更新.更好的默认设置和其他长期维护所需的微调. 我们希望为大多数用户提供平稳的迁移体验. Vue CLI v4提供了对Ni ...
- ModelSerializer 使用知识点_serializers.SerializerMethodField()使用场景总结
serializers.SerializerMethodField和钩子方法结合,可以实现对ModelSerializer类的一些字段进行二次加工,返回,如下:1.对以ModelSerializer的 ...
- 3D Computer Grapihcs Using OpenGL - 18 相机移动
移动相机需要用到键盘按键,按键事件的引入需要包含头文件 #include <Qt3DInput\qkeyevent.h> 并实现QWidget中定义的虚函数keyPressEvent 我们 ...
- HDU3398—String-(组合数)
Problem Description Recently, lxhgww received a task : to generate strings contain '0's and '1's onl ...