zoukankan      html  css  js  c++  java
  • Linova and Kingdom CodeForces 1337C [dfs and similar greedy sortings trees]

    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
    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 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.

    #include <bits/stdc++.h>
    using namespace std;
    const int inf = 1e8;
    const int mod = 1000000007;
    const int mx = 200010; //check the limits, dummy
    typedef long long ll;
    typedef pair<int, int> pa;
    const double PI = acos(-1);
    ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
    #define swa(a,b) a^=b^=a^=b
    #define re(i,a,b) for(int i=(a),_=(b);i<_;i++)
    #define rb(i,a,b) for(int i=(b),_=(a);i>=_;i--)
    #define clr(a) memset(a, 0, sizeof(a))
    #define lowbit(x) ((x)&(x-1))
    #define mkp make_pair
    void sc(int& x) { scanf("%d", &x); }void sc(int64_t& x) { scanf("%lld", &x); }void sc(double& x) { scanf("%lf", &x); }void sc(char& x) { scanf(" %c", &x); }void sc(char* x) { scanf("%s", x); }
    ll  m, n,t,x,y,z,k,sum=0,ans=0;
    int a, b, c, d[mx],sm[mx];
    vector<int> G[mx];
    vector<pair<int, int>>vec;
    void dfs(int v, int f = 0) {
        for (auto u : G[v]) {
            if (u == f)continue;
            d[u] = d[v] + 1;
            dfs(u, v);
            sm[v] += sm[u];
        }
        vec.push_back({ d[v] - sm[v],v });
        sm[v]++;
    }
    int main()
    {
        ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
        cin >> n >> k;
        re(i, 1, n) {
            cin >> a >> b;
            G[a].push_back(b);
            G[b].push_back(a);
        }
        dfs(1);
        sort(vec.begin(), vec.end());
        reverse(vec.begin(), vec.end());
        ans = 0;
        re(i, 0, k)ans += vec[i].first;
        cout << ans << endl;
        return 0;
    }
  • 相关阅读:
    禅道开源版本安装
    NATAPP内网穿透实现
    nginx部署前端项目
    docker-compose部署微服务
    python编写猜数字游戏
    Linux命令(用户管理、组和时间管理)
    Linux命令(文本编辑器)
    Linux的简单命令(防火墙篇)
    什么是泛型
    spring bean 的作用域之间有什么区别
  • 原文地址:https://www.cnblogs.com/xxxsans/p/12710415.html
Copyright © 2011-2022 走看看