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?
InputIn 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.
OutputPrint 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.
Example input Copy9output Copy
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
5Note
The following image represents possible process of removing leaves from the tree:
题意:
给一棵根节点为1的树,结点和边都有权值,对于u如果他的一个祖先结点是v,dist(u,v)>a[u],那么称v结点为sad。要让树中没有sad结点,需要删除多少点。
分析:
直接DFS,记录路径的和,如果路径和大于a[u],那么就删除这整个子树。如果路径和小于0了,就舍弃之前的路径,重新计算。如果我们从根节点1累计路径值到点u的和为-1000,此时有一条路从u到v,其权为2,而此时a【v】=1;那么如果我们继续维护这个路径和的话,此时的路径和为-9999。显然是小于1的,但是我们这个节点v又是必须删除的点
/// author:Kissheart /// #include<stdio.h> #include<algorithm> #include<iostream> #include<string.h> #include<vector> #include<stdlib.h> #include<math.h> #include<queue> #include<deque> #include<ctype.h> #include<map> #include<set> #include<stack> #include<string> #define INF 0x3f3f3f3f #define FAST_IO ios::sync_with_stdio(false) const double PI = acos(-1.0); const double eps = 1e-6; const int MAX=1e6+10; const int mod=1e9+7; typedef long long ll; using namespace std; #define gcd(a,b) __gcd(a,b) inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;} inline ll qpow(ll a,ll b){ll r=1,t=a; while(b){if(b&1)r=(r*t)%mod;b>>=1;t=(t*t)%mod;}return r;} inline ll inv1(ll b){return qpow(b,mod-2);} inline ll exgcd(ll a,ll b,ll &x,ll &y){if(!b){x=1;y=0;return a;}ll r=exgcd(b,a%b,y,x);y-=(a/b)*x;return r;} inline ll read(){ll x=0,f=1;char c=getchar();for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;for(;isdigit(c);c=getchar()) x=x*10+c-'0';return x*f;} //freopen( "in.txt" , "r" , stdin ); //freopen( "data.txt" , "w" , stdout ); ll n,ans; ll a[MAX],si[MAX]; vector<pair<ll,ll> >v[MAX]; ll DFS(ll x,ll fa,ll dist) { ll ans=0; for(ll i=0;i<v[x].size();i++) { ll to=v[x][i].first; if(to==fa) continue; ans+=DFS(to,x,max(0LL,v[x][i].second+dist)); si[x]+=si[to]; } if(dist>a[x]) ans=si[x]; return ans; } int main() { for(ll i=0;i<MAX;i++) si[i]=1; scanf("%lld",&n); for(ll i=1;i<=n;i++) scanf("%lld",&a[i]); for(ll i=2;i<=n;i++) { ll x,y; scanf("%lld%lld",&x,&y); v[i].push_back(make_pair(x,y)); v[x].push_back(make_pair(i,y)); } printf("%lld\n",DFS(1LL,1LL,0LL)); return 0; }View Code