A13443 | Linova and Kingdom
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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?
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?
输入格式
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.
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.
输出格式
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 $2$ , $5$ , $6$ , $7$ to develop industry, then the happiness of the envoy from city $2$ is $1$ , the happiness of envoys from cities $5$ , $6$ , $7$ is $2$ . The sum of happinesses is $7$ , and it can be proved to be the maximum one.

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

In the second example, choosing cities $3$ , $4$ developing industry can reach a sum of $3$ , but remember that Linova plans to choose exactly $k$ cities developing industry, then the maximum sum is $2$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted