题解 - CF613D Kingdom and its Cities

CF613D Kingdom and its Cities

题目简述

给一颗 \(n\) 个节点的树 , \(q\) 次询问每次给定 \(k\) 个关键点 , 要求删除最少非关键点使得这 \(k\) 个点互相不连通 .

\(n, q, \sum\limits_{i=1}^{q} k_i \le 10^5\) .


题解

以下节点均表示虚树上的节点 .

记 \(V_{\mathrm{key}}\) 为关键点集合 .

记 \(\operatorname{dist}(u , v)\) 为 \(u , v\) 之间的距离

记 \(\operatorname{child}(u)\) 为 \(u\) 的子节点集合 .

记 \(v \in \operatorname{child}(u)\) .

首先当 \(\exists \space u,v \in V , \space \operatorname{dist}(u,v)=1\) 时无解 .

接着看到 \(\sum\limits_{i=1}^{q} k_i \le 10^5\) 尝试使用虚树, 考虑在虚树上如何进行 \(DP\) .

\(1\). 使用 \(\mathrm{dp}_u\) 来表示状态

\(\hspace{1em}\) 设 \(\mathrm{dp}_u\) 表示将 \(u\) 子树内的节点全部分开需要的最少删除个数 .

\(\hspace{1em}\) 发现对于 \(u \notin V_{\mathrm{key}}\) 和 \(v \notin V_{\mathrm{key}}\) 不大好维护到底如何断开 (作者太笨想不到) .

\(2\). 使用 \(\mathrm{dp}_{u,\scriptscriptstyle 0/1}\) 来表示状态

\(\hspace{1em}\) 设 \(\mathrm{dp}_{u,\scriptscriptstyle 0/1}\) 表示 不删除/删除 \(u\) 将 \(u\) 子树内的节点全部分开需要的最少删除个数 .

\(\hspace{1em}\) 阿巴阿巴作者还是没想到 .

\(3\). 使用 \(\mathrm{dp}_{u,\scriptscriptstyle 0/1,\scriptscriptstyle 0/1}\) 来表示状态

\(\hspace{1em}\) 设 \(\mathrm{dp}_{u,\scriptscriptstyle 0/1 \scriptstyle,\scriptstyle*}\) 表示 不删除/删除 \(u\) 将 \(u\) 子树内的节点全部分开需要的最少删除个数 ,

\(\hspace{1em}\) 设 \(\mathrm{dp}_{u,\scriptstyle*,\scriptscriptstyle 0/1}\) 表示 \(u\) 和子树关键点不联通/联通 将 \(u\) 子树内的节点全部分开需要的最少删除个数 .

\(\hspace{1em}\) 接下来考虑求出所有 \(DP\) 状态 .

\(\hspace{1em}\) \(\mathrm{dp}_{u,\scriptscriptstyle 1 \scriptstyle,\scriptscriptstyle 1} = +\infty\) 因为不存在删除了 \(u\) 后 \(u\) 还和子树联通的情况 .

\(\hspace{1em}\) 记 \(\mathrm{cut}_{v}\) 为将 \(v\) 和 \(u\) 断开的代价 .

  • 当 \(v \in V_{\mathrm{key}}\) , \(\space \mathrm{cut}_{v} = \mathrm{dp}_{v,\scriptscriptstyle 0 \scriptstyle,\scriptscriptstyle 0} + 1\) . \(\\\) 只能从 \(u , v\) 之间删除一个点 .

  • 当 \(v \notin V_{\mathrm{key}}\) , \(\space \mathrm{cut}_{v} = \begin{cases} \min\left(\mathrm{dp}_{v,\scriptscriptstyle 1 \scriptstyle,\scriptscriptstyle 0},\mathrm{dp}_{v,\scriptscriptstyle 0 \scriptstyle,\scriptscriptstyle 0}\right) , & \operatorname{dist}(u,v)=1, \\ \min\left(\mathrm{dp}_{v,\scriptscriptstyle 1 \scriptstyle,\scriptscriptstyle 0},\mathrm{dp}_{v,\scriptscriptstyle 0 \scriptstyle,\scriptscriptstyle 0},\mathrm{dp}_{v,\scriptscriptstyle 0 \scriptstyle,\scriptscriptstyle 1} + 1\right) , & \operatorname{dist}(u,v) > 1 \end{cases}\) . \(\\\) 两种原本不连通的情况以及对于联通且边权大于 \(1\) 时可以在原树上挑一个节点删除的情况 .

\(\hspace{1em}\) 记 \(\mathrm{sumcost}\) 为将所有 \(v\) 断开的代价 , 不难发现 \(\mathrm{sumcost} = \sum\limits_{v} \mathrm{cut}_v\).

\(\hspace{1em}\) 记 \(\mathrm{keep}_v\) 为将 \(v\) 子树保留下来和 \(u\) 联通时的代价 , 不难发现 \(\mathrm{keep}_v = \begin{cases} \mathrm{dp}_{v,\scriptscriptstyle 0 \scriptstyle , \scriptscriptstyle 0} , & v \in V_{\mathrm{key}} , \\ \mathrm{dp}_{v,\scriptscriptstyle 0 \scriptstyle , \scriptscriptstyle 1} , & v \notin V_{\mathrm{key}} \end{cases}\) .

\(\hspace{1em}\) 接着进行分讨 :

\(\hspace{1em}\) 当 \(u \in V_{\mathrm{key}}\)

  • \(\mathrm{dp}_{u,\scriptscriptstyle 1 \scriptstyle,\scriptscriptstyle 0} = +\infty\) 关键点不能删除 .

  • \(\mathrm{dp}_{u,\scriptscriptstyle 0 \scriptstyle,\scriptscriptstyle 1} = +\infty\) 题目要求不联通 .

  • \(\mathrm{dp}_{u,\scriptscriptstyle 0 \scriptstyle,\scriptscriptstyle 0} = \mathrm{sumcost}\) .

\(\hspace{1em}\) 当 \(u \notin V_{\mathrm{key}}\)

  • \(\mathrm{dp}_{u,\scriptscriptstyle 1 \scriptstyle,\scriptscriptstyle 0} = \sum\limits_{v}\min\left(\mathrm{dp}_{v,\scriptscriptstyle 0 \scriptstyle,\scriptscriptstyle 0} , \mathrm{dp}_{v,\scriptscriptstyle 0 \scriptstyle,\scriptscriptstyle 1} , \mathrm{dp}_{v,\scriptscriptstyle 1 \scriptstyle,\scriptscriptstyle 0} \right) + 1\) . \(\\\)

  • 若 \(\exists\ v \in V_{\mathrm{key}} \land \operatorname{dist}(u,v) = 1\) 则 \(dp_{u,\scriptscriptstyle 0 \scriptstyle,\scriptscriptstyle 0} = +\infty\) . \(\\\) 否则 \(\mathrm{dp}_{u,\scriptscriptstyle 0 \scriptstyle,\scriptscriptstyle 0} = \sum\limits_{v} \mathrm{cut}_v\) .

  • 继续分讨 :

    • 若 \(\exists \space v_1 , v_2 \in V_{\mathrm{key}} \land v_1 \ne v2\) 则 \(\mathrm{dp}_{u,\scriptscriptstyle 0 \scriptstyle , \scriptscriptstyle 1} = +\infty\) .

    • 若 \(\exists! \space v \in V_{\mathrm{key}}\) 则 \(\mathrm{dp}_{u,\scriptscriptstyle 0 \scriptstyle , \scriptscriptstyle 1} = \mathrm{sumcost} + \mathrm{dp}_{v,\scriptscriptstyle 0 \scriptstyle , \scriptscriptstyle 0}\) .

    • 若 \(\nexists \space v \in V_{\mathrm{key}}\) 则 \(\mathrm{dp}_{u,\scriptscriptstyle 0 \scriptstyle , \scriptscriptstyle 1} = \min\limits_{v}\left(\mathrm{sumcost} + \mathrm{keep}_v - \mathrm{cut}_v\right)\) .

