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 ...
随机推荐
- 对vue源码之缓存的研究--------------引用
探索vue源码之缓存篇 一.从链表说起 首先我们来看一下链表的定义: 链表(Linked list)是一种常见的基础数据结构,是一种线性表,但是并不会按线性的顺序存储数据,而是在每一个节点里存到下一个 ...
- TypeScript作为前端开发你必须学习的技能(三)
TypeScript 运算符 TypeScript 主要包含以下几种运算: 算术运算符 逻辑运算符 关系运算符 按位运算符 赋值运算符 三元/条件运算符 字符串运算符 类型运算符 实例看图: 关系运算 ...
- LeetCode - 删除链表的倒数第N个节点
给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点. 示例: 给定一个链表: 1->2->3->4->5, 和 n = 2. 当删除了倒数第二个节点后,链表变为 ...
- Java对数组和列表的排序1.8新特性
Java对数组列表的排序 数组 Integer[] a = new Integer[] { 1, 2, 3, 4, 5, 6, 9, 8, 7, 4, 5, 5, 6, 6 }; Arrays.sor ...
- sqli-labs(10)
基于双引号的时间盲注 先来随便测试一下 发现 错不错都是返回的真确的结果 那么应该是被从定向了 我们可以输入 and sleep(5) %23 测试 加’发现立刻返回 加双引号发现报错了 过了5秒才返 ...
- NOIP2018初赛划水记
尽管初赛好像没什么好写的,但还是以此作为我的第一篇游记吧. 前夜 本来以为初赛水(去年分数线36来着),几周前做了一套普及组的卷子90多分感觉良好就没管了(不明白那些专门花时间看初赛书的人),结果Mr ...
- #20175201 实现mypwd
实验要求 学习pwd命令 研究pwd实现需要的系统调用(man -k; grep),写出伪代码 实现mypwd 测试mypwd 了解pwd命令 pwd是"Print Working Di ...
- YV12和NV12格式
害怕搞忘 直接保存图片
- 关于db4o的透明激活与激活声明
关于db4o的透明激活与激活声明 有关于透明激活,其介绍可以参看这里:http://www.cnblogs.com/redmoon/archive/2008/02/23/1078619.html 文中 ...
- Linux_LVM、RAID_RHEL7
目录 目录 LVM逻辑卷管理 把物理分区初始化为物理卷 创建卷组 建立逻辑卷 格式化 挂载 vg拓展操作 lv扩展操作 RAID RAID 类型 RAID0条带化 RAID1镜像 RAID5条带冗余 ...