才切完一道紫,然后踌躇满志地做这道题,然后被肘飞了,看了眼题解,有了点思路,决定先写做题记录整理思路。
看起来这道题与消耗战很像,都要断边,然后拿它的思路想这道题,失败了,这两道本质上还是有区别的。
先判断如何无解,答:两个波西米亚在原树上连在一起就是无解,为什么要特别强调为原树,答:因为这道题很容易看出是虚树(多次询问加关键点加提供了 的累加和,甚至字母都用的是 !),在虚树上判断也很简单,只要两个点是父子且深度相差为一就行了。
然后思考如何计算有解的答案,由于要断开边,可能断开此条边或者深度更浅时断开另一条边,再加上消耗战的影响,想到动态规划,然后思考如何转移。 这个和消耗战不同,必须要把所有关键点都断开而非只与 1 号点断开。所以当当前点是波西米亚时,我们把连向它的所有是波西米亚的儿子的边都断开;否则如果不是关键点,我们选择要么是断开通向所有是波西米亚的儿子的边要么直接解决掉这个点。这里可能有人会疑惑:为什么题目中是占领点,而这里说是断开边呢,答案是有解时两个波西米亚间肯定有南蒂罗尔,但是虚树上没体现,不然就无解,而这些没体现的南蒂罗尔就被我当成边来对待了。而南蒂罗尔和波西米亚间不一定有点,那断开这条边相当于占领这个南蒂罗尔(南蒂罗尔不可能给你的!),一个南蒂罗尔连向超过相邻两个波西米亚答案就会自动归 1。所以正确性是可以保证的。当然不要忘了加上每个儿子的 dp 值。
写完做题记录思路居然就清晰了(?),容我先去写一下(2025年5月21日19:06:43)。
当前时间:2025年5月21日19:48:45,这道题还是有些细节的。我们要注意,当南蒂罗尔只有一个波西米亚儿子时,那么不算入贡献,将它设为波西米亚,我们可以看图:
图中绿点为波西米亚,红点南蒂罗尔(笑点解析:这代表 GDP 颜色),如果按我们以往的做法,答案会是 2,而正确的做法只要把深度最浅的点删掉就行了,所以答案是 1。
然后其他就没什么了,记得是南蒂罗尔的波西米亚儿子超过 1,才归 1,不然会 WA 第 11 个点(好水)。 代码:
//Just Sayori
#pragma GCC diagnostic ignored "-Wnarrowing"
#pragma GCC diagnostic ignored "-Wsign-compare"
#include <iostream>
#include <cstdio>
#include <algorithm>
#define ll long long
#define ull unsigned long long
#define rnt register int
#define gr getchar
#define pr putchar
#define N 500005
#define M 1000000007
using namespace std;
inline ll read()
{
ll x = 0, f = 1;
char ch = gr();
while (ch < '0' || ch > '9') ch == '-' ? f = -1, ch = gr() : ch = gr();
while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + (ch ^ 48), ch = gr();
return x * f;
}
inline void write(ll x)
{
static int top = 0, sta[39];
if (x < 0) pr('-'), x = -x;
do sta[++top] = x % 10, x /= 10;
while (x);
while (top) pr(sta[top--] ^ 48);
}
struct EDGE
{
struct edge
{
int v, next;
} e[N << 1];
int head[N], cnt;
inline void tdd(int u, int v)
{
e[++cnt] = {v, head[u]}, head[u] = cnt;
e[++cnt] = {u, head[v]}, head[v] = cnt;
}
} t, vt;
int n, m, k, cnt, lca, top;
bool ans;
int p[N], dp[N], fa[N], sz[N], tp[N], dfn[N], num[N], bson[N], deep[N], stack[N];
bool q[N];
void dfs1(int u, int f)
{
fa[u] = f, sz[u] = 1;
deep[u] = deep[f] + 1;
for (rnt i = t.head[u]; i; i = t.e[i].next)
{
int v = t.e[i].v;
if (v == f) continue;
dfs1(v, u);
sz[u] += sz[v];
if (sz[v] > sz[bson[u]]) bson[u] = v;
}
}
void dfs2(int u, int p)
{
tp[u] = p;
dfn[u] = ++cnt;
if (!bson[u]) return;
dfs2(bson[u], p);
for (rnt i = t.head[u]; i; i = t.e[i].next)
{
int v = t.e[i].v;
if (v == fa[u] || v == bson[u]) continue;
dfs2(v, v);
}
}
void dfs3(int u, int f)
{
int sum = 0;
dp[u] = 0;
for (rnt i = vt.head[u]; i; i = vt.e[i].next)
{
int v = vt.e[i].v;
if (v == f) continue;
dfs3(v, u);
sum += q[v];
dp[u] += dp[v];
q[v] = 0;
}
if (!q[u] && sum == 1)
q[u] = 1, sum = 0;
if (!q[u])
sum = min(sum, 1);
dp[u] += sum;
}
int get_lca(int a, int b)
{
while (tp[a] != tp[b])
{
if (deep[tp[a]] < deep[tp[b]]) swap(a, b);
a = fa[tp[a]];
}
if (deep[a] > deep[b]) swap(a, b);
return a;
}
inline bool cmp(int a, int b)
{
return dfn[a] < dfn[b];
}
void build()
{
sort(p + 1, p + k + 1, cmp);
top = 0, stack[++top] = 1, vt.cnt = 0, vt.head[1] = 0;
for (rnt i = 1; i <= k; i++)
if (p[i] != 1)
{
lca = get_lca(p[i], stack[top]);
if (lca != stack[top])
{
while (dfn[lca] < dfn[stack[top - 1]])
vt.tdd(stack[top - 1], stack[top]), top--;
if (dfn[lca] != dfn[stack[top - 1]])
vt.head[lca] = 0, vt.tdd(lca, stack[top]), stack[top] = lca;
else
vt.tdd(lca, stack[top--]);
}
vt.head[p[i]] = 0, stack[++top] = p[i];
}
for (rnt i = 1; i < top; i++)
vt.tdd(stack[i], stack[i + 1]);
}
int main()
{
n = read();
for (rnt i = 1; i < n; i++)
t.tdd(read(), read());
m = read(), dfs1(1, 0), dfs2(1, 1);
for (rnt i = 1; i <= m; i++)
{
k = read();
for (rnt i = 1; i <= k; i++) p[i] = read(), q[p[i]] = 1;
for (rnt i = 1; i <= k; i++)
if (q[fa[p[i]]])
{
ans = 1;
break;
}
if (ans)
{
puts("-1"), ans = 0;
for (rnt i = 1; i <= k; i++) q[p[i]] = 0;
continue;
}
build(), dfs3(1, 0), q[1] = 0;
write(dp[1]), pr(10);
}
return 0;
}