codeforces980E+树上贪心


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.

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,a,b) for(int i=a;i<=b;i++)
ll rd(){
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
const int N=1<<20;
const int K=20;
vector<int> v[N];
int p[K][N];
int dep[N];
void go(int now,int prt,int tdep){
    p[0][now]=prt;
    dep[now]=tdep;
    for(int son:v[now]){
        if(son==prt)continue;
        go(son,now,tdep+1);
    }
}

int in[N];

int main(){
    freopen("in.txt","r",stdin);
    int n=rd(),k=rd();
    for(int i=1;i<n;i++){
        int x=rd(),y=rd();
        v[x].push_back(y);
        v[y].push_back(x);
    }
    go(n,n,0);
    for(int j=1;j<K;j++)
    for(int i=1;i<=n;i++)
    p[j][i]=p[j-1][p[j-1][i]];
    k=n-k;
    in[n]=true;
    k--;
    for(int i=n-1;i>=1 and k>0;i--){
        if(in[i])continue;
        int cur=i;
        for(int j=K-1;j>=0;j--)
            if(not in[p[j][cur]])
            cur=p[j][cur];
        if(dep[i]-dep[cur]+1<=k){
            cur=i;
            while(not in[cur]){
                in[cur]=true;
                k--;
                cur=p[0][cur];
            }
        }
    }
    vector<int> ans;
    rep(i,1,n)
    if(not in[i])
        ans.push_back(i);
    int len=ans.size()-1,i;
    for(i=0; i<len; i++)
    printf("%d ", ans[i]);
    printf("%d\n", ans[i]);
}

猜你喜欢

转载自blog.csdn.net/ujn20161222/article/details/80286167