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 ...
随机推荐
- kafka伪分布式安装(2.12版)
1.下载并解压. tar -xvf kafka_2.12-1.0.0.tgz 2.进入config目录修改server.properties文件. log.dirs=/tmp/kafka-logs ...
- 对GraphQL-BFF:微服务背景下的前后端数据交互方案的研究-------引用
随着多终端.多平台.多业务形态.多技术选型等各方面的发展,前后端的数据交互,日益复杂. 同一份数据,可能以多种不同的形态和结构,在多种场景下被消费. 在理想情况下,这些复杂性可以全部由后端承担.前端只 ...
- linux RPM(红帽软件包管理器)和Yum软件仓库中常见的命令
RPM(红帽软件包管理器)常用命令 安装软件:rpm -ivh filename.rpm 升级软件:rpm -Uvh filename.rpm 卸载软件:rpm -e filename.rpm 查询软 ...
- js怎么上传文件夹
1 背景 用户本地有一份txt或者csv文件,无论是从业务数据库导出.还是其他途径获取,当需要使用蚂蚁的大数据分析工具进行数据加工.挖掘和共创应用的时候,首先要将本地文件上传至ODPS,普通的小文件通 ...
- sh_07_函数的嵌套调用
sh_07_函数的嵌套调用 def test1(): print("*" * 50) def test2(): print("-" * 50) # 函数的嵌套调 ...
- 【转载】mysqld_safe Directory ‘/var/run/mysqld’ for UNIX socket file don’t exists.
This is about resetting the MySQL 5.7 root password in Ubuntu 16.04 LTS You probably tried something ...
- 【转】毛虫算法——尺取法
转自http://www.myexception.cn/program/1839999.html 妹子满分~~~~ 毛毛虫算法--尺取法 有这么一类问题,需要在给的一组数据中找到不大于某一个上限的&q ...
- [BZOJ3033]:太鼓达人(爆搜)
题目传送门 题目描述 七夕祭上,Vani牵着cl的手,在明亮的灯光和欢乐的气氛中愉快地穿行.这时,在前面忽然出现了一台太鼓达人机台,而在机台前坐着的是刚刚被精英队伍成员XLk.Poet_shy和lyd ...
- Hive、Spark优化案例
一.Join原则 将条目少的表/子查询放在Join的左边.原因:在Join的reduce阶段,位于Join左边的表的内容会被加载进内存,条目少的表放在左边,可以减少发生内存溢出的几率. 小表关联大表: ...
- 关于判断StringBuffer是否为空
对于String和StringBuffer来说,都是通过创建新的char value[]数组来达到字符串改变的操作的,只不过String是通过新创建String对象来达到目的, 而StringBuff ...