-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path2080.cpp
47 lines (42 loc) · 942 Bytes
/
2080.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int n, k;
long long ans;
deque<int> d[N];
vector<int> g[N];
int get(deque<int> &a, int x) {
if (x < 0 || x >= (int)a.size()) return 0;
if (x + 1 < (int)a.size()) return a[x] - a[x + 1];
else return a[x];
}
void combine(deque<int> &a, deque<int> &b) {
if (a.size() < b.size()) a.swap(b);
for (int i = 0; i < (int)b.size(); ++i) {
ans += 1LL * get(b, i) * get(a, k - i);
}
for (int i = 0; i < (int)b.size(); ++i) {
a[i] += b[i];
}
}
void dfs(int u, int p) {
d[u] = {1};
for (int v: g[u]) {
if (v == p) continue;
dfs(v, u);
d[v].emplace_front(d[v].front());
combine(d[u], d[v]);
}
}
int main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n >> k;
for (int i = 1, u, v; i < n; ++i) {
cin >> u >> v;
g[u].emplace_back(v);
g[v].emplace_back(u);
}
dfs(1, -1);
cout << ans;
return 0;
}