wenbao与树上dp

 

---------------------------------------------------------------

 

http://codeforces.com/problemset/problem/161/D

求树上路径长度为k的个数

 

 

 1 #include "iostream"
 2 #include <vector>
 3 using namespace std;
 4 
 5 const int maxn = 50009;
 6 vector<int> v[maxn];
 7 int n, k, cnt = 0, d[maxn][505];
 8 
 9 //d[i][j] 经过点i长度为j的路径个数
10 
11 void dd(int x, int pre){
12     d[x][0] = 1;
13     for(int i = 0; i < v[x].size(); ++i){
14         int xx = v[x][i];
15         if(xx != pre){
16             dd(xx, x);
17             for(int j = 0; j < k; ++j) cnt += d[x][j]*d[xx][k-j-1];
18             for(int j = 1; j <= k; ++j) d[x][j] += d[xx][j-1];
19         }
20     }
21 }
22 
23 int main(){
24 #ifdef wenbao
25     freopen("in", "r", stdin);
26 #endif
27     int x, y;
28     scanf("%d%d", &n, &k);
29     for(int i = 1; i < n; ++i){
30         scanf("%d%d", &x, &y);
31         v[x].push_back(y), v[y].push_back(x);
32     }
33     dd(1, 0);
34     printf("%d\n", cnt);
35     return 0;
36 }

 

 

----------------------------------------------------------------

----------------------------

 

只有不断学习才能进步!

 

posted @ 2018-04-14 13:48  wenbao  阅读(94)  评论(0编辑  收藏  举报