由于路径的深度是升序的
所以我们可以考虑用前缀和的思想,用sum维护点到根路径上节点和
对于每个点x存在路径和为s即这个点到根的路径上存在y,使sum[x]-sum[y]=s
这显然是可以二分的
type node=record
po,next:longint;
end; var w:array[..] of node;
q,s,a,p:array[..] of longint;
len,t,n,m,x,y,ans,i:longint; procedure add(x,y:longint);
begin
inc(len);
w[len].po:=y;
w[len].next:=p[x];
p[x]:=len;
end; function find(l,r,x:longint):boolean;
var m:longint;
begin
while l<=r do
begin
m:=(l+r) shr ;
if s[q[m]]=x then exit(true);
if s[q[m]]>x then r:=m- else l:=m+;
end;
exit(false);
end; procedure dfs(x:longint);
var i,y:longint;
begin
inc(t);
q[t]:=x;
i:=p[x];
while i<> do
begin
y:=w[i].po;
s[y]:=s[x]+a[y];
if find(,t,s[y]-m) then inc(ans);
dfs(y);
i:=w[i].next;
end;
dec(t);
end; begin
readln(n,m);
for i:= to n do
read(a[i]);
for i:= to n- do
begin
readln(x,y);
add(x,y);
end;
s[]:=a[];
q[]:=;
s[]:=;
dfs();
writeln(ans);
end.