bzoj - 3252 攻略 【思维 or 可并堆】 好题

传送门
题目: 就是同时有k个人从一颗有n个节点的树的根节点出发, 走过的点都可以累加到ans中, 一个点只能被累加一次, 问最大能累加多少.

思路: 其实最开始做这道题是用的倒着做的, 然后dp了一下, 现在学了这个就用这个再做做, 实际上我们从叶子节点开始每次每个节点只加在价值最大的那个子节点上, 然后加完了,从根节点开始一个个删掉, 取前k大即可.

AC Code

const int maxn = 2e5 + 5;
int root[maxn];
struct Ltree {
    int ls[maxn], rs[maxn];
    ll val[maxn];
    int Un(int x, int y){
        if (!x || !y) return x+y;
        if (val[x] < val[y]) swap(x,y);
        rs[x] = Un(rs[x], y);
        swap(ls[x], rs[x]);
        return x;
    }
    void pop(int &x) {
        x = Un(ls[x], rs[x]);
    }
    int top(int x) {
        return val[x];
    }
}heap;
ll a[maxn];
int n, m;
struct node {
    int to, next, w;
}e[maxn<<1];
int cnt, head[maxn];
void init() {
    cnt = 0; Fill(head, -1);
}
void add(int u, int v, int w) {
    e[cnt] = node{v, head[u], w};
    head[u] = cnt++;
}
void dfs(int u, int fa) {
    for (int i = head[u] ; ~i ; i = e[i].next) {
        int to = e[i].to;
        if (to == fa) continue;
        dfs(to, u);
        root[u] = heap.Un(root[u], root[to]);
    }
    heap.val[root[u]] += a[u];
}
void solve() {
    scanf("%d%d", &n, &m); init();
    for (int i = 1 ; i <= n ; i ++) {
        scanf("%lld", a+i);
        root[i] = i;
    }
    for (int i = 2 ; i <= n ; i ++) {
        int u, v; scanf("%d%d", &u, &v);
        add(u, v, 1); add(v, u, 1);
    }
    dfs(1, -1);
    ll ans = 0;
    while(m--) {
        ans += heap.val[root[1]];
        heap.pop(root[1]);
        if (!root[1]) break;
    }
    printf("%lld\n", ans);
}

顺便附上我第一次做的代码(倒着思考)

struct node {
    int to, next;
}e[maxn];
int cnt, head[maxn], cas = 1;
int n, k;
void add(int u, int v) {
    e[cnt] = node{v, head[u]};
    head[u] = cnt++;
}
int in[maxn], vis[maxn];
ll val[maxn], dp[maxn];
struct result {
    ll road; int id;
    bool operator < (const result& _) const {
        return road > _.road;
    }
}b[maxn];
void init() {
    cnt = 0; Fill(head, -1); Fill(dp, -1);
    Fill(in, 0); Fill(vis, 0); Fill(b, 0);
}
ll dfs1(int u) {
    if (dp[u] != -1) return dp[u];
    ll sum = val[u];
    for (int i = head[u] ; ~i ; i = e[i].next) {
        int to = e[i].to;
        sum += dfs1(to);
    }
    return dp[u] = sum;
}
ll sum;
void dfs2(int u) {
    if (vis[u]) return ;
    vis[u] = 1;
    sum += val[u];
    for (int i = head[u] ; ~i ; i = e[i].next) {
        int to = e[i].to;
        dfs2(to);
    }
}
void solve()
{
    scanf("%d%d", &n, &k);
    for (int i = 1 ; i <= n ; i ++) {
        scanf("%d", val+i);
    }
    init();
    for (int i = 1 ; i <= n-1 ; i ++) {
        int u, v;
        scanf("%d%d", &u, &v);
        add(v, u);
        in[u]++;
    }
    int idx = 0;
    for (int i = 1 ; i <= n ; i ++) {
        if (!in[i]) {
            b[++idx].id = i;
            b[idx].road = dfs1(i);
        }
    }
    sort(b+1, b+1+idx);
    for (int i = 1 ; i <= idx ; i ++) {
        sum = 0; dfs2(b[i].id);
        b[i].road = sum;
    }
    sort(b+1, b+1+idx);
    ll ans = 0;
    for (int i = 1 ; i <= min(idx, k) ; i ++) {
        ans += b[i].road;
    }
    printf("Case #%d: %lld\n", cas++, ans);
}

你可能感兴趣的:(可并堆/左偏树)