-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCF_1336A.cpp
63 lines (49 loc) · 985 Bytes
/
CF_1336A.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
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <utility>
using namespace std;
vector<int> edges[200001];
int level[200001];
int subtree[200001];
int det[200001];
int dfs(int node, int from) {
level[node] = level[from] + 1;
subtree[node] = 1;
for (int i = 0; i < edges[node].size(); ++i)
{
int next = edges[node][i];
if (next == from) {
continue;
}
subtree[node] += dfs(next, node);
}
det[node] = subtree[node] - level[node];
return subtree[node];
}
int main() {
int n, k;
cin >> n >> k;
for (int i = 0; i < n - 1; i++)
{
int v, u;
cin >> v >> u;
edges[v].push_back(u);
edges[u].push_back(v);
}
dfs(1, 0);
// sort by det
vector< pair<int, int> > sorting;
for (int i = 1; i <= n; i++)
{
sorting.push_back(make_pair(det[i], i));
}
sort(sorting.begin(), sorting.end());
long long answer = 0;
for (int i = 0; i < n - k; i++)
{
answer += sorting[n - i - 1].first;
}
cout << answer << endl;
}