Advertisement
IanisBelu

Fixed-Length Paths I

Dec 1st, 2023
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.43 KB | Source Code | 0 0
  1. #ifdef LOCAL
  2. #include <iostream>
  3. #include <fstream>
  4. #include <iomanip>
  5. #include <vector>
  6. #include <queue>
  7. #include <stack>
  8. #include <set>
  9. #include <map>
  10. #else
  11. #include <bits/stdc++.h>
  12. #define cerr if (false) cerr
  13. #define endl '\n'
  14. #endif
  15.  
  16. #define fi first
  17. #define se second
  18.  
  19. #define sz(a) ((int)(a).size())
  20. #define all(a) (a).begin(), (a).end()
  21.  
  22. #define lsb(x) (x & (-x))
  23.  
  24. using namespace std;
  25.  
  26. template <typename T>
  27. bool ckmax(T &a, T b) { return a < b ? a = b, true : false; }
  28. template <typename T>
  29. bool ckmin(T &a, T b) { return a > b ? a = b, true : false; }
  30.  
  31. using ll = long long;
  32. using pii = pair<int, int>;
  33.  
  34. const int NMAX = 2e5+5;
  35. //const int INF = 1e9+5;
  36.  
  37. int n, k;
  38. vector<int> g[NMAX];
  39. int blocked[NMAX];
  40. int sz[NMAX];
  41. int fr[NMAX];
  42. int maxd;
  43. ll ans;
  44.  
  45. void read() {
  46.    cin >> n >> k;
  47.    for (int i = 1, x, y; i < n; i++) {
  48.       cin >> x >> y;
  49.       g[x].push_back(y);
  50.       g[y].push_back(x);
  51.    }
  52. }
  53.  
  54. void dfs_size(int x, int p = 0) {
  55.    sz[x] = 1;
  56.    for (auto &u : g[x]) {
  57.       if (u == p || blocked[u]) continue;
  58.       dfs_size(u, x);
  59.       sz[x] += sz[u];
  60.    }
  61. }
  62.  
  63. int find_centroid(int x, int cnt, int p = 0) {
  64.    for (auto &u : g[x]) {
  65.       if (u == p || blocked[u]) continue;
  66.       if (sz[u] << 1 >= cnt)
  67.          return find_centroid(u, cnt, x);
  68.    }
  69.    return x;
  70. }
  71.  
  72. void dfs_count(int x, int p, int d) {
  73.    maxd = max(maxd, d);
  74.    ans += fr[k - d];
  75.    if (k <= d) return;
  76.    for (auto &u : g[x]) {
  77.       if (u == p || blocked[u]) continue;
  78.       dfs_count(u, x, d + 1);
  79.    }
  80. }
  81.  
  82. void dfs_mark(int x, int p, int d) {
  83.    fr[d]++;
  84.    for (auto &u : g[x]) {
  85.       if (u == p || blocked[u]) continue;
  86.       dfs_mark(u, x, d + 1);
  87.    }
  88. }
  89.  
  90. void centroid_decomp(int x = 1) {
  91.    dfs_size(x);
  92.  
  93.    x = find_centroid(x, sz[x]);
  94.    blocked[x] = true;
  95.  
  96.    fr[0] = 1;
  97.    maxd = 0;
  98.  
  99.    for (auto &u : g[x]) {
  100.       if (blocked[u]) continue;
  101.       dfs_count(u, x, 1);
  102.       dfs_mark(u, x, 1);
  103.    }
  104.  
  105.    memset(fr, 0, (maxd + 1) * sizeof(int));
  106.  
  107.    for (auto &u : g[x]) {
  108.       if (blocked[u]) continue;
  109.       centroid_decomp(u);
  110.    }
  111. }
  112.  
  113. ll solve() {
  114.    centroid_decomp();
  115.    return ans;
  116. }
  117.  
  118. signed main() {
  119. #ifdef LOCAL
  120.    freopen("input.txt", "r", stdin);
  121. #endif
  122.    ios_base::sync_with_stdio(false);
  123.    cin.tie(0);
  124.    cout.tie(0);
  125.    
  126.    int t = 1;
  127. //   cin >> t;
  128.  
  129.    while (t--) {
  130.       read();
  131.       cout << solve() << endl;
  132.    }
  133.  
  134.    return 0;
  135. }
  136.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement