A. Linova and Kingdom
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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?
Input
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.
Output
Print the only line containing a single integer — the maximum possible sum of happinesses of all envoys.
<코드>
#include <bits/stdc++.h>
using namespace std;
vector<int> Edges[200005];
bool Visit[200005];
class node
{
public:
int cnt, dist;
bool operator < (const node& a) const
{
return dist - cnt < a.dist - a.cnt;
}
} a[200005];
int dfs(int pos)
{
// a[pos].dist <-- known
// Edges[pos][0] ... Edges[pos][size()-1]
int i, k; k = Edges[pos].size();
int count = 0;
Visit[pos] = true;
for (i = 0; i < k; i++) {
if (!Visit[Edges[pos][i]]) {
a[Edges[pos][i]].dist = a[pos].dist + 1;
count += dfs(Edges[pos][i]);
}
}
a[pos].cnt = count;
return count + 1;
}
int main()
{
long long ans;
int n, k, p, q;
int i, j;
scanf("%d %d", &n, &k);
for (i = 1; i < n; i++) {
scanf("%d %d", &p, &q);
Edges[p].push_back(q);
Edges[q].push_back(p);
}
a[1].dist = 0;
dfs(1);
sort(a + 1, a + n + 1);
ans = 0;
for (j = n; j > n - k; j--)
ans += a[j].dist - a[j].cnt;
printf("%lld\n", ans);
return 0;
}
https://codeforces.com/problemset/problem/1336/A
'🧩PS > 🥇Hard' 카테고리의 다른 글
[C/C++] Codeforce 545E - Paths and Trees (0) | 2020.07.02 |
---|---|
[C/C++] Codeforce 20C - Dijkstra? (0) | 2020.06.27 |
[C/C++] Codeforce 1324F - Maximum White Subtree (0) | 2020.06.09 |
[C/C++] Codeforce 486E - LIS of Sequence (0) | 2020.06.09 |
[C/C++] Codeforce 52C - Circular RMQ (0) | 2020.06.07 |