codeforces基础题——#358(div2)C
来源:互联网 发布:南京和知梦设计研究院 编辑:程序博客网 时间:2024/05/22 00:23
#358(div2)C
题目大意: 给你有一棵n(1 <= n <= 100000)个节点, 以节点1为根的树,每个节点有一个权值ai,如果节点v的子树里存在一点u, 使得v到u的路径上所有点的权值和 > au,那么称v为不高兴的点, 问至少删除树上的多少个点能使树上不存在不高兴的点。题解: 首先我们注意删只能删叶子节点, 所以如果节点x使它的祖先中的一个不高兴, 那么只能把以它为根的子树都删掉。我们考虑枚举每一个点是否会使它的祖先不高兴。 所以先处理出每个点的子树大小, 然后一遍dfs, dfs时记录当前点的祖先中到当前点的距离的最大值, 因为只需要知道当前点是否会造成不合法, 所以只要跟祖先中最容易产生不合法的比较, 即与最大值比较, 如果不合法, 就将当前点的子树删掉。
#include <cstdio>#include <iostream>#include <cstring>using namespace std;int vd[100010];struct Edge{ int next, to, v;}edge[100010];int head[100010], num = 0;void add_edge(int a, int b, int c){ edge[++ num].to = b, edge[num].v = c; edge[num].next = head[a], head[a] = num;}int sz[100010];void dfs1(int x, int f){ sz[x] = 1; for (int i = head[x]; i != -1; i = edge[i].next) if (edge[i].to != f) { dfs1(edge[i].to, x); sz[x] += sz[edge[i].to]; }}int ans = 0;void work(int x, int f, int k){ if (k > vd[x]){ ans += sz[x]; return; } for (int i = head[x]; i != -1; i = edge[i].next) if (edge[i].to != f) work(edge[i].to, x, max(k + edge[i].v, edge[i].v));}int main(){ int n; scanf("%d", &n); memset(head, -1, sizeof(head)); for (int i = 1; i <= n; i ++) scanf("%d", &vd[i]); int a, b; for (int i = 2; i <= n; i ++){ scanf("%d %d", &a, &b); add_edge(a, i, b); } dfs1(1, 0); work(1, 0, 0); printf("%d\n", ans); return 0;}
0 0
- codeforces基础题——#358(div2)C
- codeforces基础题——#362(div2)C
- codeforces基础题——#358(div2)D
- codeforces基础题——#356(div2) C
- codeforces基础题——#362(div2)D
- codeforces基础题——#361(div2)D
- codeforces基础题——#357(div2) D
- codeforces基础题——#356(div2) D
- codeforces基础题——#161(div2) E
- codeforces 260 div2 C题
- codeforcse基础题——#357(div2)C
- codeforces#259 div2 (a,b,c)三题
- codeforces #262 DIV2 C题Present(二分+贪心)
- codeforces #429(div2)C—Leha and Function(水题)
- 【套题】Codeforces#306(div2)[A-C]
- 思路题 codeforces div2 C Replacement
- Codeforces Div2 2016.08.29 C题
- Codeforces Round #402 (Div2)C题
- 关于python实现把文件提取出来写到excel表里
- Fio压测工具和io队列深度理解和误区
- 浙大PAT甲级 1113
- 第32篇 网站试题生成word下载时bug解决
- 项目使用第三方插件发送短信-------建周短信SDK(Software Development Kit)
- codeforces基础题——#358(div2)C
- CCF 201512-2 消除类游戏(水)
- ionic实现Android下拉更新时的Toast功能
- python 写入excel两种方法
- 【汇编学习】汇编语言读书笔记(第一章)
- class_create(),device_create自动创建设备文件结点 .
- fio性能测试工具新添图形前端gfio
- 存储过程实现模糊查询问题
- androidM 显示框架