【LG4103】[HEOI2014]大工程

【LG4103】[HEOI2014]大工程

题面

洛谷

题解

先建虚树,下面所有讨论均是在虚树上的。

对于第一问:直接统计所有树边对答案的贡献即可。

对于第\(2,3\)问:记\(f[x]\)表示在\(x\)的子树内离\(x\)距离最远的关键点的距离,\(g[x]\)表示在\(x\)的子树内离\(x\)距离最近的关键点的距离。

具体更新以\(f[x]\)为例:

访问到\(v\in son_x\),

如果以前访问过的点中有关键点,则有\(f[x]=max(f[x],f[v]+dis(u,v)+f[x])\),

每次还要向上传递,即\(f[x]=max(f[x],f[v]+dis(u,v))\)。

代码

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
inline int gi() {
register int data = 0, w = 1;
register char ch = 0;
while (!isdigit(ch) && ch != '-') ch = getchar();
if (ch == '-') w = -1, ch = getchar();
while (isdigit(ch)) data = 10 * data + ch - '0', ch = getchar();
return w * data;
}
const int MAX_N = 1e6 + 5;
struct Graph { int to, next; } e[MAX_N << 2];
int fir1[MAX_N], fir2[MAX_N], e_cnt;
void clearGraph() {
memset(fir1, -1, sizeof(fir1));
memset(fir2, -1, sizeof(fir2));
}
void Add_Edge(int *fir, int u, int v) {
e[e_cnt] = (Graph){v, fir[u]};
fir[u] = e_cnt++;
}
namespace Tree {
int fa[MAX_N], dep[MAX_N], size[MAX_N], top[MAX_N], son[MAX_N], dfn[MAX_N], tim;
void dfs1(int x) {
dfn[x] = ++tim;
size[x] = 1, dep[x] = dep[fa[x]] + 1;
for (int i = fir1[x]; ~i; i = e[i].next) {
int v = e[i].to; if (v == fa[x]) continue;
fa[v] = x; dfs1(v); size[x] += size[v];
if (size[v] > size[son[x]]) son[x] = v;
}
}
void dfs2(int x, int tp) {
top[x] = tp;
if (son[x]) dfs2(son[x], tp);
for (int i = fir1[x]; ~i; i = e[i].next) {
int v = e[i].to; if (v == fa[x] || v == son[x]) continue;
dfs2(v, v);
}
}
int LCA(int x, int y) {
while (top[x] != top[y]) {
if (dep[top[x]] < dep[top[y]]) swap(x, y);
x = fa[top[x]];
}
return dep[x] < dep[y] ? x : y;
}
}
using Tree::LCA; using Tree::dfn; using Tree::dep;
int N, M, K, a[MAX_N];
bool key[MAX_N];
int f[MAX_N], g[MAX_N], s[MAX_N];
bool cmp(int i, int j) { return dfn[i] < dfn[j]; }
void build() {
static int stk[MAX_N], top;
sort(&a[1], &a[K + 1], cmp);
stk[top = 1] = 1; fir2[1] = -1;
e_cnt = 0;
for (int i = 1; i <= K; i++) {
key[a[i]] = 1;
if (a[i] == 1) continue;
int lca = LCA(stk[top], a[i]);
if (lca != stk[top]) {
while (dfn[lca] < dfn[stk[top - 1]]) {
int u = stk[top], v = stk[top - 1];
Add_Edge(fir2, u, v), Add_Edge(fir2, v, u);
--top;
}
if (dfn[lca] > dfn[stk[top - 1]]) {
fir2[lca] = -1; int u = stk[top], v = lca;
Add_Edge(fir2, u, v), Add_Edge(fir2, v, u);
stk[top] = lca;
}
else {
int u = lca, v = stk[top--];
Add_Edge(fir2, u, v), Add_Edge(fir2, v, u);
}
}
fir2[a[i]] = -1, stk[++top] = a[i];
}
for (int i = 1; i < top; i++) {
int u = stk[i], v = stk[i + 1];
Add_Edge(fir2, u, v), Add_Edge(fir2, v, u);
}
}
long long ans1;
int ans2, ans3;
void Dp(int x, int fa) {
s[x] = key[x], f[x] = 0, g[x] = (key[x] ? 0 : 1e9);
for (int i = fir2[x]; ~i; i = e[i].next) {
int v = e[i].to; if (v == fa) continue;
Dp(v, x);
}
for (int i = fir2[x]; ~i; i = e[i].next) {
int v = e[i].to, w = dep[v] - dep[x];
if (v == fa) continue;
ans1 += 1ll * (K - s[v]) * s[v] * w;
if (s[x] > 0) {
ans2 = min(ans2, g[x] + w + g[v]);
ans3 = max(ans3, f[x] + w + f[v]);
}
g[x] = min(g[x], g[v] + w);
f[x] = max(f[x], f[v] + w);
s[x] += s[v];
}
key[x] = 0;
}
int main () {
#ifndef ONLINE_JUDGE
freopen("cpp.in", "r", stdin);
#endif
clearGraph();
N = gi();
for (int i = 1; i < N; i++) {
int u = gi(), v = gi();
Add_Edge(fir1, u, v), Add_Edge(fir1, v, u);
}
Tree::dfs1(1), Tree::dfs2(1, 1);
M = gi();
while (M--) {
ans1 = 0, ans2 = 1e9, ans3 = 0;
K = gi(); for (int i = 1; i <= K; i++) a[i] = gi();
build();
Dp(1, 0);
printf("%lld %d %d\n", ans1, ans2, ans3);
}
return 0;
}
上一篇:【转】IOS中的release和nil


下一篇:【BZOJ】【3238】【AHOI2013】diff(差异)