-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathC1337.cpp
More file actions
61 lines (48 loc) · 859 Bytes
/
Copy pathC1337.cpp
File metadata and controls
61 lines (48 loc) · 859 Bytes
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
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define pb push_back
using namespace std;
vector<int> adj[200005];
int size[200005];
int depth[200005];
bool visited[200005];
vector<int> placeTurism;
int dfs(int u, int p)
{
size[u] = 1;
depth[u] = depth[p] + 1;
for(int v : adj[u])
{
if(v != p)
{
size[u] += dfs(v, u);
}
}
placeTurism.pb(size[u]-1 - depth[u]);
return size[u];
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int n, k; cin >> n >> k;
for(int i = 0; i < n-1; i++)
{
int u, v; cin >> u >> v;
adj[u].pb(v);
adj[v].pb(u);
}
depth[-1] = -1;
depth[1] = 0;
visited[1] = true;
dfs(1, -1);
sort(placeTurism.rbegin(), placeTurism.rend());
ll sum = 0;
for(int i = 0; i < n-k; i++)
{
sum+=placeTurism[i];
}
cout << sum << '\n';
return 0;
}