时间复杂度

\(O \left( n \log n + \sum\limits_{i=1}^{q} k_i \log n \right)\) .

主要来源于预处理 \(\mathrm{st}\) 表和建虚树的复杂度 .

空间复杂度

\(O \left( n \log n \right)\) .

主要来源于 \(\mathrm{st}\) 表的复杂度 .

屎山代码

#include<bits/stdc++.h>
using namespace std;

#define inf 0x3f3f3f3f

int n,lgn;
int q,k;
int _u,_v;
int rootvt;

int d=0,dfn[100001];
int dep[100001];
int st[21][100001];
bool iskey[100001];
int dp[100001][2][2];

int sz=0,stk[100001];
int posvis=0,vis[100001];

int pos=0,head[100001];
int posvt=0,headvt[200001];

struct edge
{
	int u,v,w;
	int nxt;
}e[200001],evt[100001];

struct tsk
{
	int h;
	bool operator<(const tsk &_cmp)
	{
		return dfn[h]<dfn[_cmp.h];
	}
}t[100001];

void addedge(int u,int v)
{
	e[++pos]={u,v,1,head[u]};
	head[u]=pos;
}

void dfs_init(int u,int f)
{
	dep[u]=dep[f]+1;
	st[0][u]=f;
	dfn[u]=++d;
	for(int i=head[u];i;i=e[i].nxt)
	{
		int v=e[i].v;
		if(v==f)
		{
			continue;
		}
		dfs_init(v,u);
	}
}

void init()
{
	lgn=__lg(n)+1;
	dfs_init(1,1);
	for(int k=1;k<=lgn;k++)
	{
		for(int u=1;u<=n;u++)
		{
			st[k][u]=st[k-1][st[k-1][u]];
		}
	}
}

int lcabf(int u,int v)
{
	if(dep[u]<dep[v])
	{
		swap(u,v);
	}
	for(int k=lgn;k>=0;k--)
	{
		if(dep[st[k][u]]>=dep[v])
		{
			u=st[k][u];
		}
	}
	if(u==v)
	{
		return u;
	}
	for(int k=lgn;k>=0;k--)
	{
		if(st[k][u]!=st[k][v])
		{
			u=st[k][u];
			v=st[k][v];
		}
	}
	return st[0][u];
}

int dis(int u,int v)
{
	return dep[u]+dep[v]-2*dep[lcabf(u,v)];
}

void addedgevt(int u,int v)
{
	int w=dis(u,v);
	evt[++posvt]={u,v,w,headvt[u]};
	headvt[u]=posvt;
}
void buildvt()
{
	sort(t+1,t+k+1);
	sz=1;
	stk[1]=t[1].h;
	vis[++posvis]=t[1].h;
	rootvt=t[1].h;
	for(int i=2;i<=k;i++)
	{
		int newnode=t[i].h;
		int lca=lcabf(newnode,stk[sz]);
		if(lca!=stk[sz])
		{
			while(sz>1&&dfn[lca]<dfn[stk[sz-1]])
			{
				addedgevt(stk[sz-1],stk[sz]);
				sz--;
			}
			if(dfn[lca]>dfn[stk[sz-1]])
			{
				addedgevt(lca,stk[sz]);
				stk[sz]=lca;
				vis[++posvis]=lca;
			}
			else
			{
				addedgevt(stk[sz-1],stk[sz]);
				sz--;
			}
		}
		stk[++sz]=newnode;
		vis[++posvis]=newnode;
	}
	for(int i=2;i<=sz;i++)
	{
		addedgevt(stk[i-1],stk[i]);
	}
	rootvt=stk[1];
}
void clearvt()
{
	posvt=0;
	for(int i=1;i<=posvis;i++)
	{
		int u=vis[i];
		headvt[u]=0;
		iskey[u]=0;
	}
	posvis=0;
}

