CF2152H2 Victorious Coloring (Hard Version)

· · 题解

2025.10.13 upd:原来的代码复杂度疑似有问题,现已修正。

翻译官方题解。

首先红点形成一个连通块,然后观察最优解中选出的连通块有什么性质。

不妨令边权互不相同。发现对于任意 e_1 使得其两端都是红点,和任意 e_2 使得其恰好有一端是红点,都满足 w(e_1) > w(e_2)。证明考虑出现了一对 w(e_1) \le w(e_2),那么可以把 e_1 断开,删除 e_2 连接的那个连通块,那么花费至少减少 w(e_2) - w(e_1) \ge 0。

称满足上述条件的连通块是”好的“。发现好的连通块最多只有 2n - 1 个,因为每个单点是好的,除此之外每个好的连通块可以由其边权最小的边唯一确定。

同时好的连通块也可以方便地描述。按边权从大到小建 Kruskal 重构树,那么每棵子树的叶子集合就代表了一个好的连通块。定义一个点的点权为,恰好有一端在它代表的连通块中的边的边权和。每个点的点权也是容易算的:对每条边 (u, v, d) 进行重构树上 u \to v 除 LCA 之外的点的点权加 d 即可。

那么现在问题变成:有一棵 2n - 1 个点的二叉树,给每个叶子赋一个权值 x_i,使得对于每个点 u,都有 a_u + \sum\limits_{i \in S} x_i \ge l,其中 S 为 u 子树内的叶子集合。

这个问题容易自底向上贪心解决。设 f_u 表示只考虑 u 子树内的限制,u 子树内叶子的 x_i 之和的最小值。那么:

答案即为 f_{rt}。直接做时间复杂度 O(n \log n + nq),可以通过 Easy Version。

:::info[Easy Version 代码]

#include <bits/stdc++.h>
#define pb emplace_back
#define fst first
#define scd second
#define mkp make_pair
#define mems(a, x) memset((a), (x), sizeof(a))

using namespace std;
typedef long long ll;
typedef double db;
typedef unsigned long long ull;
typedef long double ldb;
typedef pair<ll, ll> pii;

const int maxn = 500100;

ll n, m, fa[maxn], ls[maxn], rs[maxn], a[maxn], f[maxn];
vector<pii> G[maxn];

struct edge {
    ll u, v, d;
} e[maxn];

int find(int x) {
    return fa[x] == x ? x : fa[x] = find(fa[x]);
}

void solve() {
    scanf("%lld", &n);
    for (int i = 1; i <= n * 2; ++i) {
        fa[i] = i;
        ls[i] = rs[i] = a[i] = 0;
    }
    for (int i = 1; i < n; ++i) {
        scanf("%lld%lld%lld", &e[i].u, &e[i].v, &e[i].d);
    }
    scanf("%lld", &m);
    sort(e + 1, e + n, [&](const edge &a, const edge &b) {
        return a.d > b.d;
    });
    int nt = n;
    for (int i = 1; i < n; ++i) {
        int x = find(e[i].u), y = find(e[i].v);
        if (x != y) {
            int z = ++nt;
            fa[x] = fa[y] = z;
            ls[z] = x;
            rs[z] = y;
            a[e[i].u] += e[i].d;
            a[e[i].v] += e[i].d;
            a[z] -= e[i].d * 2;
        }
    }
    for (int i = n + 1; i <= nt; ++i) {
        a[i] += a[ls[i]] + a[rs[i]];
    }
    while (m--) {
        ll l;
        scanf("%lld", &l);
        for (int i = 1; i <= nt; ++i) {
            if (i <= n) {
                f[i] = max(0LL, l - a[i]);
            } else {
                f[i] = max(f[ls[i]] + f[rs[i]], l - a[i]);
            }
        }
        printf("%lld\n", f[nt]);
    }
}

int main() {
    int T = 1;
    scanf("%d", &T);
    while (T--) {
        solve();
    }
    return 0;
}

:::

这个做法拓展到 Hard Version 是容易的。设 f_u(x) 为当 l = x 时 f_u 的值,那么显然 f_u(x) 是凸的。因为在叶子处显然是凸的,并且两个凸函数相加或取 \max 仍然是凸的。

那么考虑直接 slope trick,每个点开一个堆维护所有斜率的断点,那么凸函数相加相当于两个堆合并,对 y = x - a_u 取 \max 就算出这条直线和凸包的交点,只会 pop 掉最左边的一些断点。

