「KTSC 2025 R2」编号游戏
Description
给定一张 可能有重边 的图,要求给每个点分配一个编号 ,使得任意一条路径上的点编号单调不 增/减。
请你求出 。
Analyze
很自然的题,完全不用什么人类智慧。不建议思考时间低于 分钟就看题解。
最大化不同可以变成最小化相同,记 ,即 。
显然每个边双中的点编号一定相同,边双缩点之后形成了一颗树。
把相同编号的点缩起来之后一定是一条链,考虑证明:
若不是一条链,则存在一个节点度数大于等于 。故从这个点出发的链单调性相同的至少有 条,与条件矛盾。
对这颗树进行树形 dp 求出 子树内以 为链一端的最小权值 。转移是 。
对每个点为根做一遍就可以得到 做法。
考虑优化,我们设 表示 子树外以 为链一端的最小代价。
对于 的儿子 , 的转移式是 。
后面这个东西拆开来是 的形式。将儿子按 排序后跑单调队列即可,我写了李超树。
则 。
Code
#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long uLL;
#define rep(i,l,r) for (int i = l; i <= r; i ++)
#define rrp(i,l,r) for (int i = l; i >= r; i --)
const int N = 1e6 + 5;
typedef long long LL;
inline LL p2(LL x) { return x * x; }
int n, m, cnt[N];
vector<int> a[N];
namespace prework {
vector<pair<int, int>> a[N], e;
int n, m, tim, dfn[N], low[N], col[N], st[N], tp;
void dfs(int x, int las) {
dfn[x] = low[x] = ++ tim, st[++ tp] = x;
for (auto [y, i] : a[x]) if (i ^ las) {
if (!dfn[y]) dfs(y, i), low[x] = min(low[x], low[y]);
else low[x] = min(low[x], dfn[y]);
}
if (dfn[x] == low[x]) {
++ ::n;
for (int y = -1; y != x; )
y = st[tp --], col[y] = ::n, ++ cnt[::n];
}
}
void main(int n, int m, vector<int> U, vector<int> V) {
::m = n;
rep (i, 1, m) {
int u = U[i-1] + 1, v = V[i-1] + 1;
a[u].emplace_back(v, i);
a[v].emplace_back(u, i);
e.emplace_back(u, v);
}
dfs(1, -1);
for (auto [u, v] : e) {
u = col[u], v = col[v];
if (u != v) { ::a[u].push_back(v), ::a[v].push_back(u); }
}
}
}
int sz[N]; LL f[N], g[N], val[N], ans;
void getsz(int x, int fa) {
sz[x] = cnt[x];
for (auto y : a[x]) if (y != fa) getsz(y, x), sz[x] += sz[y];
}
void dfs1(int x, int fa) {
f[x] = p2(sz[x]);
for (auto y : a[x]) if (y != fa) {
dfs1(y, x), f[x] = min(f[x], f[y] + p2(sz[x] - sz[y]));
}
ans = min(ans, f[x] + p2(m - sz[x]));
}
struct line {
LL k, b;
line(LL _k = 0, LL _b = 1e18) : k(_k), b(_b) {}
LL operator() (LL x) { return k * x + b; }
} seg[N];
struct Sgt {
int t[N << 2], segcnt;
bool check (int p, int q, int x) {
return seg[p](x) < seg[q](x);
}
void upd (int p, int c, int l, int r) {
auto &q = t[c];
int mid = l + r >> 1;
if (check(p, q, mid)) swap(p, q);
if (l == r) return ;
if (check(p, t[c << 1], l)) upd (p, c << 1, l, mid);
if (check(p, t[c << 1 | 1], r)) upd (p, c << 1 | 1, mid + 1, r);
}
void ins(LL k, LL b) {
seg[++ segcnt] = {k, b};
upd(segcnt, 1, 1, m);
}
void clear() {
rep (i, 0, segcnt) seg[i] = line();
segcnt = 0;
}
LL qry (int p, int c, int l, int r) {
LL val = seg[t[c]](p);
if (l == r) return val;
int mid = l + r >> 1;
if (p <= mid) return min(qry(p, c << 1, l, mid), val);
return min(qry(p, c << 1 | 1, mid + 1, r), val);
}
} T;
void dfs2(int x, int fa) {
ans = min(ans, g[x] + p2(sz[x]));
vector<int> son;
for (auto y : a[x]) if (y != fa) son.push_back(y);
for (auto y : son) val[y] = f[y] - 2LL * m * sz[y] + p2(sz[y]);
T.clear();
for (auto y : son) g[y] = T.qry(sz[y], 1, 1, m), T.ins(2 * sz[y], val[y]);
T.clear(), reverse(son.begin(), son.end());
for (auto y : son) g[y] = min(g[y], T.qry(sz[y], 1, 1, m)), T.ins(2 * sz[y], val[y]);
for (auto y : son) g[y] = min(g[y] + p2(m - sz[y]), g[x] + p2(sz[x] - sz[y])), dfs2(y, x);
}
void brute(int x, int fa) {
ans = min(ans, g[x] + p2(sz[x]));
vector<int> son;
for (auto y : a[x]) if (y != fa) son.push_back(y);
for (auto y : son) val[y] = f[y] - 2LL * m * sz[y] + p2(sz[y]);
for (auto y : son) {
g[y] = g[x] + p2(sz[x] - sz[y]);
for (auto z : son) if (y ^ z)
g[y] = min(g[y], val[z] + 2LL * sz[y] * sz[z] + p2(m - sz[y]));
brute(y, x);
}
}
long long max_diversity(int N, int M, vector<int> U, vector<int> V) {
prework::main(N, M, U, V);
ans = p2(m), getsz(1, 0), dfs1(1, 0), dfs2(1, 0);
return (p2(m) - ans) / 2;
}