题目链接:http://codeforces.com/problemset/problem/161/D
题意:
给你一棵树,问你有多少对点的距离为k。
思路:
dp[i][j]表示离i节点距离为j的点个数,2次dfs,一次从底向上,另一次从顶向下。
//#pragma comment(linker, "/STACK:102400000, 102400000")
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
typedef long long LL;
typedef pair <int, int> P;
const int N = 1e5 + ;
int dp[][], n, m;
vector <int> G[]; void dfs1(int u, int p) {
dp[u][] = ;
for(int i = ; i < G[u].size(); ++i) {
int v = G[u][i];
if(v == p)
continue;
dfs1(v, u);
for(int i = ; i <= m; ++i) {
dp[u][i] += dp[v][i - ];
}
}
} void dfs2(int u, int p) {
for(int i = ; i < G[u].size(); ++i) {
int v = G[u][i];
if(v == p)
continue;
for(int i = m; i >= ; --i) {
dp[v][i] += dp[u][i - ] - dp[v][i - ];
}
dp[v][]++;
dfs2(v, u);
}
} int main()
{
int u, v;
while(~scanf("%d %d", &n, &m)) {
for(int i = ; i < n; ++i) {
scanf("%d %d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
}
if(m == ) {
printf("%d\n", n - );
} else {
dfs1(, -);
dfs2(, -);
LL ans = ;
for(int i = ; i <= n; ++i) {
ans += (LL)dp[i][m];
}
printf("%lld\n", ans / );
}
}
return ;
}