Linova and Kingdom【DFS】

题目描述:原题链接

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

思路:

DFS每个点的深度,注意要减去当前点的后继数,因为如果当这个点是工厂后,它的后继再走过它后就不会加开心值。最后选取较大的k个。注意因为是无向图,idx最大会加到221e5。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <vector>
using namespace std;
typedef long long ll;
const int N=400010;

int n,k;
int h[N],e[N],ne[N],idx;
int d[N],st[N],t[N];

bool cmp(int x,int y)
{
    return x>y;
}

void add(int a,int b)
{
    e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}

void dfs(int x)
{
    st[x]=1;t[x]=1;
    for(int i=h[x];i!=-1;i=ne[i])
    {
        int j=e[i];
        if(st[j]) continue;
        d[j]=d[x]+1;
        dfs(j);
        t[x]+=t[j];
    }
}

int main()
{
    scanf("%d %d",&n,&k);
    memset(h,-1,sizeof h);
    for(int i=0;i<n-1;i++)
    {
        int x,y;
        scanf("%d %d",&x,&y);
        add(x,y),add(y,x);
    }
    dfs(1);
    for(int i=1;i<=n;i++) d[i]-=t[i]-1;
    sort(d+1,d+n+1,cmp);
    ll ans=0;
    for(int i=1;i<=k;i++) ans+=d[i];
    printf("%lld",ans);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值