「SDOI 2011」消耗战

Problem

Description

给定一棵 n 个点带边权的树,根节点的编号为 1
m 次询问,每次询问给出 k 个点,求使得根节点与这 k 个点中的任意点都不连通所需要的最小花费.切断一条边的花费等于其边权。

Constraints

2\le n\le 250000,1\le m,\sum_{i=1}^{m}k_{i}\le 5\times 10^{5},1\le k_{i}\le n-1

Solution

Analysis

虚树模板题。对每次询问建出虚树后在虚树上 DP 即可。
f(u) 为将以 u 号点为根的子树切断的最小代价, cost(u) u 号点到其在虚树上的父节点路径上的最小边权,则易有 DP 方程:

f(u)=\min\left(cost(u),\sum_{\left<u,v\right>\in E}f(v)\right)

时间复杂度 O\left(\sum k_{i}\right)

Code

BZOJ 上第一第二都是我的 233

时间最小,空间最小,代码最长(大雾

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include <cstdio>
#include <algorithm>

using i64 = long long;

constexpr i64 inf = 5e18;
constexpr int maxn = 250000;

template <typename _Tp>
inline _Tp Min(const _Tp &x, const _Tp &y) {
return x < y ? x : y;
}
template <typename _Tp>
inline _Tp Max(const _Tp &x, const _Tp &y) {
return x > y ? x : y;
}
template <typename _Tp>
inline void swap(_Tp &x, _Tp &y) {
_Tp z = x; x = y; y = z;
}

namespace IOManager {
constexpr int FILESZ = 131072;
char buf[FILESZ];
const char *ibuf = buf, *tbuf = buf;

struct IOManager {
inline char gc() {
return (ibuf == tbuf) and (tbuf = (ibuf = buf) + fread(buf, 1, FILESZ, stdin), ibuf == tbuf) ? EOF : *ibuf++;
}

template <typename _Tp>
inline operator _Tp() {
_Tp s = 0u; char c = gc();
for (; c < 48; c = gc());
for (; c > 47; c = gc())
s = (_Tp)(s * 10u + c - 48u);
return s;
}
};
} IOManager::IOManager io;

namespace OriginalTree {

struct Node;

struct Edge {
int w; Node *v; Edge *las;
inline Edge *init(Node *const &to, const int &val, Edge *const &ls) {
return v = to, w = val, las = ls, this;
}
};

struct Node {
int dep, sz, s, t;
i64 w;
Node *top, *p, *son;
Edge *las;

void predecomp() {
static int idx = 0;
s = ++idx; sz = 1;
for (Edge *o = las; o; o = o->las)
if (o->v not_eq p) {
Node *const &v = o->v;
v->p = this;
v->dep = dep + 1;
v->w = Min(w, (i64)o->w);
v->predecomp();
sz += v->sz;
if (son == nullptr or son->sz < v->sz) son = v;
}
t = idx;
}

void decomp() {
if (top == nullptr) top = this;
if (son not_eq nullptr)
son->top = top,
son->decomp();
for (Edge *o = las; o; o = o->las)
if (o->v not_eq p and o->v not_eq son)
o->v->decomp();
}

inline void lnk(Node *const &to, const int &val) {
static Edge pool[maxn << 1], *alloc_p = pool - 1;
las = (++alloc_p)->init(to, val, las);
}
} node[maxn + 1];

inline int lca(const int &x, const int &y) {
Node *u = node + x, *v = node + y;
for (Node *tu = u->top, *tv = v->top; tu not_eq tv; u = tu->p, tu = u->top)
if (tu->dep < tv->dep) swap(u, v), swap(tu, tv);
return (u->dep < v->dep ? u : v) - node;
}

}

namespace VirtualTree {

struct Edge {
int v; Edge *las;
inline Edge *init(const int &to, Edge* const &ls) {
return v = to, las = ls, this;
}
} *las[maxn + 1];
Edge pool[maxn << 1], *alloc_p;

inline void lnk(const int &u, const int &v) {
if (u not_eq v) las[u] = (++alloc_p)->init(v, las[u]);
}

struct Pair {
int idx, dfn;
inline void init(const int &x, const int &y) {
idx = x; dfn = y;
}
inline bool operator<(const Pair &rhs) const {
return dfn < rhs.dfn;
}
};

void setup(int *const &qlis, const int &n) {
using OriginalTree::node;
alloc_p = pool - 1;

static Pair lis[maxn];
for (int i = 0; i < n; ++i)
lis[i].init(qlis[i], node[qlis[i]].s);
std::sort(lis, lis + n);

int cnt = 0;
for (int i = 1; i < n; ++i)
if (lis[i].dfn > node[lis[cnt].idx].t)
lis[++cnt] = lis[i];

static Pair stk[maxn + 1], *stp = stk;
(++stp)->init(1, 1);
for (int i = 0; i <= cnt; ++i) {
const int lca = OriginalTree::lca(lis[i].idx, stp->idx), slca = node[lca].s;
for (; (stp - 1)->dfn > slca; --stp) lnk((stp - 1)->idx, stp->idx);
lnk(lca, (stp--)->idx);
if ((stp - 1)->dfn < slca) (++stp)->init(lca, slca);
*++stp = lis[i];
}
for (; stp > stk + 1; --stp)
lnk((stp - 1)->idx, stp->idx);
}

i64 calc(const int &u) {
if (las[u] == nullptr)
return OriginalTree::node[u].w;
i64 sum = 0;
for (Edge *&o = las[u]; o; o = o->las)
sum += calc(o->v);
return Min(sum, OriginalTree::node[u].w);
}

}

int main() {
static int qlis[maxn];
using OriginalTree::node;

const int n = io;
for (int i = n - 1, u, v, w; i; --i) {
u = io; v = io; w = io;
node[u].lnk(node + v, w);
node[v].lnk(node + u, w);
}

node[1].w = inf;
node[1].predecomp();
node[1].decomp();

for (int q = io; q; --q) {
const int k = io;
for (int i = 0; i < k; ++i) qlis[i] = io;
VirtualTree::setup(qlis, k);
printf("%lld\n", VirtualTree::calc(1));
}

return 0;
}