Linova and Kingdom CodeForces - 1337C(贪心+排序)

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?

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

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

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

Examples
Input
7 4
1 2
1 3
1 4
3 5
3 6
4 7
Output
7
Input
4 1
1 2
1 3
2 4
Output
2
Input
8 5
7 5
1 7
6 1
3 7
8 3
2 1
4 5
Output
9
Note

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.
在这里插入图片描述
思路:这个问题的关键就是:选择第num个点的时候,如何确定?也就是这个点所带来的贡献和这个点所带来的的损失之差应该最大。如图所示:
在这里插入图片描述
对于4来说:选取它,带来的贡献是它的深度deep[4],而它带来的损失是它的两个儿子节点走的城市少了,少了多少?少了son[4]个。也就是2个。
对于9来说:选取它,带来的贡献是它的深度deep[9],而它带来的损失是它的两个儿子节点走的城市少了,少了多少?少了son[9]个,也就是3个。
(为什么带来的损失就是儿子数量呢?我们贪心的想一下,肯定是先选取深度大的点更优一些,这样的话,如果选取这个点,那么它的儿子节点一定被选取了。)
我们比较一下差值,发现选取9更优一些。
即:deep[i]-son[i]>deep[j]-son[j]。我们要保证这个前提下,才能保证选取的是最优的。因此dfs一遍,处理出深度和儿子节点的数量,然后排序就可以了。
代码如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;

const int maxx=2e5+100;
struct edge{
	int to,next;
}e[maxx<<1];
struct node{
	ll son;
	ll dep;
	int id;
	bool operator<(const node &a)const{
		return dep-son>a.dep-a.son;
	}
}p[maxx];
int head[maxx<<1],vis[maxx];
ll deep[maxx],son[maxx];
int n,tot,k;

inline void init()
{
	memset(head,-1,sizeof(head));
	memset(vis,0,sizeof(vis));
	tot=0;
	for(int i=1;i<=n;i++) p[i].dep=p[i].son=0;
}
inline void add(int u,int v)
{
	e[tot].next=head[u],e[tot].to=v,head[u]=tot++;
}
inline void dfs(int u,int f,int num)
{
	p[u].dep=num;
	p[u].son=1;
	p[u].id=u;
	for(int i=head[u];i!=-1;i=e[i].next)
	{
		int to=e[i].to;
		if(to==f) continue;
		dfs(to,u,num+1);
		p[u].son+=p[to].son;
	}
}
inline void Dfs(int u,int f,ll &ans)
{
	if(k<=0) return ;
	if(vis[u]==1) 
	{
		ans+=(ll)min((ll)k,son[u])*(ll)deep[u];
		k-=son[u];
		return ;
	}
	for(int i=head[u];i!=-1;i=e[i].next)
	{
		int to=e[i].to;
		if(to==f) continue;
		Dfs(to,u,ans);
	}
}
int main()
{
	scanf("%d%d",&n,&k);
	init();
	int x,y;
	for(int i=1;i<n;i++)
	{
		scanf("%d%d",&x,&y);
		add(x,y);
		add(y,x);
	}
	dfs(1,0,0);
	sort(p+1,p+1+n);
	for(int i=1;i<=k;i++) vis[p[i].id]=1;
	for(int i=1;i<=n;i++) deep[p[i].id]=p[i].dep,son[p[i].id]=p[i].son;
	ll ans=0;
	Dfs(1,0,ans);
	cout<<ans<<endl;
	return 0;
}

努力加油a啊,(o)/~

发布了668 篇原创文章 · 获赞 118 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/starlet_kiss/article/details/105585983