CF613D Kingdom and its Cities

给定一棵树,每次询问$k_i$个点,询问最少需要截断多少个点,可以使得这些点两两联通。

$\sum k_i \leq 10^5$

显然是虚树,然后考虑如何$dp$。

首先特胖一下是否有相邻的两个点。

$f[x]$为以$x$为根的子树的答案.

$g[x]$为此时有没有关键点直连到 $x$ 的父亲,即把$x$去掉。

  • 如果$x$不是关键点,并且旗下关键点$>1$,那么显然可以$f[x]=\sum dp[y]+1$,把自己去了并且$g[x]=0$
  • 如果是关键点,子树里有顶部存在关键点去掉中间的点,$dp[x]=\sum (g[x] +dp[y])$,
  • 如果$x$不是关键点,并且旗下关键点$g[x]=1$,无所谓,那么显然可以$f[x]=\sum dp[y]$,$g[x]=(g[x]=1)$
代码
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
187
188
189
190

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pii pair<int, int>
#define mk make_pair
const int N = 2e5 + 10;
const int mod = 1e9 + 7;
const int INF = 1e9;
int read()
{
int x = 0, F = 1;
char c = getchar();
while (c < '0' || c > '9')
{
if (c == '-')
F = -1;
c = getchar();
}
while (c >= '0' && c <= '9')
x = (x << 1) + (x << 3) + c - '0', c = getchar();
return x * F;
}

namespace Tree
{

int st[N][22], dfn[N], dep[N], tot, lg2[N];
vector<int> G[N];
void dfs(int x, int fa)
{
st[++tot][0] = x;
dep[x] = dep[fa] + 1;
dfn[x] = tot;
for (int to : G[x])
{
if (to == fa)
continue;
dfs(to, x);
st[++tot][0] = x;
}
}
int lower(int u, int v)
{
return (dep[u] < dep[v]) ? u : v;
}

void Lca_init()
{
dfs(1, 0);
for (int i = 2; i <= tot; i++)
lg2[i] = lg2[i >> 1] + 1;
for (int l = 1; (1 << l) <= tot; l++)
{
for (int i = 1; i + (1 << l) - 1 <= tot; i++)
st[i][l] = lower(st[i][l - 1], st[i + (1 << (l - 1))][l - 1]);
}
}
int lca(int u, int v)
{
u = dfn[u];
v = dfn[v];
if (u > v)
swap(u, v);
int i = lg2[v - u + 1], w = (1 << i);
return lower(st[u][i], st[v - w + 1][i]);
}
int dis(int u, int v)
{
int lc = lca(u, v);
return dep[u] + dep[v] - 2 * dep[lc];
}
}; // namespace Tree
using namespace Tree;

int vis[N];
int b[N];
namespace XTree
{
stack<int> s;
int F[N], G[N];
int flag;

vector<int> g[N];

bool cmp(int x, int y)
{
return Tree::dfn[x] < Tree::dfn[y];
}

void addEdge(int u, int v)
{

int d = Tree::dis(u, v);

if (vis[u] && vis[v] && d == 1)
flag = 0;
g[u].push_back(v);
g[v].push_back(u);
}

void build(int k)
{
flag = 1;

while (!s.empty())

s.pop();
sort(b + 1, b + 1 + k, cmp);

s.push(1);
for (int i = 1; i <= k; ++i)
{
int x = b[i];
int lca = Tree::lca(x, s.top());
while (s.top() != lca)
{
int tmp = s.top();
s.pop();
if (dfn[s.top()] < dfn[lca])
s.push(lca);
addEdge(s.top(), tmp);
}
s.push(x);
}
while (s.top() != 1)
{
int tmp = s.top();
s.pop();
addEdge(s.top(), tmp);
}
}
void dfs(int x, int fa)
{
F[x] = 0, G[x] = 0;
for (int to : g[x])
{
if (to == fa)
continue;
dfs(to, x);
F[x] += F[to];
G[x] += G[to];
}
if (!vis[x])
F[x] += (G[x] > 1), G[x] = (G[x] == 1);
else
F[x] += G[x], G[x] = 1;
g[x].clear();
}
void solve()
{
dfs(1, 0);
if (!flag)
puts("-1");
else
{
cout << F[1] << endl;
}
}
} // namespace XTree

int main()
{
int n = read();
for (int i = 1; i < n; i++)
{
int u = read(), v = read();
G[u].push_back(v);
G[v].push_back(u);
}
Lca_init();
int q = read();

for (int i = 1; i <= q; i++)
{
int k = read();

for (int j = 1; j <= k; j++)
{
int x = read();
b[j] = x, vis[x] = 1;
}

XTree::build(k);
XTree::solve();

for (int j = 1; j <= k; j++)
vis[b[j]] = 0;
}
}