Codeforces Round #635 (Div. 2) C. Linova and Kingdom

https://codeforces.com/contest/1337/problem/C

C. Linova and Kingdom

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
inputCopy
7 4
1 2
1 3
1 4
3 5
3 6
4 7
outputCopy
7
inputCopy
4 1
1 2
1 3
2 4
outputCopy
2
inputCopy
8 5
7 5
1 7
6 1
3 7
8 3
2 1
4 5
outputCopy
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.

 题目描述:

在以1为根的树上选k个点作为工业城市,其他为旅游城市。现在要从每个工业城市出发到1城市,途中每有一个旅游城市,幸福指数加1。求怎么选定工业城市使得总的幸福指数最大,输出最大的幸福指数。

分析:

先从根1-dfs下去,记录下每个点i的深度dep,和以这个点i为根,树的大小size。对于所有的i的val=dep-size,答案是最大的k个val的和。

因为旅游城市总是在重复最多的点上越好,可以看出他是围绕根1分布的,工业城市的子树都是工业城市。假设i为工业城市,dep[i]就是i到1的距离,就是经过旅游城市数。假设点i是旅游城市,他的子树的工业城市都会经过他,相反,如果他是工业城市,使得他子树的节点经过的旅游城市都少1,对总幸福指数的影响就是少size[i]。

代码:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<sstream>
#include<vector>
#include<stack>
#include<deque>
#include<cmath>
#include<map>
using namespace std;
typedef long long ll;
const int maxn=2e5+6;
struct node
{
    int Size;
    int pos;
}a[maxn];
vector<int> son[maxn];
bool used[maxn],col[maxn];
int dep[maxn];
void dfs(int pr,int x)
{
    a[x].Size=1;
    dep[x]=dep[pr]+1;
    used[x]=1;
    for(int i=0;i<son[x].size();i++)
    {    
        int nxt=son[x][i];
        if(!used[nxt]) 
        {
            dfs(x,nxt);
            a[x].Size+=a[nxt].Size;
        }
    }
}
bool cmp(struct node a,struct node b)
{
    return a.Size>b.Size;
}
int main()
{
    int n,k;
    cin>>n>>k;
    for(int i=1;i<n;i++)
    {
        int u,v;
        scanf("%d %d",&u,&v);
        son[u].push_back(v);
        son[v].push_back(u);
    }
    dep[0]=0;
    dfs(0,1);
    for(int i=1;i<=n;i++) a[i].pos=i,a[i].Size=dep[i]-a[i].Size;
    sort(a+1,a+n+1,cmp);
    ll ans=0;
    for(int i=1;i<=k;i++) ans+=a[i].Size;
    cout<<ans<<endl;
    return 0;
}

 

上一篇:Python连接巴法云,通过mqtt协议,和tcp协议


下一篇:Educational Codeforces Round 84 (Rated for Div. 2) B. Princesses and Princes(模拟)