Codeforces 161 D Distance in Tree 树形DP
2016-07-15 09:50
441 查看
题目大意:给出一棵n个节点的树,统计树中长度为k的路径的条数(1<=n<=50000
,1<=k<=500);
题目分析:设dp[i][j]表示以i作为长度为j的路径其中一个点的方案数。转移:dp[u][j]+=dp[v][j-1]; 方案计数:ans+=dp[v][j]*dp[u][k-j-1];
,1<=k<=500);
题目分析:设dp[i][j]表示以i作为长度为j的路径其中一个点的方案数。转移:dp[u][j]+=dp[v][j-1]; 方案计数:ans+=dp[v][j]*dp[u][k-j-1];
#include<cstdio> #include<cstdlib> #include<cstring> #include<iostream> #include<algorithm> #include<functional> #include<cmath> #include<cctype> #include<cassert> #include<climits> using namespace std; #define For(i,n) for(int i=1;i<=n;i++) #define Rep(i,n) for(int i=0;i<n;i++) #define Fork(i,k,n) for(int i=k;i<=n;i++) #define ForD(i,n) for(int i=n;i;i--) #define Forp(x) for(int p=pre[x];p;p=next[p]) #define RepD(i,n) for(int i=n;i>=0;i--) #define MEM(a) memset(a,0,sizeof(a)) #define MEMI(a) memset(a,127,sizeof(a)) #define MEMi(a) memset(a,128,sizeof(a)) #define INF (2139062143) #define phiF (1000000006) #define MAXN (1000000+10) typedef long long LL; struct info{ int to,next; }e[100005]; int tot,first[100005],dp[50005][505],k,ans,n,x,y; void add(int u,int v){ tot++; e[tot].to=v; e[tot].next=first[u]; first[u]=tot; } void dfs(int u,int fa){ dp[u][0]=1; for (int p=first[u];p;p=e[p].next){ int v=e[p].to; if (v==fa) continue; dfs(v,u); Rep (j,k) ans+=dp[v][j]*dp[u][k-j-1]; For (j,k) dp[u][j]+=dp[v][j-1]; } } int main(){ scanf("%d%d",&n,&k); For (i,n-1){ scanf("%d%d",&x,&y); add(x,y); add(y,x); } dfs(1,0); printf("%d",ans); }
相关文章推荐
- Codeforces Round #197 (Div. 2)
- Codeforces Round #198 (Div. 1)
- Codeforces 405E Codeforces Round #238 (Div. 2)E
- Codeforces 407C Codeforces Round #239 (Div. 1)C
- CodeForces 449A - Jzzhu and Chocolate
- CodeForces 449 B. Jzzhu and Cities
- codeforces 618C. Constellation
- Codeforces Round #349 (Div. 2) - C
- Codeforces Round #361 (Div. 2)
- 7.13Codeforces Round #360 (Div. 2)
- Codeforces Round #265 (Div. 2)
- Codeforces #310 div2 C. Case of Matryoshkas
- 状态压缩DP codeforces 244 Problem C. The Brand New Function 和 codeforces 165 E. Compatible Numbers
- codeforces 16 Problem E fish
- Codeforces Round332 部分题解
- CodeForces 603A_Alternative Thinking (DP)
- CodeForces 602B_Approximating a Constant Range_DP
- Codeforces round #247 for Div. 2
- Codeforces Round #246 (Div. 2)
- Codeforces #264(div 2)D.Gargari and Permutations