2783: [JLOI2012]树
1 var 2 max,u,v,ans,s,i,j,k,n,m:longint; 3 p,sum:array[0..100000,0..20]of longint; 4 f,d,x:Array[0..100000]of longint; 5 procedure dou(a:longint); 6 var now,i,j,k:longint; 7 begin 8 now:=x[a]; 9 if a=1 then begin if now=s then inc(ans); exit;end; 10 k:=trunc(ln(d[a])/ln(2)); 11 for i:=k downto 0 do 12 begin 13 if (now+sum[a,i]<=s) then 14 begin 15 now:=now+sum[a,i]; 16 a:=p[a,i]; 17 end; 18 if now=s then break; 19 end; 20 if now=s then inc(ans); 21 end; 22 23 begin 24 readln(n,s); 25 for i:=1 to n do 26 read(x[i]); 27 for i:=1 to n-1 do 28 begin 29 readln(u,v); 30 f[v]:=u; 31 d[v]:=d[u]+1; 32 if d[v]>max then 33 max:=d[v]; 34 end; 35 k:=trunc(ln(max)/ln(2)); 36 for i:=1 to n do 37 sum[i,0]:=x[f[i]]; 38 for i:=1 to n do p[i,0]:=f[i]; 39 for j:=1 to k do 40 for i:=1 to n do 41 begin 42 p[i,j]:=p[p[i,j-1],j-1]; 43 sum[i,j]:=sum[i,j-1]+sum[p[i,j-1],j-1]; 44 end; 45 for i:=1 to n do 46 if (x[i]<=s) then 47 dou(i); 48 writeln(ans); 49 end.
-------------------------------------------------------------------------
花有重开日,人无再少年