C. Alyona and the Tree
来源:互联网 发布:26650锂电池容量的算法 编辑:程序博客网 时间:2024/05/27 00:40
Alyona decided to go on a diet and went to the forest to get some apples. There she unexpectedly found a magic rooted tree with root in the vertex 1, every vertex and every edge of which has a number written on.
The girl noticed that some of the tree's vertices are sad, so she decided to play with them. Let's call vertex v sad if there is a vertex u in subtree of vertex v such that dist(v, u) > au, where au is the number written on vertex u, dist(v, u) is the sum of the numbers written on the edges on the path from v to u.
Leaves of a tree are vertices connected to a single vertex by a single edge, but the root of a tree is a leaf if and only if the tree consists of a single vertex — root.
Thus Alyona decided to remove some of tree leaves until there will be no any sad vertex left in the tree. What is the minimum number of leaves Alyona needs to remove?
In the first line of the input integer n (1 ≤ n ≤ 105) is given — the number of vertices in the tree.
In the second line the sequence of n integers a1, a2, ..., an (1 ≤ ai ≤ 109) is given, where ai is the number written on vertex i.
The next n - 1 lines describe tree edges: ith of them consists of two integers pi and ci (1 ≤ pi ≤ n, - 109 ≤ ci ≤ 109), meaning that there is an edge connecting vertices i + 1 and pi with number ci written on it.
Print the only integer — the minimum number of leaves Alyona needs to remove such that there will be no any sad vertex left in the tree.
988 22 83 14 95 91 98 53 113 247 -81 671 649 655 126 -803 8
5
The following image represents possible process of removing leaves from the tree:
题目大意:给你一棵n个顶点的数,然后每个点会有一个值,点于点之间有权重,把叶到任意祖宗的路权重和大于点值的点删去,需要最少几步
数据范围: n (1 ≤ n ≤ 105)(顶点个数),a1, a2, ..., an (1 ≤ ai ≤ 109)(各点值),pi,ci (1 ≤ pi ≤ n, - 109 ≤ ci ≤ 109),pi(点编号),ci(点与点之间的权值)
解题思路:一开始想用邻接表,但是不能开那么大的,所以就放弃了,之后想用三个数组去存,但是个人感觉太难以理解,不太好弄,意外发现vector,他是一个灵活的数组,然后用多少取多少,每次压入栈的是对应的连接点和两点之间的权值,然后从根开始遍历,不断沿着路线下去,当查到不符合条件的时候返回0,相当于不存在,之后函数返回的值是存在的点,与点总数比较即可
- C. Alyona and the Tree
- Codeforces 682C. Alyona and the Tree
- codeforces 682C. Alyona and the Tree
- codeforces 682C Alyona and the Tree
- Codeforces 682C C. Alyona and the Tree (DFS)
- Codeforces Round #358 (Div. 2) C. Alyona and the Tree
- Codeforces Round #358 (Div. 2)C. Alyona and the Tree
- CodeForces 682C Alyona and the Tree 搜索
- Codeforces Round #358 (Div. 2) C. Alyona and the Tree
- CodeForces 682C - Alyona and the Tree(dfs)
- codeforces 682C Alyona and the Tree(DFS)
- Codeforces 682C Alyona and the Tree DP
- Codeforces Round #358 (Div. 2)-C. Alyona and the Tree(树形dp)
- Codeforces Round #358 (Div. 2) C. Alyona and the Tree (DFS)
- Codeforces Round #358 (Div. 2) C Alyona and the Tree(树形DP)
- Codeforces Round #358 (Div. 2) -- C. Alyona and the Tree (树上的DFS)
- Codeforces Round #358 (Div. 2) C. Alyona and the Tree (二叉树+DFS)
- Codeforces Round #358 (Div. 2) C. Alyona and the Tree(DFS)
- ROS 环境配置问题
- Activity的四种启动模式
- (2)开发环境搭建-DAO层
- MOS管栅极串联电阻的作用
- php判断字符串是否含有中文
- C. Alyona and the Tree
- js中怎么获取系统当前时间 格式 yyyy-MM-dd HH:MM:SS
- 1001. A+B Format
- POJ - 2349----Arctic Network(prim)
- linux读书摘要--正规表示法与文件格式化处理
- ListView里TextView使用ClickableSpan,ListView的Item无法点击的解决办法
- 图结构练习——最短路径(Dijkstra算法)
- 图解说明ES5和ES6中继承的区别
- 【scikit-learn】02:使用sklearn库进行统计学习