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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章