[Codeforces Round #635 (div2)]1337C - Linova and Kingdom[dfs][贪心]

1337C - Linova and Kingdom[ d f s dfs dfs][贪心]

time limit per testmemory limit per testinputoutput
2 seconds256 megabytesstandard inputstandard output

Description:

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 n n cities and n − 1 n−1 n1 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 1 1 to n n n, and the city 1 1 1 is the capital of the kingdom. So, the kingdom has a tree structure.

As the queen, Linova plans to choose exactly k k 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 k 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 n n and k ( 2 ≤ n ≤ 2 ⋅ 1 0 5 , 1 ≤ k < n ) k (2≤n≤2⋅10^5, 1≤k<n) k(2n2105,1k<n) — the number of cities and industry cities respectively.

Each of the next n − 1 n−1 n1 lines contains two integers u u u and v ( 1 ≤ u , v ≤ n ) v (1≤u,v≤n) v(1u,vn), denoting there is a road connecting city u u u and city v v 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.


One Example input

7 4
1 2
1 3
1 4
3 5
3 6
4 7

One Example output

7

Two Example input

4 1
1 2
1 3
2 4

Two Example output

2

Three Example input

8 5
7 5
1 7
6 1
3 7
8 3
2 1
4 5

Three Example output

9

Hit

在这里插入图片描述
In the first example, Linova can choose cities 2 , 5 , 6 , 7 2, 5, 6, 7 2,5,6,7 to develop industry, then the happiness of the envoy from city 2 2 2 is 1 1 1, the happiness of envoys from cities 5 , 6 , 7 5, 6, 7 5,6,7 is 2 2 2. The sum of happinesses is 7 7 7, and it can be proved to be the maximum one.
在这里插入图片描述
In the second example, choosing cities 3 , 4 3, 4 3,4 developing industry can reach a sum of 3 3 3, but remember that Linova plans to choose exactly k k k cities developing industry, then the maximum sum is 2 2 2.


分析:
题意:
一个联通图,固定 1 1 1 为最终要到达的地方即根节点
k k k 个点作为工业城市,其他的都为旅游城市( 1 1 1都不是)
工业城市可以通过简单路径出发到 1 1 1 这个根节点
途中每经过一个旅游城市可以得到一点开心值(?我不知道我乱说的,懒得再看一遍题目)
问每个工业城市的开心值总和最大化可以为多少

做法:
对于每个点成为工业城市后的贡献值
应该为 d e p [ u ] − s o n [ u ] + 1 dep[u] - son[u] + 1 dep[u]son[u]+1
d e p [ 1 ] = 0 dep[1] = 0 dep[1]=0
原因是要使得贡献值最大,我们必然会从叶子结点开始选
那么叶子结点选完了,肯定优先选择儿子数量最少且距离叶子结点最近的点
(这也是我一开始的思路,但是不知道为什么写着写着写挂了)
那么选择这个点,必然是他的儿子结点都被选了
本来这个点可以得到的贡献值是 d e p dep dep
现在要扣掉儿子都被选了 s o n − 1 son - 1 son1
然后排序选取贡献值最大的 k k k

参考博客 C. Linova and Kingdom (DFS&贪心)


Code:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 4e5 + 5;

vector<int> ed[maxn];
int cnt[maxn]; // 表示这个点涂黑后的贡献值 = 深度 - 儿子的个数 + 1

int dfs(int u, int fa, int deep) {
    int son = 1;
    for(auto v : ed[u]) {
        if(v == fa) continue;
        son += dfs(v, u, deep + 1);
    }
    cnt[u] = deep - son + 1;
    return son;
}

int main() {
    int n, u, v, k;
    scanf("%d%d", &n, &k);
    for(int i = 1; i < n; ++i) {
        scanf("%d%d", &u, &v);
        ed[u].push_back(v), ed[v].push_back(u);
    }
    ll ans = 0;
    dfs(1, 1, 0);
    sort(cnt + 1, cnt + 1 + n);
    for(int i = n; i > n - k; --i)
        ans += cnt[i] * 1ll;
    printf("%lld\n", ans);
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值