CF 980E(树上差分)树状数组

本文介绍了一种使用树状数组优化树形DP的方法,解决了一类在树上删除K个节点的问题,目的是使剩余节点的权值之和最大化,并确保所有剩余节点保持连通。文章提供了一个具体的代码实现案例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <bits/stdc++.h>

using namespace std;
const int maxn = 1e6 + 7;
vector<int> g[maxn];
int par[maxn];
int in[maxn], out[maxn];
bool remain[maxn];
struct BIT
{
    int n, c[maxn];
    void init(int _n) {
        n = _n;
        memset(c, 0, (n + 2) << 2);
    }
    void update(int p, int d) {
        for(int i = p;i <= n;i += i & -i) c[i] += d;
    }
    int query(int p) {
        int res = 0;
        for(int i = p;i > 0;i -= i & -i) res += c[i];
        return res;
    }
}bit;
int time_tag;
void dfs(int u, int fa, int d)
{
    par[u] = fa;
    in[u] = ++time_tag;
    bit.update(in[u], d);
    bit.update(in[u]+1, -d);
    for(int i = 0;i < g[u].size();i ++) {
        int to = g[u][i];
        if(to != fa) dfs(to, u, d + 1);
    }
    out[u] = time_tag;
}
int main()
{
    int n, k;
    scanf("%d %d", &n, &k);
    bit.init(n);
    for(int i = 1;i < n;i ++) {
        int a, b;
        scanf("%d%d",&a,&b);
        g[a].push_back(b);
        g[b].push_back(a);
    }
    dfs(n,0,1);
    for(int rem=0,i = n;i >= 1;i --) {
        if(remain[i]) continue;
        int t = bit.query(in[i]);
        if(t + rem <= n - k) {
            for(int j = i;!remain[j] && j;j = par[j]) {
                remain[j] = 1;
                rem ++;
                bit.update(in[j], -1);
                bit.update(out[j]+1,1);
            }
        }
    }
    for(int i = 1;i <= n;i ++) if(!remain[i]) printf("%d ",i);
    return 0;
}

题目链接: http://codeforces.com/contest/980/problem/E
E. The Number Games
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.

题意:给出一颗N个节点的树,第i个节点的权值为2^i, 现在你必须删除K个节点(K < N),并且保证剩下的点都联通的情况下,使得剩下的点权值最大。


题解:用树状数组,维护这个点到根的路径上有几个需要保留点。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值