-
Notifications
You must be signed in to change notification settings - Fork 0
/
24844.cpp
62 lines (62 loc) · 1.28 KB
/
24844.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
#include<bits/stdc++.h>
using namespace std;
vector<int> adj[10101];
vector<int> lvl[10101];
int cnt[10101];
int con[10101];
int parent[10101];
int maxD;
int go(int s, int p, int d) {
++cnt[s];
parent[s] = p;
maxD = max(maxD, d);
lvl[d].push_back(s);
for (auto nx : adj[s]) {
if (nx == p) continue;
cnt[s] += go(nx, s, d + 1);
}
return cnt[s];
}
int go2(int s, int p) {
int res = 1;
for (auto nx : adj[s]) {
if (nx == p || con[nx] == 0) continue;
res += go2(nx, s);
}
return res;
}
int main() {
int n, k; scanf("%d %d", &n, &k);
for (int i = 0; i < n - 1; ++i) {
int a, b; scanf("%d %d", &a, &b);
adj[a].push_back(b);
adj[b].push_back(a);
}
go(1, -1, 0);
for (int i = maxD; i > 0; --i) {
priority_queue<pair<int, int>> q;
vector<int> noCut;
if (lvl[i].size() <= k) {
for (auto x : lvl[i])
con[x] = 1;
continue;
}
for (auto x : lvl[i])
q.push({ -cnt[x], x });
while (q.size() && noCut.size() + q.size() > k) {
auto cur = q.top();
q.pop();
if (con[cur.second])
noCut.push_back(cur.second);
else
cnt[parent[cur.second]] += cur.first;
while (q.size()) {
auto cur = q.top(); q.pop();
con[parent[cur.second]] = con[cur.second] = 1;
}
for (auto x : noCut)
con[parent[x]] = con[x] = 1;
}
}
printf("%d", go2(1, -1));
}