跳到正文

题解

题解 - CF613D Kingdom and its Cities

yhmmAB约 18 分钟
原文
文章信息与版本

文章信息

发布
2026年8月26日
收录
2026年8月26日
更新
2026年11月11日
篇幅
约 18 分钟
获赞
1
题目
CF613D

版本

  1. v1当前

CF613D Kingdom and its Cities

题目简述

给一颗 nn 个节点的树 , qq 次询问每次给定 kk 个关键点 , 要求删除最少非关键点使得这 kk 个点互相不连通 .

n,q,∑i=1qki≤105n, q, \sum\limits_{i=1}^{q} k_i \le 10^5 .


题解

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

记 VkeyV_{\mathrm{key}} 为关键点集合 .

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

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

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

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

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

11. 使用 dpu\mathrm{dp}_u 来表示状态

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

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

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

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

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

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

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

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

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

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

\hspace{1em} 记 cutv\mathrm{cut}_{v} 为将 vv 和 uu 断开的代价 .

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

  • 当 v∉Vkeyv \notin V_{\mathrm{key}} ,  cutv={min⁡(dpv,1,0,dpv,0,0),dist⁡(u,v)=1,min⁡(dpv,1,0,dpv,0,0,dpv,0,1+1),dist⁡(u,v)>1\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} . \\ 两种原本不连通的情况以及对于联通且边权大于 11 时可以在原树上挑一个节点删除的情况 .

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

\hspace{1em} 记 keepv\mathrm{keep}_v 为将 vv 子树保留下来和 uu 联通时的代价 , 不难发现 keepv={dpv,0,0,v∈Vkey,dpv,0,1,v∉Vkey\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∈Vkeyu \in V_{\mathrm{key}}
  • dpu,1,0=+∞\mathrm{dp}_{u,\scriptscriptstyle 1 \scriptstyle,\scriptscriptstyle 0} = +\infty 关键点不能删除 .

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

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

\hspace{1em} 当 u∉Vkeyu \notin V_{\mathrm{key}}
  • dpu,1,0=∑vmin⁡(dpv,0,0,dpv,0,1,dpv,1,0)+1\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 . \\

  • 若 ∃ v∈Vkey∧dist⁡(u,v)=1\exists\ v \in V_{\mathrm{key}} \land \operatorname{dist}(u,v) = 1 则 dpu,0,0=+∞dp_{u,\scriptscriptstyle 0 \scriptstyle,\scriptscriptstyle 0} = +\infty . \\ 否则 dpu,0,0=∑vcutv\mathrm{dp}_{u,\scriptscriptstyle 0 \scriptstyle,\scriptscriptstyle 0} = \sum\limits_{v} \mathrm{cut}_v .

  • 继续分讨 :

    • 若 ∃ v1,v2∈Vkey∧v1≠v2\exists \space v_1 , v_2 \in V_{\mathrm{key}} \land v_1 \ne v2 则 dpu,0,1=+∞\mathrm{dp}_{u,\scriptscriptstyle 0 \scriptstyle , \scriptscriptstyle 1} = +\infty .

    • 若 ∃! v∈Vkey\exists! \space v \in V_{\mathrm{key}} 则 dpu,0,1=sumcost+dpv,0,0\mathrm{dp}_{u,\scriptscriptstyle 0 \scriptstyle , \scriptscriptstyle 1} = \mathrm{sumcost} + \mathrm{dp}_{v,\scriptscriptstyle 0 \scriptstyle , \scriptscriptstyle 0} .

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

时间复杂度

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

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

空间复杂度

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

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

屎山代码

C++
#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

原文