题意翻译

题目描述

给你一棵树,边与节点都有权值,根节点为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. Kafka(华为FusionInsight )操作命令

    华为大数据kafka操作web界面创建角色.用户.用户管理角色进入服务器环境,进入客户端目录/opt/hadoopclient,导入环境变量source bigdata_env.切换用户kinit k ...

  2. MSVCRTD.lib(crtexe.obj) : error LNK2019: unresolved external symbol _main re

    出现这个问题是因为工程是应用win32,必须要有main函数,修改方式为: configuration properties中General->configuration Type->将a ...

  3. html a标签 语法

    html a标签 语法 作用:<a> 标签定义超链接,用于从一张页面链接到另一张页面. 直线电机滑台 说明:<a> 元素最重要的属性是 href 属性,它指示链接的目标.在所有 ...

  4. Spring Boot教程(八)创建含有多module的springboot工程

    创建根工程 创建一个maven 工程,其pom文件为: <?xml version="1.0" encoding="UTF-8"?> <pro ...

  5. beforeRouterEnter与replace的使用

    这次使用beforeRouterEnter来判断是一定条件下才执行相应的页面跳转. beforeRouterEnter:组件内路由,跟data,methods同级 beforeRouteEnter ( ...

  6. Java数据结构之排序---选择排序

    简单选择排序的介绍: 从给定的序列中,按照指定的规则选出某一个元素,再根据规定交换位置后达到有序的目的. 简单选择排序的基本思想: 假定我们的数组为int [] arr = new int[n],第一 ...

  7. RestFul的无状态规则详解

    无状态原则 Statelessness:无状态原则是RESTful架构设计中一个非常重要的原则,无状态是相对于有状态而言的.在理解什么是无状态的交互请求之前,首先我们需要了解什么是有状态,并对两者进行 ...

  8. 多层全连接神经网络实现minist手写数字分类

    import torch import numpy as np import torch.nn as nn from torch.autograd import Variable import tor ...

  9. 在HTML标签元素中,绑定JS函数

    <a onclick="ShowMsg(this)" id="myA" href="#">按钮</a> //JS方法 ...

  10. lr_save_string和sprintf、lr_eval_string的使用

    一.lr_save_string函数 1.该函数主要是将程序中的常量或变量保存为参数: //将常量保存为参数 lr_save_string("777","page&quo ...