CF2152H2 Victorious Coloring (Hard Version)
EuphoricStar · · 题解
2025.10.13 upd:原来的代码复杂度疑似有问题,现已修正。
翻译官方题解。
首先红点形成一个连通块,然后观察最优解中选出的连通块有什么性质。
不妨令边权互不相同。发现对于任意
称满足上述条件的连通块是”好的“。发现好的连通块最多只有
同时好的连通块也可以方便地描述。按边权从大到小建 Kruskal 重构树,那么每棵子树的叶子集合就代表了一个好的连通块。定义一个点的点权为,恰好有一端在它代表的连通块中的边的边权和。每个点的点权也是容易算的:对每条边
那么现在问题变成:有一棵
这个问题容易自底向上贪心解决。设
- 若
u 是叶子,则f_u = \max(0, l - a_u) ; - 若
u 不是叶子,设v, w 为其两个儿子,则f_u = \max(f_v + f_w, l - a_u) 。
答案即为
:::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 是容易的。设
那么考虑直接 slope trick,每个点开一个堆维护所有斜率的断点,那么凸函数相加相当于两个堆合并,对
注意交点横坐标不是整数的情况,以及为了保证每次只往堆里加
回答询问就二分得到它所在的那段直线即可。
使用可并堆,时间复杂度
:::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;
}
:::