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
.
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?
The first line of input contains two integers n
) — 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 bin the nation. It is guaranteed that there is exactly one path between every two districts.
Print k
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
In the first sample, the maximum possible total number of fans is 22+25+26=100
#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]);
}