注意交点横坐标不是整数的情况,以及为了保证每次只往堆里加 O(1) 个元素需要维护每个断点的出现次数,否则复杂度可能有问题。细节可以见代码。

回答询问就二分得到它所在的那段直线即可。

使用可并堆,时间复杂度 O((n + q) \log n)。

:::info[Hard Version 代码]

// Problem: H2. Victorious Coloring (Hard Version)
// Contest: Codeforces - Squarepoint Challenge (Codeforces Round 1055, Div. 1 + Div. 2)
// URL: https://codeforces.com/contest/2152/problem/H2
// Memory Limit: 1024 MB
// Time Limit: 3000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
#include <ext/pb_ds/priority_queue.hpp>
#define pb emplace_back
#define fst first
#define scd second
#define mkp make_pair
#define mems(a, x) memset((a), (x), sizeof(a))

using namespace std;
typedef long long ll;
typedef __int128 lll;
typedef double db;
typedef unsigned long long ull;
typedef long double ldb;
typedef pair<ll, ll> pii;

const int maxn = 500100;

ll n, m, fa[maxn], ls[maxn], rs[maxn], a[maxn], b[maxn], c[maxn];
vector<pii> G[maxn];

struct edge {
    ll u, v, d;
} e[maxn];

int find(int x) {
    return fa[x] == x ? x : fa[x] = find(fa[x]);
}

__gnu_pbds::priority_queue< pii, greater<pii> > q[maxn];

void solve() {
    scanf("%lld", &n);
    for (int i = 1; i <= n * 2; ++i) {
        fa[i] = i;
        ls[i] = rs[i] = a[i] = 0;
        q[i].clear();
    }
    for (int i = 1; i < n; ++i) {
        scanf("%lld%lld%lld", &e[i].u, &e[i].v, &e[i].d);
    }
    scanf("%lld", &m);
    sort(e + 1, e + n, [&](const edge &a, const edge &b) {
        return a.d > b.d;
    });
    int nt = n;
    for (int i = 1; i < n; ++i) {
        int x = find(e[i].u), y = find(e[i].v);
        if (x != y) {
            int z = ++nt;
            fa[x] = fa[y] = z;
            ls[z] = x;
            rs[z] = y;
            a[e[i].u] += e[i].d;
            a[e[i].v] += e[i].d;
            a[z] -= e[i].d * 2;
        }
    }
    for (int i = n + 1; i <= nt; ++i) {
        a[i] += a[ls[i]] + a[rs[i]];
    }
    for (int u = 1; u <= nt; ++u) {
        if (u <= n) {
            q[u].push(pii(a[u], 1));
        } else {
            q[u].swap(q[ls[u]]);
            q[u].join(q[rs[u]]);
            if (q[u].top().fst <= a[u]) {
                continue;
            }
            ll k = 1, v = 0;
            while (q[u].size()) {
                ll x = q[u].top().fst, t = q[u].top().scd;
                q[u].pop();
                ll y = (q[u].size() ? q[u].top().fst : 1e18);
                k += t - 1;
                if (v + (lll)k * (y - x) >= y - a[u]) {
                    lll xx = (lll)k * x - a[u] - v, yy = k - 1;
                    if (xx % yy == 0) {
                        q[u].push(pii(xx / yy, k - 1));
                        q[u].push(pii(a[u], 1));
                    } else {
                        ll z = xx / yy;
                        ll v1 = z - a[u], v2 = v + k * (z + 1 - x);
                        q[u].push(pii(z + 1, k - v2 + v1));
                        q[u].push(pii(z, v2 - v1 - 1));
                        q[u].push(pii(a[u], 1));
                    }
                    break;
                }
                v += k * (y - x);
                ++k;
            }
        }
    }
    int tot = 0;
    while (q[nt].size()) {
        ll x = q[nt].top().fst, y = q[nt].top().scd;
        for (int _ = 0; _ < y; ++_) {
            b[++tot] = x;
        }
        q[nt].pop();
    }
    for (int i = 1; i <= tot; ++i) {
        c[i] = c[i - 1] + (b[i] - b[i - 1]) * (i - 1);
    }
    while (m--) {
        ll x;
        scanf("%lld", &x);
        int p = upper_bound(b + 1, b + tot + 1, x) - b - 1;
        printf("%lld\n", c[p] + (x - b[p]) * p);
    }
}

int main() {
    int T = 1;
    scanf("%d", &T);
    while (T--) {
        solve();
    }
    return 0;
}

:::