A tree, the side length is 1, ask this tree how many points to the distance is just K
Make tree (i) a subtree that is rooted in I
DP[I][J][1]: In tree (i), through node I, length J, where one endpoint is the number of paths of I
Dp[i][j][0]: In tree (i), through node I, Length J, the end of the path is not the number of I
Then target: ∑ (dp[i][k][0]+dp[i][k][1])
Initialize: dp[i][0][1]=1, the remaining 0
Siz[i]:tree (i), I and the distance from the farthest point of I
Recursion:
DP[I][J][0]+=DP[I][J-L][1]*DP[SONI][L-1][1]
DP[I][J][1]=∑DP[SONI][J-1][1]
Note: When updating each dp[i], update dp[i][j][0], and then update dp[i][j][1]
1#include <cstdio>2#include <cstring>3#include <iostream>4 5 using namespacestd;6 7 Const intmaxn=50000+5;8 Const intmaxk=505;9 #defineLL Long LongTen OneInlineintMaxintAintb) A { - returnA>b?a:b; - } the -InlineintMinintAintb) - { - returnA<b?a:b; + } - +LL dp[maxn][maxk][2]; A intSIZ[MAXN]; at structEdge - { - intTo,next; - }; -Edge edge[maxn<<1]; - intHEAD[MAXN]; in inttot; - intK; to + voidInit () - { thememset (head,-1,sizeofhead); *tot=0; $Memset (DP,0,sizeofDP);Panax Notoginseng } - the voidAddedge (intUintv) + { Aedge[tot].to=v; theedge[tot].next=Head[u]; +head[u]=tot++; - } $ $ voidSolveint,int ); - voidDfsint,int ); - the intMain () - {Wuyi init (); the intN; -scanf"%d%d",&n,&k); Wu for(intI=1; i<n;i++) - { About intu,v; $scanf"%d%d",&u,&v); - Addedge (u,v); - Addedge (v,u); - } A solve (n,k); + return 0; the } - $ voidSolveintNintk) the { theDfs1,0); theLL ans=0; the for(intI=1; i<=n;i++) - { inans+= (dp[i][k][0]+dp[i][k][1]); the } thecout<<ans<<Endl; About return ; the } the the voidDfsintUintpre) + { -dp[u][0][1]=1; thesiz[u]=0;Bayi for(intI=head[u];~i;i=edge[i].next) the { the intv=edge[i].to; - if(v==pre) - Continue; the DFS (v,u); the for(intL=1; l<=siz[v]+1; l++) the { the for(intj=l+1; j<=siz[u]+l;j++) - { the if(j>k) the Continue; thedp[u][j][0]+=dp[u][j-l][1]*dp[v][l-1][1];94 } the } the for(intj=1; j<=siz[v]+1; j + +) thedp[u][j][1]+=dp[v][j-1][1];98Siz[u]=max (siz[u],siz[v]+1); Aboutsiz[u]=min (siz[u],k); - }101}
View Code
CF 161D Distance in tree DP