Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- int n, k;
- vector<int> g[100100];
- int sz[100100];
- int lev[100100];
- int mx[100100];
- int u[100100];
- vector<int> G;
- void dfs(int v, int pa){
- sz[v] = 1;
- mx[v] = 0;
- lev[v] = lev[pa] + 1;
- for(int to: g[v]){
- if(u[to]) continue;
- if(to == pa) continue;
- dfs(to, v);
- mx[v] = max(mx[v], sz[to]);
- sz[v] += sz[to];
- }
- G.push_back(v);
- }
- int cnt[100100];
- ll ans = 0;
- void solve(int v){
- G.clear();
- dfs(v, 0);
- int C = -1;
- for(int to: G){
- if(C == -1) C = to;
- else if(max(mx[C], (int) G.size() - sz[C]) > max(mx[to], (int) G.size() - sz[to])){
- C = to;
- }
- }
- int D = G.size();
- cnt[0]++;
- lev[C] = 0;
- for(int to: g[C]){
- if(u[to]) continue;
- G.clear();
- dfs(to, C);
- for(int V: G){
- if(lev[V] <= k){
- ans += cnt[k-lev[V]];
- }
- }
- for(int V: G){
- cnt[lev[V]]++;
- }
- }
- for(int i = 0; i < D; i++) cnt[i] = 0;
- u[C] = 1;
- for(int to: g[C]){
- if(u[to]) continue;
- solve(to);
- }
- }
- int main(){
- scanf("%d%d", &n, &k);
- for(int i = 1, x, y;i < n; i++){
- scanf("%d%d", &x, &y);
- g[x].push_back(y);
- g[y].push_back(x);
- }
- solve(1);
- cout << ans << "\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement