国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

C. Alyona and the Tree

2019-11-08 03:08:06
字體:
來源:轉載
供稿:網友
C. Alyona and the Treetime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output

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 udist(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?

Input

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.

Output

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.

Exampleinput
988 22 83 14 95 91 98 53 113 247 -81 671 649 655 126 -803 8output
5Note

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,相當于不存在,之后函數返回的值是存在的點,與點總數比較即可


上一篇:1001. A+B Format

下一篇:ROS 環境配置問題

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 玉山县| 宝应县| 织金县| 颍上县| 揭阳市| 广元市| 宁安市| 邢台市| 阿坝县| 莱州市| 乐东| 徐水县| 元阳县| 昂仁县| 杨浦区| 曲靖市| 嘉义县| 武川县| 白城市| 全南县| 南漳县| 丰顺县| 岳阳市| 平度市| 东辽县| 醴陵市| 洪湖市| 太仓市| 筠连县| 天台县| 弥渡县| 红桥区| 邵阳市| 湟中县| 建瓯市| 驻马店市| 南华县| 安化县| 新泰市| 上饶市| 微博|