• A. 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 nn cities and n1n−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 (2n21052≤n≤2⋅105, 1k<n1≤k<n)  — the number of cities and industry cities respectively.

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

    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个就可以了。

    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <string>
    #include <set>
    #include <queue>
    #include <map>
    #include <sstream>
    #include <cstdio>
    #include <cstring>
    #include <numeric>
    #include <cmath>
    #include <iomanip>
    #include <deque>
    #include <bitset>
    //#include <unordered_set>
    //#include <unordered_map>
    //#include <bits/stdc++.h>
    //#include <xfunctional>
    #define ll              long long
    #define PII             pair<int, int>
    #define rep(i,a,b)      for(int  i=a;i<=b;i++)
    #define dec(i,a,b)      for(int  i=a;i>=b;i--)
    #define pb              push_back
    #define mk              make_pair
    using namespace std;
    int dir[4][2] = { { 0,1 } ,{ 0,-1 },{ 1,0 },{ -1,0 } };
    const long long INF = 0x7f7f7f7f7f7f7f7f;
    const int inf = 0x3f3f3f3f;
    const double pi = 3.14159265358979;
    const int mod = 998244353;
    const int N = 2e5+5;
    //if(x<0 || x>=r || y<0 || y>=c)
    
    inline ll read()
    {
        ll x = 0; bool f = true; char c = getchar();
        while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); }
        while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
        return f ? x : -x;
    }
    
    ll gcd(ll m, ll n)
    {
        return n == 0 ? m : gcd(n, m%n);
    }
    struct node
    {
        int steps = 0,sub = 0;
        vector<int> sa;
    }a[N];
    bool cmp(node a, node b)
    {
        return a.steps-a.sub > b.steps-b.sub;
    }
    vector<bool> vis(N);
    int dfs(int x)
    {
        vis[x] = 1;
        for (int i = 0; i < a[x].sa.size(); i++)
        {
            if (!vis[a[x].sa[i]])
            {
                a[x].sub++;
                a[a[x].sa[i]].steps = a[x].steps + 1;
                a[x].sub+=dfs(a[x].sa[i]);
            }
        }
        return a[x].sub;
    }
    int main()
    {
        int n, k;
        cin >> n >> k;
        for (int i = 1; i <= n - 1; i++)
        {
            int s, t;
            cin >> s >> t;
            a[s].sa.push_back(t);
            a[t].sa.push_back(s);
        }
        dfs(1);
        sort(a + 1, a + n + 1, cmp);
        ll res = 0;
        rep(i, 1, k)
        {
            res += a[i].steps-a[i].sub;
        }
        cout << res << endl;
        return 0;
    }
  • 相关阅读:
    HTTP协议中几个状态码
    Memcache 使用方法
    mysql 时间设置
    Adobe After Effect CS6 下载 安装 破解 汉化
    skycons.js 基于canvas的天气动态图标小插件
    Adobe Creative Cloud Cleaner Tool 清除工具
    nodejs常用命令行
    js过滤特殊字符
    QuickTime 介绍 下载 安装(win7及以上版本)
    js 16进制转10进制
  • 原文地址:https://www.cnblogs.com/dealer/p/12871499.html
Copyright © 2020-2023  润新知