E. The Number Games 倍增

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 (1≤k<n≤106
) — the number of districts in Panel, and the number of contestants the president wishes to remove, respectively.

The next n−1

lines each contains two integers a and b (1≤a,b≤n, a≠b), 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

因为每个数字各不相同,所以如果能选择大的,绝不会选择小的,因为下面的全都加起来,也不如一个高位,所以就是怎样来选择的问题了,nlogn的复杂度,从大到小遍历,如果可以选,我们一定选并且把路径上的点,全都选上,这样才可以,所以用最近公共祖先来做,其实就是记忆化,每次直接查找就可以了

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>

using namespace std;

const int maxn=1e6+10;

vector<int> e[maxn];
int in[maxn],p[maxn][21];

void dfs(int u,int fa)
{
    int len=e[u].size();
    p[u][0]=fa;
    for(int i=1;i<=20;i++)
    {
        p[u][i]=p[p[u][i-1]][i-1];
    }
    for(int i=0;i<len;i++)
    {
        int v=e[u][i];
        if(v!=fa)
        {
            dfs(v,u);
        }
    }
}

int cal(int u,int k)
{
    for(int i=20;i>=0,k>0;i--)
    {
        if(k>=(1<<i))
        {
            k-=(1<<i);
            u=p[u][i];
        }
    }
    return u;
}

int ans[maxn];

int main() {

    int n,k;
    while(~scanf("%d %d",&n,&k))
    {
        memset(p,0,sizeof(p));
        memset(in,0,sizeof(in));
        for(int i=0;i<=1000000;i++)
            e[i].clear();

        k=n-k;
        for(int i=1;i<n;i++){
            int u,v;
            scanf("%d %d",&u,&v);
            e[u].push_back(v),e[v].push_back(u);
        }

        dfs(n,0);

        //cout<<"~"<<endl;
        in[0]=1;

        for(int i=n;i>=1&&k>0;i--)
        {
            if(in[i]==1)
                continue;

            int rt=cal(i,k);
            if(in[rt])
            {
                int sta=i;
                while(in[sta]==0)
                {
                    in[sta]=1;
                    k--;
                    sta=p[sta][0];
                }
            }
        }
        int cnt=0;
        for(int i=1;i<=n;i++)
        {
            if(in[i]==0)
                ans[cnt++]=i;
        }
        for(int i=0;i<cnt;i++)
            printf("%d ",ans[i]);
        printf("\n");

    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值