rembocoder

Untitled

Apr 11th, 2023
563
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.39 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define int int64_t
  6.  
  7. const int inf = 2e18;
  8. const int mod = 1e9 + 7;
  9.  
  10. vector<vector<int>> g;
  11. vector<vector<int>> dp;
  12.  
  13. void relax(int& a, int b) {
  14.     a = min(a, b);
  15. }
  16.  
  17. void count_dp(int v, int p) {
  18.     vector<int> sons;
  19.     for (int to: g[v]) {
  20.         if (to == p) {
  21.             continue;
  22.         }
  23.         sons.push_back(to);
  24.         count_dp(to, v);
  25.     }
  26.     dp[v].resize(2);
  27.     dp[v][1] = 0;
  28.     for (int to: sons) {
  29.         vector<int> new_dp(dp[v].size() + dp[to].size() - 1, inf);
  30.         for (int x = 1; x < dp[v].size(); x++) {
  31.             relax(new_dp[x], dp[v][x] + 1);
  32.         }
  33.         for (int x = 1; x < dp[v].size(); x++) {
  34.             for (int y = 1; y < dp[to].size(); y++) {
  35.                 relax(new_dp[x + y], dp[v][x] + dp[to][y]);
  36.             }
  37.         }
  38.         dp[v] = new_dp;
  39.     }
  40. }
  41.  
  42. int32_t main() {
  43.     ios_base::sync_with_stdio(0);
  44.     cin.tie(0); cout.tie(0);
  45.     int n, k;
  46.     cin >> n >> k;
  47.     g.resize(n);
  48.     for (int i = 0; i < n - 1; i++) {
  49.         int a, b;
  50.         cin >> a >> b;
  51.         a--; b--;
  52.         g[a].push_back(b);
  53.         g[b].push_back(a);
  54.     }
  55.     dp.resize(n);
  56.     count_dp(0, -1);
  57.     int ans = inf;
  58.     for (int v = 0; v < n; v++) {
  59.         if (k < dp[v].size()) {
  60.             relax(ans, dp[v][k] + (v != 0));
  61.         }
  62.     }
  63.     cout << ans << '\n';
  64. }
  65.  
Advertisement
Add Comment
Please, Sign In to add comment