# Codeforces 1336A Linova and Kingdom【贪心】

### 题目

Writing light novels is the most important thing in Linova's life. Last night, Linova dreamed about a fantastic kingdom. She began to write a light novel for the kingdom as soon as she woke up, and of course, she is the queen of it.

There are n cities and n-1 two-way roads connecting pairs of cities in the kingdom. From any city, you can reach any other city by walking through some roads. The cities are numbered from 1 to n, and the city 1 is the capital of the kingdom. So, the kingdom has a tree structure.

As the queen, Linova plans to choose exactly k cities developing industry, while the other cities will develop tourism. The capital also can be either industrial or tourism city.

A meeting is held in the capital once a year. To attend the meeting, each industry city sends an envoy. All envoys will follow the shortest path from the departure city to the capital (which is unique).

Traveling in tourism cities is pleasant. For each envoy, his happiness is equal to the number of tourism cities on his path.

In order to be a queen loved by people, Linova wants to choose k cities which can maximize the sum of happinesses of all envoys. Can you calculate the maximum sum for her?

#### Input

The first line contains two integers n and k (2\le n\le 2 \cdot 10^5, 1\le k< n) — the number of cities and industry cities respectively.

Each of the next n-1 lines contains two integers u and v (1\le u,v\le n), denoting there is a road connecting city u and city v.

It is guaranteed that from any city, you can reach any other city by the roads.

#### Output

Print the only line containing a single integer — the maximum possible sum of happinesses of all envoys.

### Code

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <queue>
#define MAX_N 200005
#define pii pair<int, int>
#define int long long

using namespace std;

int n, K, ans = 0;
int dep[MAX_N];
int siz[MAX_N];
int cnt[MAX_N];
vector<int> e[MAX_N];
priority_queue<pii> q;

void dfs(int x, int p, int d) {
dep[x] = d, siz[x] = 1;
for (int i = 0; i < e[x].size(); i++) {
int t = e[x][i];
if (t != p) {
dfs(t, x, d + 1);
siz[x] += siz[t];
}
}
}

signed main() {
scanf("%lld%lld", &n, &K);
int x, y;
for (int i = 1; i < n; i++) {
scanf("%lld%lld", &x, &y);
e[x].push_back(y);
e[y].push_back(x);
}
dfs(1, 0, 0);
for (int i = 1; i <= n; i++) {
cnt[i] = (i == 1 ? e[i].size() : e[i].size() - 1);
if (!cnt[i]) {
q.push(pii(dep[i], i));
}
}
for (int i = 1; i <= K; i++) {
pii p = q.top();
q.pop(), x = p.second, ans += p.first;
for (int j = 0; j < e[x].size(); j++) {
int t = e[x][j];
cnt[t]--;
if (!cnt[t]) {
q.push(pii(dep[t] - (siz[t] - 1), t));
}
}
}
printf("%lld\n", ans);
}