-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1336a.cpp
55 lines (46 loc) · 934 Bytes
/
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
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <queue>
using namespace std;
using ll = long long;
const int MAX = 200005;
vector<int> adj[MAX];
priority_queue<int> pq;
inline void quick_IO(){
ios_base :: sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
}
int dfs(int currNode, int prevNode, int level){
int child = 0;
for(auto x:adj[currNode]){
if(x!=prevNode){
child += dfs(x, currNode, level+1) + 1;
}
}
pq.push(level - child);
return child;
}
int main(){
quick_IO();
int n, counts;
int i;
int a, b;
cin>>n>>counts;
for(i=0;i<n-1;i++){
cin>>a>>b;
adj[a].push_back(b);
adj[b].push_back(a);
}
dfs(1, -1, 0);
ll answer = 0;
for(i=0;i<counts;i++){
answer += pq.top();
pq.pop();
}
cout<<answer<<"\n";
return 0;
}