给你一棵nn个结点带边权的树。有mm 次询问,每次询问给出kk,并给出一个包含 k 个点的点集 SS

求将点集SS11 号点切断最小的代价

n2.5×105,m1,k5×105n \le 2.5 \times 10^5, m \ge 1, \sum k \le 5 \times 10^5

Luogu P2495

Solution

考虑一组询问,很容易想到一个这样的dp:

dp[x]dp[x]表示xx的子树都合法的最小代价,Min[x]Min[x]表示xx到根的路径上边权最小值,则

dp[x]={Min[x]xSmin{(yson[x]dp[y]),Min[x]}otherwise dp[x] = \begin{cases} Min[x] &x \in S\\\\ min\{\Big(\sum_{y\in son[x]} dp[y]\Big) , Min[x]\} &\text{otherwise} \end{cases}

预处理出MinMin数组之后直接在虚树上dp即可

第一次写虚树,用的倍增求lca,慢死了,下次记得用树剖求

Code

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
#include <bits/stdc++.h>

#define x first
#define y second
#define y1 Y1
#define y2 Y2
#define mp make_pair
#define pb push_back

using namespace std;

typedef long long LL;
typedef pair <int, int> pii;

template <typename T> inline int Chkmax (T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline int Chkmin (T &a, T b) { return a > b ? a = b, 1 : 0; }
template <typename T> inline T read ()
{
T sum = 0, fl = 1; char ch = getchar();
for (; !isdigit(ch); ch = getchar()) if (ch == '-') fl = -1;
for (; isdigit(ch); ch = getchar()) sum = (sum << 3) + (sum << 1) + ch - '0';
return sum * fl;
}

inline void proc_status ()
{
ifstream t ("/proc/self/status");
cerr << string (istreambuf_iterator <char> (t), istreambuf_iterator <char> ()) << endl;
}

const int Maxn = 250000 + 100, Maxk = 500000;

int N, Q, K;
int e, Begin[Maxn], To[Maxn << 1], Next[Maxn << 1], W[Maxn << 1];
int dfn[Maxn], size[Maxn], dfs_clock, dep[Maxn], anc[20][Maxn];
LL Min[Maxn];

inline void add_edge (int x, int y, int z) { To[++e] = y; Next[e] = Begin[x]; Begin[x] = e; W[e] = z; }

int A[Maxk];

inline int get_lca (int x, int y)
{
if (dep[x] < dep[y]) swap(x, y);
for (int i = 18; i >= 0; --i) if (dep[anc[i][x]] >= dep[y]) x = anc[i][x];
if (x == y) return x;
for (int i = 18; i >= 0; --i) if (anc[i][x] != anc[i][y]) x = anc[i][x], y = anc[i][y];
return anc[0][x];
}

namespace VTREE
{
int e, Begin[Maxn], To[Maxn << 1], Next[Maxn << 1];
int Save[Maxk], save_cnt;
int Stack[Maxk], top;
int Vis[Maxk];

inline void add_edge (int x, int y) { To[++e] = y; Next[e] = Begin[x]; Begin[x] = e; }

inline void init ()
{
for (int i = 1; i <= save_cnt; ++i) Begin[Save[i]] = 0, Vis[Save[i]] = 0;
e = save_cnt = top = 0;
}

inline int cmp_by_dfn (int x, int y) { return dfn[x] < dfn[y]; }

inline void build ()
{
sort(A + 1, A + K + 1, cmp_by_dfn);
for (int i = 1; i <= K; ++i) Vis[A[i]] = 1;
for (int i = K; i > 1; --i) A[++K] = get_lca (A[i], A[i - 1]); A[++K] = 1;
sort(A + 1, A + K + 1, cmp_by_dfn);
K = unique (A + 1, A + K + 1) - A - 1;
for (int i = 1; i <= K; ++i) Save[++save_cnt] = A[i];

for (int i = 1; i <= K; ++i)
{
while (top && dfn[Stack[top]] + size[Stack[top]] - 1 < dfn[A[i]]) --top;
if (top) add_edge (Stack[top], A[i]);
Stack[++top] = A[i];
}
}

inline LL get_dp (int x)
{
if (Vis[x]) return Min[x];

LL sum = 0;
for (int i = Begin[x]; i; i = Next[i]) sum += get_dp (To[i]);

return min(Min[x], sum);
}

inline void work ()
{
printf("%lld\n", get_dp (1));
}
}

inline void dfs_pre (int x)
{
dfn[x] = ++dfs_clock, dep[x] = dep[anc[0][x]] + 1, size[x] = 1;
for (int i = 1; i <= 18; ++i) anc[i][x] = anc[i - 1][anc[i - 1][x]];

for (int i = Begin[x]; i; i = Next[i])
{
int y = To[i];
if (y == anc[0][x]) continue;
anc[0][y] = x, Min[y] = min(Min[x], (LL)W[i]);

dfs_pre (y);

size[x] += size[y];
}
}

inline void Solve ()
{
/**/Min[1] = 2e18;
dfs_pre (1);

Q = read<int>();
while (Q--)
{
K = read<int>();
for (int i = 1; i <= K; ++i) A[i] = read<int>();
VTREE :: init ();
VTREE :: build ();
VTREE :: work ();
}
}

inline void Input ()
{
N = read<int>();

for (int i = 1; i < N; ++i)
{
int x = read<int>(), y = read<int>(), z = read<int>();
add_edge (x, y, z);
add_edge (y, x, z);
}
}

int main()
{

#ifndef ONLINE_JUDGE
freopen("A.in", "r", stdin);
freopen("A.out", "w", stdout);
#endif

Input ();
Solve ();

return 0;
}