A. Linova and Kingdom Codeforces Round 635 (Div. 1)

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�−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 n�, 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 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?

Input

The first line contains two integers n� and k� (2≤n≤2⋅1052≤�≤2⋅105, 1≤k<n1≤�<�)  — the number of cities and industry cities respectively.

Each of the next n−1�−1 lines contains two integers u� and v� (1≤u,v≤n1≤�,�≤�), 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.

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.

In the second example, choosing cities 33, 44 developing industry can reach a sum of 33, but remember that Linova plans to choose exactly k� cities developing industry, then the maximum sum is 22.

题目大致意思:给n个城市,选k个城市,求1到所有选择的城市,途中没有被选的城市的个数。

AC题目思路:暴力dfs。

照树状图处理所有的城市:

ll dfs(ll x ,ll y ,ll z){//当前点,从哪个点过来的,节点深度 
	ll sum = 0;
	for(auto i : lian[x]){
		if(i == y)continue;
		sum += dfs(i,x,z+1);
	}//让非叶子节点的值 = -所有相连叶子节点到此的深度
	v[x] = z-sum;//叶子节点的值 = 深度
	return sum+1;
}

AC代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl "\n"

const ll N = 2e5+7;
ll n,m,k;
vector<ll>lian[N];//连接图
ll v[N];//存值

ll dfs(ll x ,ll y ,ll z){//当前点,从哪个点过来的,节点深度 
	ll sum = 0;
	for(auto i : lian[x]){
		if(i == y)continue;
		sum += dfs(i,x,z+1);
	}//让非叶子节点的值 = -所有相连叶子节点到此的深度
	v[x] = z-sum;//叶子节点的值 = 深度
	return sum+1;
}

void solve(){
	cin >> n >> k;
	ll x,y,sum=0;
	for(ll i = 1 ; i < n ; i ++)
		cin >> x >> y,
		lian[x].push_back(y),lian[y].push_back(x);
	dfs(1,0,0);
	sort(v+1,v+n+1);
	for(ll i = 0 ; i < k ; i ++)sum +=v[n-i];
	cout << sum << endl;
	return;
}

int main(){
	ll t=1;//cin >> t;
	while(t--)solve();
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值