题意翻译

题目描述

给你一棵树,边与节点都有权值,根节点为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的节点的父节点以及该边的边权

输出格式:

一个整数,最少需要删除的点的个数

输入输出样例

输入样例#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
输出样例#1:

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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. CodeForces 682C Alyona and the Tree (树+dfs)

    Alyona and the Tree 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/C Description Alyona ...

  6. Alyona and a tree

    Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. 目标检测 | OHEM

    参考:https://blog.csdn.net/app_12062011/article/details/77945600 参考:http://www.cnblogs.com/sddai/p/102 ...

  2. JS几种数组遍历方式总结

    JS数组遍历的几种方式 JS数组遍历,基本就是for,forin,foreach,forof,map等等一些方法,以下介绍几种本文分析用到的数组遍历方式以及进行性能分析对比 第一种:普通for循环 代 ...

  3. cvtColor

    E:/OpenCV/opencv/sources/modules/imgproc/src/color.cpp CV_RGB2GRAY:RGB--->GRAY.

  4. Navicat使用与python操作数据库

    一.Navicat使用 1.下载地址: <https://pan.baidu.com/s/1bpo5mqj> 2.测试+链接数据库,新建库 3.新建表,新增字段+类型+约束 4.设计表:外 ...

  5. [LeetCode]-DataBase-Duplicate Emails

    Write a SQL query to find all duplicate emails in a table named Person. +----+---------+ | Id | Emai ...

  6. 冲刺周三The Third Day

    一.ThirdDay照片 二.项目分工 三.今日份燃尽图 四.项目进展 码云团队协同环境构建完毕 利用Leangoo制作任务分工及生成燃尽图 完成AES加解密部分代码 用代码实现对文件的新建.移动.复 ...

  7. java学习之- 创建线程run和start特点

    标签(空格分隔): run,start 为什么做run方法的覆盖? 1.Thread类用于描述线程,该类就定义一个功能用于存储线程要运行的代码,该存储功能就是run方法: 也就是说Thread种的ru ...

  8. maven使用常见问题

    1.我写的是src/main/java/config/mybatis-cofig.xml 但总是报错 Could not find resource src/main/java/config/myba ...

  9. legend3---PHP使用阿里云短信服务

    legend3---PHP使用阿里云短信服务 一.总结 一句话总结: 使用步骤照官方文档,代码拷贝即可 1.php使用阿里云短信服务的步骤? 入驻阿里云->开通短信服务->获取Access ...

  10. leetcode-mid-others-150. Evaluate Reverse Polish Notation

    mycode   42.30%. 注意:如果不考虑符号,-1//3=-1而不是等于0,因为是向下取整 class Solution(object): def evalRPN(self, tokens) ...