Distance in Tree
来源:互联网 发布:limit mysql 优化 编辑:程序博客网 时间:2024/06/09 20:21
http://codeforces.com/problemset/problem/161/D
//dp[i][j]表示以i节点为端点 且 长度为j的种数 #include<stdio.h>#include<string.h>#include<vector>using namespace std;int n,k;const int maxn=50006;typedef long long lld;int dp[maxn][505];lld ans;vector<int>edge[maxn];void DFS(int u,int f){int i,j,v;int sz=edge[u].size();dp[u][0]++;for(i=0;i<sz;i++){v=edge[u][i];if(f==v)continue;DFS(v,u);for(j=0;j<k;j++)ans+=(lld)dp[v][j]*dp[u][k-j-1];//计算长度为k的种数for(j=0;j<=k;j++)dp[u][j+1]+=dp[v][j];//转移}}int main(){int i,j;int u,v;while(scanf("%d%d",&n,&k)!=EOF){for(i=0;i<=n;i++)edge[i].clear();memset(dp,0,sizeof(dp));for(i=1;i<n;i++){scanf("%d%d",&u,&v);edge[u].push_back(v);edge[v].push_back(u);}ans=0;DFS(1,1);printf("%lld\n",ans);}return 0;}
- Distance in Tree
- codeforces D. Distance in Tree
- Ural1471-Distance in the Tree
- CF-161D Distance in Tree
- Codeforces 161D Distance in Tree
- Codeforces 161D Distance in Tree
- codeforces 161D Distance in Tree(树形dp)
- CodeForces 160D - Distance in Tree 树形DP
- CF 161D Distance in Tree【树DP】
- codeforces 161D. Distance in Tree(树dp)
- codeforces 161D - Distance in Tree 树状DP
- codeforces 161D - Distance in Tree(树形dp)
- CF 161D Distance in Tree 树形DP
- CodeForces 161D Distance in Tree(树形DP)
- codeforces 161D Distance in Tree (树形DP 经典题)
- codeforces 161D D. Distance in Tree(树形dp)
- CodeForces 161D Distance in Tree 树形DP
- Codeforces 161 D Distance in Tree 树形DP
- 更改开机默认操作系统及等待时间修改
- 行列互转[整理人:中国风(Roy)]
- 如何让secureCRT显示Linux的颜色
- UVa 146 - ID Codes
- ruby--Hash方法汇总
- Distance in Tree
- hdoj1331
- 诺基亚裁了哪些中国员工?
- redis实现master-slave分布式
- android中wifi,蓝牙等常见设置的控制
- UVa 442 - Matrix Chain Multiplication
- 常用的 HTML5 移动应用开发框架比较
- Opera用户达到2亿 非洲市场增长强劲
- UVa 10763 - Foreign Exchange