CF1336A.Linova and Kingdom

普及/提高-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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?

创作轻小说是林ova生命中最重要的事情。昨晚,林ova梦见了一个奇妙的王国。她一醒来便立刻开始为这个王国撰写轻小说,当然,她本人就是这个王国的女王。

王国中共有 nn 座城市,以及 n−1n-1 条双向道路连接若干对城市。从任意一座城市出发,均可通过若干条道路到达其余任意一座城市。城市编号为 11 至 nn,其中城市 11 是王国的首都。因此,该王国的结构是一棵树。

作为女王,林ova 计划恰好选择 kk 座城市发展工业,其余城市则发展旅游业。首都也可以被选为工业城市或旅游城市。

每年在首都举行一次会议。为参加此次会议,每座工业城市均需派出一名使者。所有使者均沿其出发城市至首都的唯一最短路径行进。

在旅游城市中旅行令人愉悦。对每位使者而言,其幸福感等于其路径上所经过的旅游城市的数量。

为了成为一位受人民爱戴的女王,林ova 希望选出 kk 座城市,使得所有使者的幸福感之和最大。你能帮她计算出这个最大值吗?

输入格式

The first line contains two integers nn and kk (2≤n≤2⋅1052\le n\le 2 \cdot 10^5, 1≤k<n1\le k \lt 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\le u,v\le 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.

第一行包含两个整数 nn 和 kk(2≤n≤2⋅1052\le n\le 2 \cdot 10^5,1≤k<n1\le k \lt n)—— 分别表示城市的数量和工业城市的数量。

接下来的 n−1n-1 行中,每行包含两个整数 uu 和 vv(1≤u,v≤n1\le u,v\le n),表示城市 uu 与城市 vv 之间有一条道路相连。

保证任意两座城市之间均可通过道路相互到达。

输出格式

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

输出唯一一行,包含一个整数——所有使节幸福感的最大可能总和。

输入输出样例

  • 输入#1

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

    输出#1

    7
  • 输入#2

    4 1
    1 2
    1 3
    2 4

    输出#2

    2
  • 输入#3

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

    输出#3

    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.

在第一个例子中,Linova 可选择城市 22、55、66、77 发展工业,则来自城市 22 的使者幸福感为 11,来自城市 55、66、77 的使者幸福感均为 22。幸福感总和为 77,且可证明这是最大值。

在第二个例子中,若选择城市 33、44 发展工业,可得到总和 33;但请注意,Linova 计划恰好选择 kk 个城市发展工业,因此最大总和为 22。

输入解题思路,AI测评打分。不知道怎么写?

首页