bool dfs_check(int u)
{
	bool flag=true;
	for(int i=headvt[u];i;i=evt[i].nxt)
	{
		int v=evt[i].v;
		int w=evt[i].w;
		bool c=dfs_check(v);
		if(!c)
		{
			flag=false;
		}
		if(iskey[u]==1&&iskey[v]==1&&w==1)
		{
			flag=false;
		}
	}
	return flag;
}

void dfs(int u)
{
	dp[u][1][1]=inf;
	if(iskey[u])
	{
		dp[u][1][0]=inf;
		dp[u][0][1]=inf;
		dp[u][0][0]=0;
		for(int i=headvt[u];i;i=evt[i].nxt)
		{
			int v=evt[i].v;
			int w=evt[i].w;
			dfs(v);
			if(iskey[v])
			{
				dp[u][0][0]+=dp[v][0][0]+1;
			}
			else
			{
				int res=min(dp[v][1][0],dp[v][0][0]);
				if(w>1)
				{
					res=min(res,dp[v][0][1]+1);
				}
				dp[u][0][0]+=res;
			}
		}
	}
	else
	{
		dp[u][0][0]=0;
		dp[u][1][0]=1;
		int sumcost=0;
		int cntnearkey=0;
		int nearkey=-1;
		vector<int>cut,keep;
		for(int i=headvt[u];i;i=evt[i].nxt)
		{
			int w=evt[i].w;
			int v=evt[i].v;
			dfs(v);
			dp[u][1][0]+=min({dp[v][0][0],dp[v][1][0],dp[v][0][1]});
			if(iskey[v])
			{
				if(w==1)
				{
					dp[u][0][0]=inf;
					cut.push_back(inf);
					cntnearkey++;
					nearkey=v;
				}
				else
				{
					dp[u][0][0]+=dp[v][0][0]+1;
					sumcost+=dp[v][0][0]+1;
					cut.push_back(dp[v][0][0]+1);
				}
				keep.push_back(dp[v][0][0]);
			}
			else
			{
				int res=min(dp[v][1][0],dp[v][0][0]);
				if(w>1)
				{
					res=min(res,dp[v][0][1]+1);
				}
				dp[u][0][0]+=res;
				sumcost+=res;
				cut.push_back(res);
				keep.push_back(dp[v][0][1]);
			}
		}
		dp[u][0][1]=inf;
		if(cntnearkey>=2)
		{
			return;
		}
		else if(cntnearkey==1)
		{
			dp[u][0][1]=sumcost+dp[nearkey][0][0];
			return;
		}
		for(int i=0;i<cut.size();i++)
		{
			if(keep[i]==inf)
			{
				continue;
			}
			dp[u][0][1]=min(dp[u][0][1],sumcost+keep[i]-cut[i]);
		}
	}
}

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	cin>>n;
	for(int i=1;i<n;i++)
	{
		cin>>_u>>_v;
		addedge(_u,_v);
		addedge(_v,_u);
	}
	init();
	cin>>q;
	for(int _=1;_<=q;_++)
	{
		cin>>k;
		for(int i=1;i<=k;i++)
		{
			cin>>t[i].h;
			iskey[t[i].h]=1;
		}
		buildvt();
		if(!dfs_check(rootvt))
		{
			clearvt();
			cout<<"-1\n";
			continue;
		}
		dfs(rootvt);
		if(iskey[rootvt])
		{
			cout<<dp[rootvt][0][0]<<'\n';
		}
		else
		{
			cout<<min({dp[rootvt][0][0],dp[rootvt][1][0],dp[rootvt][0][1]})<<'\n';
		}
		clearvt();
	}
	return 0;
}

题外话

作者第一次写题解 , 写的不好请见谅 .

Runami_Yachiyo
posted @ 2026-08-26 15:14  炎黄喵喵  阅读(68)  评论(0)    收藏  举报