Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define int int64_t
- const int inf = 2e18;
- const int mod = 1e9 + 7;
- vector<vector<int>> g;
- vector<vector<int>> dp;
- void relax(int& a, int b) {
- a = min(a, b);
- }
- void count_dp(int v, int p) {
- vector<int> sons;
- for (int to: g[v]) {
- if (to == p) {
- continue;
- }
- sons.push_back(to);
- count_dp(to, v);
- }
- dp[v].resize(2);
- dp[v][1] = 0;
- for (int to: sons) {
- vector<int> new_dp(dp[v].size() + dp[to].size() - 1, inf);
- for (int x = 1; x < dp[v].size(); x++) {
- relax(new_dp[x], dp[v][x] + 1);
- }
- for (int x = 1; x < dp[v].size(); x++) {
- for (int y = 1; y < dp[to].size(); y++) {
- relax(new_dp[x + y], dp[v][x] + dp[to][y]);
- }
- }
- dp[v] = new_dp;
- }
- }
- int32_t main() {
- ios_base::sync_with_stdio(0);
- cin.tie(0); cout.tie(0);
- int n, k;
- cin >> n >> k;
- g.resize(n);
- for (int i = 0; i < n - 1; i++) {
- int a, b;
- cin >> a >> b;
- a--; b--;
- g[a].push_back(b);
- g[b].push_back(a);
- }
- dp.resize(n);
- count_dp(0, -1);
- int ans = inf;
- for (int v = 0; v < n; v++) {
- if (k < dp[v].size()) {
- relax(ans, dp[v][k] + (v != 0));
- }
- }
- cout << ans << '\n';
- }
Advertisement
Add Comment
Please, Sign In to add comment