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 nn districts numbered from 11 to nn, each district has exactly one path connecting it to every other district. The number of fans of a contestant from district ii is equal to 2i2i.

This year, the president decided to reduce the costs. He wants to remove kk 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 nn and kk (1≤k<n≤1061≤k<n≤106) — the number of districts in Panel, and the number of contestants the president wishes to remove, respectively.

The next n−1n−1 lines each contains two integers aa and bb (1≤a,b≤n1≤a,b≤n, a≠ba≠b), that describe a road that connects two different districts aa and bb in the nation. It is guaranteed that there is exactly one path between every two districts.

Output

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

输入:

6 3
2 1
2 6
4 2
5 6
2 3

输出:

1 3 4

输入:

8 4
2 6
2 7
7 8
1 2
3 1
2 4
7 5

输出:

1 3 4 5

题意:给你n个点,n<=100w,每个点有点权,点权为2^{i},再给你一个整数k,k<=1e6,表示你要删除k个点后使得这棵树仍联通且剩余点的权值和最大,输出所有删除的点。

思路:尽可能找大点。

 

代码:

#include <iostream>
#include <stdio.h>
#include <map>
#include <set>
#include <queue>
#include <vector>
#include <string.h>
#include <algorithm>
using namespace std;
typedef long long ll;
const int N=1e6+10;

vector<int>ve[N];
int f[N],fa[N][21];
int vis[N];
void dfs(int u,int faa)
{
    f[u]=faa;
    fa[u][0]=faa;
    for(int i=1;i<=20;i++)
    {//u节点往上跳2的i次方步数所达到的点,超过的都为n,思考一下。
        fa[u][i]=fa[fa[u][i-1]][i-1];
    }
    int sz=ve[u].size();
    for(int i=0;i<sz;i++)
    {
        int v=ve[u][i];
        if(v==faa)
            continue;
        dfs(v,u);
    }
    return;
}

int main()
{
    int n,k;
    scanf("%d%d",&n,&k);
    int res=n-k;
    for(int i=1;i<n;i++)
    {
        int u,v;
        scanf("%d%d",&u,&v);
        ve[u].push_back(v);
        ve[v].push_back(u);
    }
    dfs(n,n);
    res--;
    vis[n]=1;
    for(int i=n-1;i>=1;i--)
    {
        if(vis[i])
            continue;
        int len=1;
        int v=i;
        for(int j=20;j>=0;j--)
        {
            if(!vis[fa[v][j]])
            {
                v=fa[v][j];
                len+=(1<<j);
            }
        }
        if(len<=res)
        {
            res-=len;
            int ff=i;
            while(1)
            {
                if(vis[ff])
                    break;
                vis[ff]=1;
                ff=f[ff];//找上一个点
            }
        }
    }
    for(int i=1;i<=n;i++)
    {
        if(!vis[i])
            printf("%d ",i);
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章