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 nn cities and n−1n−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 11 to nn, and the city 11 is the capital of the kingdom. So, the kingdom has a tree structure.
As the queen, Linova plans to choose exactly kk 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 kk cities which can maximize the sum of happinesses of all envoys. Can you calculate the maximum sum for her?
The first line contains two integers nn and kk (2≤n≤2⋅1052≤n≤2⋅105, 1≤k<n1≤k<n) — the number of cities and industry cities respectively.
Each of the next n−1n−1 lines contains two integers uu and vv (1≤u,v≤n1≤u,v≤n), denoting there is a road connecting city uu and city vv.
It is guaranteed that from any city, you can reach any other city by the roads.
Print the only line containing a single integer — the maximum possible sum of happinesses of all envoys.
7 4 1 2 1 3 1 4 3 5 3 6 4 7
7
4 1 1 2 1 3 2 4
2
8 5 7 5 1 7 6 1 3 7 8 3 2 1 4 5
9
In the first example, Linova can choose cities 22, 55, 66, 77 to develop industry, then the happiness of the envoy from city 22 is 11, the happiness of envoys from cities 55, 66, 77 is 22. The sum of happinesses is 77, and it can be proved to be the maximum one.
In the second example, choosing cities 33, 44 developing industry can reach a sum of 33, but remember that Linova plans to choose exactly kk cities developing industry, then the maximum sum is 22.
贪心,每个节点的贡献=深度-所有子孙个数,把贡献排个序,加k个就可以了。
#include <iostream> #include <vector> #include <algorithm> #include <string> #include <set> #include <queue> #include <map> #include <sstream> #include <cstdio> #include <cstring> #include <numeric> #include <cmath> #include <iomanip> #include <deque> #include <bitset> //#include <unordered_set> //#include <unordered_map> //#include <bits/stdc++.h> //#include <xfunctional> #define ll long long #define PII pair<int, int> #define rep(i,a,b) for(int i=a;i<=b;i++) #define dec(i,a,b) for(int i=a;i>=b;i--) #define pb push_back #define mk make_pair using namespace std; int dir[4][2] = { { 0,1 } ,{ 0,-1 },{ 1,0 },{ -1,0 } }; const long long INF = 0x7f7f7f7f7f7f7f7f; const int inf = 0x3f3f3f3f; const double pi = 3.14159265358979; const int mod = 998244353; const int N = 2e5+5; //if(x<0 || x>=r || y<0 || y>=c) inline ll read() { ll x = 0; bool f = true; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); } while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar(); return f ? x : -x; } ll gcd(ll m, ll n) { return n == 0 ? m : gcd(n, m%n); } struct node { int steps = 0,sub = 0; vector<int> sa; }a[N]; bool cmp(node a, node b) { return a.steps-a.sub > b.steps-b.sub; } vector<bool> vis(N); int dfs(int x) { vis[x] = 1; for (int i = 0; i < a[x].sa.size(); i++) { if (!vis[a[x].sa[i]]) { a[x].sub++; a[a[x].sa[i]].steps = a[x].steps + 1; a[x].sub+=dfs(a[x].sa[i]); } } return a[x].sub; } int main() { int n, k; cin >> n >> k; for (int i = 1; i <= n - 1; i++) { int s, t; cin >> s >> t; a[s].sa.push_back(t); a[t].sa.push_back(s); } dfs(1); sort(a + 1, a + n + 1, cmp); ll res = 0; rep(i, 1, k) { res += a[i].steps-a[i].sub; } cout << res << endl; return 0; }