forked from mrsac7/CSES-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 1
/
2080 - Fixed-Length Paths I.cpp
84 lines (72 loc) · 1.59 KB
/
2080 - Fixed-Length Paths I.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
Problem Name: Fixed-Length Paths I
Problem Link: https://cses.fi/problemset/task/2080
Author: Sachin Srivastava (mrsac7)
*/
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
const int mxN = 2e5+5;
vector<int> adj[mxN];
int mark[mxN], dp[mxN], dis[mxN] = {1};
int dfs(int s, int p) {
dp[s] = 1;
for (auto i: adj[s]) {
if (i != p && !mark[i]) {
dp[s] += dfs(i, s);
}
}
return dp[s];
}
int k;
int ans = 0, mxd = 0;
int dfs2(int s, int p, int n) {
for (auto i: adj[s]) {
if (i != p && !mark[i] && dp[i] > n/2) {
return dfs2(i, s, n);
}
}
return s;
}
void dfs3(int s, int p, int d, int flag) {
if (d > k) return;
mxd = max(mxd, d);
if (flag) dis[d]++;
else ans += dis[k-d];
for (auto i: adj[s]) {
if (i != p && !mark[i])
dfs3(i, s, d+1, flag);
}
}
void solve(int s) {
dfs(s, 0);
int ct = dfs2(s, 0, dp[s]);
mark[ct] = 1;
mxd = 0;
for (auto i: adj[ct]) {
if (!mark[i]) {
dfs3(i, ct, 1, 0);
dfs3(i, ct, 1, 1);
}
}
fill(dis + 1, dis + mxd + 1, 0);
for (auto i: adj[ct]) if (!mark[i]) {
solve(i);
}
}
signed main(){
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#ifdef LOCAL
freopen("input.txt", "r" , stdin);
freopen("output.txt", "w", stdout);
#endif
int n; cin>>n>>k;
for (int i = 1; i < n; i++) {
int x, y; cin>>x>>y;
adj[x].push_back(y);
adj[y].push_back(x);
}
solve(1);
cout<<ans;
}