【题解】BZOJ-2286-Luogu-2495-[SDOI2011]消耗战

题目

https://www.luogu.org/problemnew/show/P2495

题意

给一棵有边权的树,每次询问一些点,切除若干条边权和最小的边,使得这些点与根不连通。

题解

  • 虚树。
  • 树形 dp。\(dp[u]\) 表示将 \(u\) 与其子树中的所有关键点断开的最小代价,\(w[u]\) 表示 \(u\) 到根结点的边权的最小值。

\[ dp[u] = \sum_{v} \begin{cases} w[v] & \text{v is key node}\\ min\{w[v], dp[v]\} & \text{otherwise}& \end{cases} \]

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
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
#define FOR(i, x, y) for (decay<decltype(y)>::type i = (x), _##i = (y); i < _##i; ++i)
#define FORD(i, x, y) for (decay<decltype(x)>::type i = (x), _##i = (y); i > _##i; --i)
#ifdef zerol
#define dbg(args...) do { cout << "\033[32;1m" << #args << " -> "; err(args); } while (0)
#else
#define dbg(...)
#endif
void err() { cout << "\033[39;0m" << endl; }
template<template<typename...> class T, typename t, typename... Args>
void err(T<t> a, Args... args) { for (auto x: a) cout << x << ' '; err(args...); }
template<typename T, typename... Args>
void err(T a, Args... args) { cout << a << ' '; err(args...); }
// -----------------------------------------------------------------------------
const int N = 25E4 + 100, INF = 1E9;
struct E {
int to, w;
};
int w[N], in[N], out[N], clk, dep[N];
vector<E> G[N];
bool key[N];
int pa[N][20];
LL f[N];

void dfs(int u, int fa) {
in[u] = ++clk;
pa[u][0] = fa;
FOR (i, 1, 20) pa[u][i] = pa[pa[u][i - 1]][i - 1];
for (E& e: G[u]) {
int v = e.to;
if (v == fa) continue;
w[v] = min(e.w, w[u]);
dep[v] = dep[u] + 1;
dfs(v, u);
}
out[u] = clk;
}

int lca(int u, int v) {
if (dep[u] < dep[v]) swap(u, v);
int t = dep[u] - dep[v];
FOR (i, 0, 20) if (t & (1 << i)) u = pa[u][i];
FORD (i, 19, -1) {
int uu = pa[u][i], vv = pa[v][i];
if (uu != vv) { u = uu; v = vv; };
}
return u == v ? u : pa[u][0];
}

void go(vector<int>& V, int& k) {
int u = V[k]; f[u] = 0;
dbg(u, k);
while (k + 1 < V.size()) {
int to = V[k + 1];
if (in[to] <= out[u]) {
go(V, ++k);
if (key[to]) f[u] += w[to];
else f[u] += min(f[to], (LL) w[to]);
} else break;
}
}

inline bool cmp(int a, int b) { return in[a] < in[b]; }
LL solve(vector<int>& V) {
static vector<int> a; a.clear();
for (int& x: V) a.push_back(x);
sort(a.begin(), a.end(), cmp);
FOR (i, 1, a.size())
a.push_back(lca(a[i], a[i - 1]));
a.push_back(1);
sort(a.begin(), a.end(), cmp);
a.erase(unique(a.begin(), a.end()), a.end());
dbg(a);
int tmp; go(a, tmp = 0);
return f[1];
}

int main() {
#ifdef zerol
freopen("in", "r", stdin);
#endif
int n; cin >> n;
FOR (i, 1, n) {
int u, v, w; scanf("%d%d%d", &u, &v, &w);
G[u].push_back({v, w}); G[v].push_back({u, w});
}
w[1] = INF;
dfs(1, 1);
int Qn; cin >> Qn;
while (Qn--) {
int k; scanf("%d", &k);
static vector<int> V; V.clear();
FOR (_, 0, k) {
int t; scanf("%d", &t);
V.push_back(t);
key[t] = true;
}
printf("%lld\n", solve(V));
for (int& x: V) key[x] = false;
}
}