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.
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
The following image represents possible process of removing leaves from the tree:
![](https://i-blog.csdnimg.cn/blog_migrate/02fa27e54826f0191db512f3bf69f177.png)
题目大意:
给你一个包含N个点的一个树,其中保证点1为根节点,如果此时有:点u是点v的子树中的节点,并且dis(u,v)>a[u];那么对应表示这个点v是伤心的,问最少去掉多少个点能够使得整个树中没有伤心的节点(每次删除节点之后其子树中的节点也相当于被删除了)。
思路:
1、首先建立无向图,接下来从点1开始暴搜,如果我们计算两两节点间距离dis(u,v)显然需要n^2的时间复杂度,会TLE .那么接下来考虑进行优化。
2、我们从根节点开始搜索,对应维护一个路径长度值,如果我们这个路径长度值一直维护的是整条路上的路径和,那么很明显,如果这个长度值大于了a【u】;那么其当前节点u的子节点也就不用搜索了,此时直接删除整颗以u为根的子树中的所有节点。
那么接下来考虑这个问题,如果我们从根节点1累计路径值到点u的和为-1000,此时有一条路从u到v,其权为2,而此时a【v】=1;那么如果我们继续维护这个路径和的话,此时的路径和为-9999。显然是小于1的,但是我们这个节点v又是必须删除的点,那么我们需要怎样做呢?我们绕来绕去又绕出来N^2的做法了?
我们只需要在累加路径和的时候,取0和路径和的最大值即可。就是说如果累加到当前节点的时候路径和为负数,那么对应前边的路径和就舍弃了,那么应该删除的节点,也就对应删除掉了。
3、整个过程描述可能过于简短而且可能有点乱,具体参考一下代码吧O.O....
Ac代码:
#include<stdio.h>
#include<string.h>
#include<vector>
#include<iostream>
using namespace std;
#define ll __int64
struct node
{
int from;
int to;
ll w;
int next;
}e[150000*2];
int n,cont;
ll a[150000];
int head[150000];
int vis[150000];
void add(int from,int y,ll w)
{
e[cont].to=y;
e[cont].w=w;
e[cont].next=head[from];
head[from]=cont++;
}
void Dfs(int u,ll sum)
{
vis[u]=1;
for(int i=head[u];i!=-1;i=e[i].next)
{
int v=e[i].to;
ll w=e[i].w;
if(vis[v]==0&&w+sum<=a[v])
{
Dfs(v,max(0LL,sum+w));//维护最大值...
}
}
}
int main()
{
while(~scanf("%d",&n))
{
cont=0;
memset(vis,0,sizeof(vis));
memset(head,-1,sizeof(head));
for(int i=1;i<=n;i++)
{
scanf("%I64d",&a[i]);
}
for(int i=2;i<=n;i++)
{
int x,y;
scanf("%d%d",&x,&y);
add(i,x,y);
add(x,i,y);
}
Dfs(1,0);
int output=0;
for(int i=1;i<=n;i++)
{
if(vis[i]==0)output++;
}
printf("%d\n",output);
}
}