题意翻译

题目描述

给你一棵树,边与节点都有权值,根节点为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. Text-CNN 文本分类

    1.简介 TextCNN 是利用卷积神经网络对文本进行分类的算法,由 Yoon Kim 在 “Convolutional Neural Networks for Sentence Classifica ...

  2. API网关原理

    1.API网关介绍 API网关是一个服务器,是系统的唯一入口.从面向对象设计的角度看,它与外观模式类似.API网关封装了系统内部架构,为每个客户端提供一个定制的API.它可能还具有其它职责,如身份验证 ...

  3. luogu 4725 【模板】多项式对数函数(多项式 ln)

    $G(x)=ln(A(x))$ $G'(x)=ln'(A(x))A'(x)=\frac{A'(x)}{A(x)}$     由于求导和积分是互逆的,所以对 $G$ 求积分,即 $G(x)=\int\f ...

  4. UI编辑

    UI编辑 基本部件介绍 (1)Layout(布局) (2)Space(空间) (3)Button (4)ItemView (5)ItemWidget Widget继承自View,即ListWidget ...

  5. sh_09_print函数的结尾

    sh_09_print函数的结尾 # 在默认情况下,print 函数输出内容之后,会自动在内容末尾增加换行 print("*", end="---") prin ...

  6. linux文件夹目录含义及用途

    /boot,存放linux启动文件和内核: /initrd,boot loader initialized RAM disk,就是由boot loader初始化的内存盘.在linux内核启动前,boo ...

  7. Burp suite抓取HTTPS请求

    一.下载链接:Burp suite 密码:orpr 二.抓取浏览器HTTPS请求 1.打开CMD,进入到Burp suite下载路径,执行:java -jar BurpLoader.jar 2.点击 ...

  8. java 用redisTemplate 的 Operations存取list集合

    一 .存取为list类型 @RestController @RequestMapping("/test") @Slf4j public class TestController { ...

  9. [LeetCode]-011-Roman_to_Integer

    Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...

  10. springboot 基于@Scheduled注解 实现定时任务

    前言 使用SpringBoot创建定时任务非常简单,目前主要有以下三种创建方式: 一.基于注解(@Scheduled) 二.基于接口(SchedulingConfigurer) 前者相信大家都很熟悉, ...