Codeforces 1337C Linova and Kingdom

C. 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.

Examples

input

Copy

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

output

Copy

7

input

Copy

4 1
1 2
1 3
2 4

output

Copy

2

input

Copy

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

output

Copy

9

Note

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.

3ffb39c877eca7565234f5b27836c176c6a85f3f.pnguploading.4e448015.gif正在上传…重新上传取消

 

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个点,然后求出这K个点到编号为1的点所经过的除了这K个点的其他点的总和。

 

假设所有的点全部选中,然后只需要选择N-K个,剩下的就是要选的K个点,考虑选择的N-K个点每个点对答案的贡献为:当前点的子节点-当前点的父节点的个数,由此DFS求出每个点的深度和他的子节点,然后按照贡献规则,排序选择前N-K个点就是其最大。

 

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=5e5+10;
int head[maxn],vis[maxn];
struct edge
{
	int to,w,next;
}edge[maxn];
struct node
{
	int deep,cnt;
	bool operator < (const node &x) const{
		return 	cnt-deep > x.cnt-x.deep;
	}
}p[maxn]; 
int cnt=0;
void add(int u,int v,int w)
{
	edge[cnt].to=v;
	edge[cnt].w=w;
	edge[cnt].next=head[u];
	head[u]=cnt++;
}
int dfs(int index,int deep)
{
	p[index].deep=deep;
	vis[index]=1;
	for(int i=head[index];~i;i=edge[i].next)
	{
		if(vis[edge[i].to]==0)
		p[index].cnt += dfs(edge[i].to,deep+1);
	}
	return p[index].cnt+1;
}
int main()
{
	memset(head,-1,sizeof(head));
	int n,k;
	cin>>n>>k;
	for(int i=0;i<n-1;i++)
	{
		int u,v,w;
		cin>>u>>v;
		add(u,v,1);
		add(v,u,1);
	}
	dfs(1,0);
	sort(p+1,p+1+n);
	ll ans=0;
	for(int i=1;i<=n-k;i++)
	{
		ans += p[i].cnt-p[i].deep;
	}
	cout<<ans<<endl;
	return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值