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]);
}

引用\[1\]中提到了一种树形动态规划的方法来解决CodeForces - 982C问题。在这个问题中,subtree指的是子连通块,而不是子树。为了使cnt_white - cnt_black尽可能大,可以使用两次树形动态规划来求解。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。如果ans\[u\]为正,则减去dp\[v\]就是树链所代表的子树的权值。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] 问题: CodeForces - 982C 树形DP是什么问题?如何解决? 回答: CodeForces - 982C是一个树形动态规划问题。在这个问题中,需要求解子连通块的最大权值和,使得cnt_white - cnt_black尽可能大。解决这个问题的方法是使用两次树形动态规划。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] #### 引用[.reference_title] - *1* *2* [CodeForces - 1324F Maximum White Subtree(树形dp)](https://blog.csdn.net/qq_45458915/article/details/104831678)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值