题目链接:点击打开链接
题解:不断维护:ans += dfs(u,v,max( val[v], cur-w) )
AC代码:
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=1000000+10;
struct Edge{
int v,w,next;
Edge(){}
Edge(int _v,int _w,int _next):v(_v),w(_w),next(_next){}
}e[N*2];
int head[N];
int val[N];
int n,E;
int dfs(int o,int u,ll cur)
{
int ans=1;
int v,w;
for(int i=head[u];~i;i=e[i].next)
{
v=e[i].v;
w=e[i].w;
if(v==o)continue;
// printf ("val[%d]=%d, cur-w=%lld\n", v,val[v], cur-w);
if(val[v]<cur-w) continue;
ans+=dfs(u,v,max( (ll)val[v], cur-w) );
}
//printf ("u=%d, ans=%d\n", u, ans);
return ans;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
memset(head,-1,sizeof(head));
E=0;
int u,v,w;
for(int i=1;i<n;i++)
{
scanf("%d%d%d",&u,&v,&w);//u->v
e[E]=Edge(v,w,head[u]);
head[u]=E++;
e[E]=Edge(u,w,head[v]);
head[v]=E++;
}
for(int i=1;i<=n;i++)scanf("%d",&val[i]); //点权
printf("%d\n",dfs(1,1,val[1]));
}
return 0;
}