Codeforces 980E(树上贪心倍增)

本文解析了Codeforces E题The Number Game,介绍了如何通过保留特定节点以最大化剩余节点权重的算法,并提供了完整的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接: http://codeforces.com/contest/980/problem/E
E. The Number Games
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

The nation of Panel holds an annual show called The Number Games, where each district in the nation will be represented by one contestant.

The nation has n

districts numbered from 1 to n, each district has exactly one path connecting it to every other district. The number of fans of a contestant from district i is equal to 2i

.

This year, the president decided to reduce the costs. He wants to remove k

contestants from the games. However, the districts of the removed contestants will be furious and will not allow anyone to cross through their districts.

The president wants to ensure that all remaining contestants are from districts that can be reached from one another. He also wishes to maximize the total number of fans of the participating contestants.

Which contestants should the president remove?

Input

The first line of input contains two integers n

and k (1k<n106

) — the number of districts in Panel, and the number of contestants the president wishes to remove, respectively.

The next n1

lines each contains two integers a and b (1a,bn, ab), that describe a road that connects two different districts a and b

in the nation. It is guaranteed that there is exactly one path between every two districts.

Output

Print k

space-separated integers: the numbers of the districts of which the contestants should be removed, in increasing order of district number.

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

In the first sample, the maximum possible total number of fans is 22+25+26=100

. We can achieve it by removing the contestants of the districts 1, 3, and 4.

题意:给出一颗N个节点的树,第i个节点的权值为2^i, 现在你必须删除K个节点(K < N),并且保证剩下的点都联通的情况下,使得剩下的点权值最大。

题解:我们要以N当根,N必须保留,这点很重要,然后贪心的从N-1 ~ 1这些点判断这个点i是否能保留,如果这个点能保留,则他到根N的这些点都必须被保留,这里需要倍增预处理,用个变量记录当前图还需要保留几个点,贪心即可。

#include <bits/stdc++.h>

using namespace std;

const int maxn = 1E6 + 7;
vector<int>g[maxn];
int par[maxn];
int up[20][maxn];
bool was[maxn];

void dfs(int u, int fa)
{
    par[u] = up[0][u] = fa;
    for(int i = 1;i < 20;i ++) up[i][u] = up[i-1][ up[i-1][u] ];
    for(int to:g[u]) if(to != fa) dfs(to, u);
}

int main()
{
    int n, k;
    scanf("%d %d", &n, &k);
    was[n] = 1;
    for(int u, v, i = 1;i < n;i ++) {
        scanf("%d %d",&u,&v);
        g[u].push_back(v);
        g[v].push_back(u);
    }
    dfs(n, n);
    int ost = n - 1 - k;
    for(int i = n - 1;i >= 1;i --) {
        if(was[i]) continue;
        int v = i, len = 0;
        for(int j = 19;j >= 0;j --) {
            if(!was[ up[j][v] ]) {
                v = up[j][v];
                len += 1 << j;
            }
        }
        len ++;
        if(len <= ost) {
            v = i;
            ost -= len;
            while(true) {
                if(was[v]) break;
                was[v] = 1;
                v = par[v];
            }
        }
    }
    for(int i = 1;i <= n; i++) if(!was[i]) printf("%d ",i);
    printf("\n");
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值