yaoxi-std 的博客

$\text{开}\mathop{\text{卷}}\limits^{ju\check{a}n}\text{有益}$

0%

P2495 消耗战 题解

P2495 消耗战 题解

题面

题目链接

解法

虚树模版题,不多说。

AC代码

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
/**
* @file: P2495.cpp
* @author: yaoxi-std
* @url: https://www.luogu.com.cn/problem/P2495
*/
// #pragma GCC optimize ("O2")
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define resetIO(x) \
freopen(#x ".in", "r", stdin), freopen(#x ".out", "w", stdout)
#define debug(fmt, ...) \
fprintf(stderr, "[%s:%d] " fmt "\n", __FILE__, __LINE__, ##__VA_ARGS__)
template <class _Tp>
inline _Tp& read(_Tp &x) {
bool sign = false;
char ch = getchar();
long double tmp = 1;
for (; !isdigit(ch); ch = getchar())
sign |= (ch == '-');
for (x = 0; isdigit(ch); ch = getchar())
x = x * 10 + (ch ^ 48);
if (ch == '.')
for (ch = getchar(); isdigit(ch); ch = getchar())
tmp /= 10.0, x += tmp * (ch ^ 48);
return sign ? (x = -x) : x;
}
template <class _Tp>
inline void write(_Tp x) {
if (x < 0)
putchar('-'), x = -x;
if (x > 9)
write(x / 10);
putchar((x % 10) ^ 48);
}
const int MAXN = 3e5 + 10;
const int MAXM = 5e5 + 10;
const int MAXK = 20;
const int INFL = 0x3f3f3f3f3f3f3f3f;
struct graph {
struct edge {
int u, v, w;
} e[MAXM];
int head[MAXN], nxt[MAXM], tot;
void addedge(int u, int v, int w) {
e[++tot] = {u, v, w};
nxt[tot] = head[u];
head[u] = tot;
}
};
int n, m, k, inde, h[MAXN], dfn[MAXN], dep[MAXN];
int fa[MAXN][MAXK], mn[MAXN][MAXK], dp[MAXN], sta[MAXN], top;
bool tag[MAXN];
graph tr, vtr;
void build_tr(int u, int f, int w) {
dfn[u] = ++inde, dep[u] = dep[f] + 1;
fa[u][0] = f, mn[u][0] = w;
for (int i = 1; i < MAXK; ++i)
fa[u][i] = fa[fa[u][i - 1]][i - 1];
for (int i = 1; i < MAXK; ++i)
mn[u][i] = min(mn[u][i - 1], mn[fa[u][i - 1]][i - 1]);
for (int i = tr.head[u]; i; i = tr.nxt[i])
if (tr.e[i].v != f)
build_tr(tr.e[i].v, u, tr.e[i].w);
}
int lca(int u, int v) {
if (dep[u] < dep[v])
swap(u, v);
int t = dep[u] - dep[v];
for (int i = MAXK - 1; ~i; --i)
if ((t >> i) & 1)
u = fa[u][i];
if (u == v)
return u;
for (int i = MAXK - 1; ~i; --i)
if (fa[u][i] != fa[v][i])
u = fa[u][i], v = fa[v][i];
return fa[u][0];
}
int minpath(int f, int u) {
int t = dep[f] - dep[u];
int ret = INFL;
for (int i = MAXK - 1; ~i; --i)
if ((t >> i) & 1)
ret = min(ret, mn[u][i]), u = fa[u][i];
return ret;
}
void add_vedge(int u, int v) {
vtr.addedge(u, v, minpath(u, v));
}
void build_vtr() {
sort(h + 1, h + k + 1, [](int x, int y) {
return dfn[x] < dfn[y];
});
sta[top = 1] = 1, vtr.tot = 0, vtr.head[1] = 0;
for (int i = 1; i <= k; ++i) {
if (h[i] == 1)
continue;
int l = lca(sta[top], h[i]);
if (l != sta[top]) {
while (dfn[sta[top - 1]] > dfn[l])
add_vedge(sta[top - 1], sta[top]), --top;
if (sta[top - 1] == l)
add_vedge(l, sta[top]), --top;
else
vtr.head[l] = -1, add_vedge(l, sta[top]), sta[top] = l;
}
vtr.head[h[i]] = 0, sta[++top] = h[i];
}
for (int i = 1; i < top; ++i)
add_vedge(sta[i], sta[i + 1]);
}
void dfs(int u) {
dp[u] = 0;
for (int i = vtr.head[u]; i; i = vtr.nxt[i]) {
int v = vtr.e[i].v, w = vtr.e[i].w;
dfs(v);
if (tag[v])
dp[u] += w;
else
dp[u] += min(dp[v], w);
}
}
signed main() {
read(n);
for (int i = 1; i < n; ++i) {
int u, v, w;
read(u), read(v), read(w);
tr.addedge(u, v, w);
tr.addedge(v, u, w);
}
fill(mn[0], mn[0] + MAXK, INFL);
build_tr(1, 0, INFL);
read(m);
while (m--) {
read(k);
for (int i = 1; i <= k; ++i)
read(h[i]), tag[h[i]] = true;
build_vtr();
dfs(1);
write(dp[1]), putchar('\n');
for (int i = 1; i <= k; ++i)
tag[h[i]] = false;
}
return 0;
}