跳到正文

算法·理论

虚树学习笔记

Karieciation约 41 分钟
原文
文章信息与版本

文章信息

发布
2026年5月9日
收录
2026年5月16日
更新
2026年8月27日
篇幅
约 41 分钟

版本

  1. v1当前

虚树简介

‌虚树(Virtual Tree)是一种用于优化树上问题的高效数据结构,通过仅保留关键节点(如查询点)及其必要路径上的最近公共祖先(LCA),将原树规模从 Θ(n)\Theta\left(n\right) 压缩至 Θ(k)\Theta\left(k\right)(kk 为关键节点数),从而降低计算复杂度‌,适用于树上路径查询、树形 DP 等场景。‌‌‌

虚树的建立与应用

一个玩意发明出来肯定是给人用的你说对吧。

应用环境

一个问题往往会对树上的小部分点进行大量的操作,而一棵树本身往往很大,无用开销会很多,我们想到可以尽可能只操作我们想要操作的那一部分点从而优化(一种暴力罢了)。

思想

保留关键节点及各个关键节点之间的 LCA,这样会保留原关键节点个数的两倍不到,然后对于这些节点重新建树进行操作。但是如 LCA 这样的算法依旧带 log⁡\log,最终均摊期望达到 Θ(nlog⁡2n)\Theta(n\log_2n)。

代码

建一棵无边权虚树:

代码
C++
#include <cstdio>
#include <algorithm>
#define LF putchar('\n')
#define SP putchar(' ')
#define ui unsigned int
#define ll unsigned long long
#define N 1000005
#define M 2000005
#define K 20
using namespace std;

struct EDGE {
	struct Edge { ui to, nxt; } edge[M];
	ui head[N], idx;
	void A(const ui u, const ui v) {
		edge[++idx].to=v, edge[idx].nxt=head[u], head[u]=idx;
	}
} E, G;
#define Eh E.head
#define Ei E.idx
#define Ee E.edge
#define Ex for(ui i=Eh[u]; i; i=Ee[i].nxt)
#define Gh G.head
#define Gi G.idx
#define Ge G.edge
#define Gx for(ui i=Gh[u]; i; i=Ge[i].nxt)
ui c, b[N], dfn, num[N], dep[N], fat[N][K], rt;
bool vis[N];
bool cmp(ui a, ui b) {
	return num[a]<num[b];
}
void dfs(const ui u, const ui fa) {
	dep[u]=dep[fat[u][0]=fa]+1, num[u]=++dfn;
	for(ui i=1, j=0; i<K; ++i, ++j)
		fat[u][i]=fat[fat[u][j]][j];
	Ex {
		const ui v=Ee[i].to;
		if(v==fa) continue;
		dfs(v, u);
	}
}
ui LCA(ui x, ui y) {
	if(dep[x]<dep[y]) swap(x, y);
	ui t=dep[x]-dep[y], r=0;
	while(t) {
		if(t&1)
			x=fat[x][r];
		t>>=1, ++r;
	}
	if(x==y) return x;
	for(ui i=K-1; ~i; --i)
		if(fat[x][i]!=fat[y][i])
			x=fat[x][i], y=fat[y][i];
	return fat[x][0];
}
void fun(const ui u) {
	Gx {
		const ui v=Ge[i].to;
		fun(v);
	}
}

void clear() {
	Gi=0;
	for(ui i=1; i<=c; ++i)
		Gh[b[i]]=vis[b[i]]=false;
}
void build() {
	for(ui i=1; i<=c; ++i)
		vis[b[i]]=true;
	sort(b+1, b+1+c, cmp);
	rt=LCA(b[1], b[c]), b[++c]=rt;
	for(ui i=1; num[b[i+1]]>num[b[i]]; ++i)
		b[++c]=LCA(b[i], b[i+1]);
	sort(b+1, b+1+c, cmp);
	c=unique(b+1, b+1+c)-(b+1);
	for(ui i=2; i<=c; ++i)
		G.A(LCA(b[i-1], b[i]), b[i]);
}

建一棵有边权虚树(边权累加):

代码
C++
#include <cstdio>
#include <algorithm>
#define LF putchar('\n')
#define SP putchar(' ')
#define ui unsigned int
#define ll unsigned long long
#define N 1000005
#define M 2000005
#define K 20
using namespace std;

struct EDGE {
	struct Edge { ui to, nxt, w; } edge[M];
	ui head[N], idx;
	void A(const ui u, const ui v, const ui w) {
		edge[++idx].to=v, edge[idx].nxt=head[u], edge[idx].w=w, head[u]=idx;
	}
} E, G;
#define Eh E.head
#define Ei E.idx
#define Ee E.edge
#define Ex for(ui i=Eh[u]; i; i=Ee[i].nxt)
#define Gh G.head
#define Gi G.idx
#define Ge G.edge
#define Gx for(ui i=Gh[u]; i; i=Ge[i].nxt)
ui c, b[N], dfn, num[N], dep[N], fat[N][K], rt;
ll d[N][K];
bool vis[N];
bool cmp(ui a, ui b) {
	return num[a]<num[b];
}
void dfs(const ui u, const ui fa) {
	dep[u]=dep[fat[u][0]=fa]+1, num[u]=++dfn;
	for(ui i=1, j=0; i<K; ++i, ++j)
		fat[u][i]=fat[fat[u][j]][j], d[u][i]=d[u][j]+d[fat[u][j]][j];
	Ex {
		const ui v=Ee[i].to;
		if(v==fa) continue;
		d[v][0]=Ee[i].w;
		dfs(v, u);
	}
}
ui LCA(ui x, ui y) {
	if(dep[x]<dep[y]) swap(x, y);
	ui t=dep[x]-dep[y], r=0;
	while(t) {
		if(t&1)
			x=fat[x][r];
		t>>=1, ++r;
	}
	if(x==y) return x;
	for(ui i=K-1; ~i; --i)
		if(fat[x][i]!=fat[y][i])
			x=fat[x][i], y=fat[y][i];
	return fat[x][0];
}
void fun(const ui u) {
	Gx {
		const ui v=Ge[i].to;
		fun(v);
	}
}
ll getl(ui x, const ui y) {
	ui t=dep[x]-dep[y], r=0;
	ll res=0;
	while(t) {
		if(t&1)
			res+=d[x][r], x=fat[x][r];
		t>>=1, ++r;
	}
	return res;
}

void clear() {
	Gi=0;
	for(ui i=1; i<=c; ++i)
		Gh[b[i]]=vis[b[i]]=false;
}
void build() {
	for(ui i=1; i<=c; ++i)
		vis[b[i]]=true;
	sort(b+1, b+1+c, cmp);
	rt=LCA(b[1], b[c]), b[++c]=rt;
	for(ui i=1; num[b[i+1]]>num[b[i]]; ++i)
		b[++c]=LCA(b[i], b[i+1]);
	sort(b+1, b+1+c, cmp);
	c=unique(b+1, b+1+c)-(b+1);
	for(ui i=2, lca; i<=c; ++i) {
		lca=LCA(b[i-1], b[i]);
		G.A(lca, b[i], getl(b[i], lca));
	}
}

注意点

  1. 虚数数组要开两倍空间;
  2. 建虚树时要比较 dfn 序而不是原点编号,dfn 序是从原点编号对应来的;
  3. 注意空间的安排和使用;
  4. 想一想虚树的各个零部件有没有写挂;
  5. 待定。

应用(例题)

消耗战

带边权但是求 min⁡\min 就可以了。

代码
C++
#include <bits/stdc++.h>
#define LF putchar('\n')
#define SP putchar(' ')
#define ll long long
#define ui unsigned int
#define N 500005
#define M 500005
#define K 18
#define INF 0x3f3f3f3f
#define mod 1000000007
using namespace std;
namespace FastIO {
template<typename T> void read(T& x) {
	x=0; char ch=getchar(); while(ch<'0' || ch>'9') ch=getchar();
	while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+(ch&15), ch=getchar();
}
template<typename T, typename... Args> void read(T& x, Args&... args) { read(x); read(args...); }
template<typename T> void write(T x) {
	if(x==0) { putchar('0'); return ; }
	ui a[25], t=0; while(x) a[++t]=x%10, x/=10; while(t) putchar(a[t--]|48);
}
template<typename T, typename... Ts> void write(T x, Ts... args) { write(x); if(sizeof...(args)!=0) { SP; write(args...); } }
}
using namespace FastIO;
namespace M_K_W_ {
struct EDGE {
	struct Edge { ui to, nxt, w; } edge[M];
	ui head[N], idx;
	void A(const ui u, const ui v, const ui w) {
		edge[++idx].to=v, edge[idx].nxt=head[u], edge[idx].w=w, head[u]=idx;
	}
} E, G;
#define Eh E.head
#define Ei E.idx
#define Ee E.edge
#define Ex for(ui i=Eh[u]; i; i=Ee[i].nxt)
#define Gh G.head
#define Gi G.idx
#define Ge G.edge
#define Gx for(ui i=Gh[u]; i; i=Ge[i].nxt)
ui n, m, x, y, c, b[N], dfn, num[N], dep[N], fat[N][K], d[N][K];
ll f[N];
bool vis[N];
bool cmp(ui a, ui b) {
	return num[a]<num[b];
}
void dfs(const ui u, const ui fa) {
	dep[u]=dep[fat[u][0]=fa]+1, num[u]=++dfn;
	for(ui i=1, j=0; i<K; ++i, ++j)
		fat[u][i]=fat[fat[u][j]][j], d[u][i]=min(d[u][j], d[fat[u][j]][j]);
	Ex {
		const ui v=Ee[i].to;
		if(v==fa) continue;
		d[v][0]=Ee[i].w;
		dfs(v, u);
	}
}
ui LCA(ui x, ui y) {
	if(dep[x]<dep[y]) swap(x, y);
	ui t=dep[x]-dep[y], r=0;
	while(t) {
		if(t&1)
			x=fat[x][r];
		t>>=1, ++r;
	}
	if(x==y) return x;
	for(ui i=K-1; ~i; --i)
		if(fat[x][i]!=fat[y][i])
			x=fat[x][i], y=fat[y][i];
	return fat[x][0];
}
void fun(const ui u) {
	f[u]=0;
	Gx {
		const ui v=Ge[i].to;
		if(vis[v])
			f[u]+=Ge[i].w;
		else {
			fun(v);
			f[u]+=min((ll)Ge[i].w, f[v]);
		}
	}
}
ui getl(ui x, const ui y) {
	ui t=dep[x]-dep[y], r=0, res=INF;
	while(t) {
		if(t&1)
			res=min(res, d[x][r]), x=fat[x][r];
		t>>=1, ++r;
	}
	return res;
}

void clear() {
	Gi=0;
	for(ui i=1; i<=c; ++i)
		Gh[b[i]]=vis[b[i]]=false;
}
void build() {
	read(c);
	for(ui i=1; i<=c; vis[b[i++]]=true)
		read(b[i]);
	sort(b+1, b+1+c, cmp);
	b[++c]=1;
	for(ui i=1; num[b[i+1]]>num[b[i]]; ++i)
		b[++c]=LCA(b[i], b[i+1]);
	sort(b+1, b+1+c, cmp);
	c=unique(b+1, b+1+c)-(b+1);
	for(ui i=2, lca; i<=c; ++i) {
		lca=LCA(b[i-1], b[i]);
		G.A(lca, b[i], getl(b[i], lca));
	}
}
void solve() {
	read(n);
	for(ui i=1; i<n; ++i) {
		read(x, y, c);
		E.A(x, y, c);
		E.A(y, x, c);
	}
	dfs(1, 0);
	read(m);
	while(m--) {
		build();
		fun(1);
		write(f[1]);
		LF;
		clear();
	}
}
}
int main() {
	M_K_W_ :: solve();
	return 0;
}

Kingdom and its Cities

朴素无边权虚树,但是关键点中多了一项两原关键点中的一个点,在虚树上贪心(我为啥写的很臭)。

代码
C++
#include <bits/stdc++.h>
#define LF putchar('\n')
#define SP putchar(' ')
#define ll long long
#define ui unsigned int
#define N 200005
#define M 200005
#define K 18
#define INF 0x3f3f3f3f
#define mod 1000000007
using namespace std;
namespace FastIO {
template<typename T> void read(T& x) {
	x=0; char ch=getchar(); bool f=0; while(ch<'0' || ch>'9') { if(ch=='-') f=1; ch=getchar(); }
	while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+(ch&15), ch=getchar(); if(f) x=-x;
}
template<typename T, typename... Args> void read(T& x, Args&... args) { read(x); read(args...); }
template<typename T> void write(T x) {
	if(x==0) { putchar('0'); return ; } if(x<0) { putchar('-'); x=-x; }
	ui a[25], t=0; while(x) a[++t]=x%10, x/=10; while(t) putchar(a[t--]|48);
}
template<typename T, typename... Ts> void write(T x, Ts... args) { write(x); if(sizeof...(args)!=0) { SP; write(args...); } }
}
using namespace FastIO;
namespace M_K_W_ {
struct EDGE {
	struct Edge { ui to, nxt; } edge[M];
	ui head[N], idx;
	void A(const ui u, const ui v) {
		edge[++idx].to=v, edge[idx].nxt=head[u], head[u]=idx;
	}
} E, G;
#define Eh E.head
#define Ei E.idx
#define Ee E.edge
#define Ex for(ui i=Eh[u]; i; i=Ee[i].nxt)
#define Gh G.head
#define Gi G.idx
#define Ge G.edge
#define Gx for(ui i=Gh[u]; i; i=Ge[i].nxt)
ui n, m, x, y, c, b[N], dfn, num[N], dep[N], fat[N][K], rt, f[N][2];
bool vis[N], flag;
bool cmp(ui a, ui b) {
	return num[a]<num[b];
}
void dfs(const ui u, const ui fa) {
	dep[u]=dep[fat[u][0]=fa]+1, num[u]=++dfn;
	for(ui i=1, j=0; i<K; ++i, ++j)
		fat[u][i]=fat[fat[u][j]][j];
	Ex {
		const ui v=Ee[i].to;
		if(v==fa) continue;
		dfs(v, u);
	}
}
ui LCA(ui x, ui y) {
	if(dep[x]<dep[y]) swap(x, y);
	ui t=dep[x]-dep[y], r=0;
	while(t) {
		if(t&1)
			x=fat[x][r];
		t>>=1, ++r;
	}
	if(x==y) return x;
	for(ui i=K-1; ~i; --i)
		if(fat[x][i]!=fat[y][i])
			x=fat[x][i], y=fat[y][i];
	return fat[x][0];
}
ui get(ui x, const ui y) {
	ui t=dep[x]-dep[y]-1, r=0;
	while(t) {
		if(t&1)
			x=fat[x][r];
		t>>=1, ++r;
	}
	return x;
}

void clear() {
	Gi=0;
	for(ui i=1; i<=c; ++i)
		Gh[b[i]]=vis[b[i]]=false;
}
void build() {
	read(c);
	for(ui i=1; i<=c; vis[b[i++]]=true)
		read(b[i]);
	sort(b+1, b+1+c, cmp);
	rt=LCA(b[1], b[c]), b[++c]=rt;
	for(ui i=1; num[b[i+1]]>num[b[i]]; ++i) {
		b[++c]=LCA(b[i], b[i+1]);
		if(vis[b[c]])
			b[c]=get(b[i+1], b[c]);
	}
	sort(b+1, b+1+c, cmp);
	c=unique(b+1, b+1+c)-(b+1), flag=false;
	for(ui i=2; i<=c; ++i)
		G.A(LCA(b[i-1], b[i]), b[i]);
}
void fun(const ui u) {
	f[u][1]=0, f[u][0]=1;
	if(vis[u]) f[u][0]=1e9;
	ui cnt=0;
	Gx {
		const ui v=Ge[i].to;
		fun(v);
		if(vis[u]) {
			f[u][1]+=f[v][0];
			if(vis[v]) flag=true;
		}
		else if(f[v][1]<1e9)
			f[u][0]+=f[v][1], f[u][1]+=f[v][1], ++cnt;
		else if(f[v][0]<1e9)
			f[u][0]+=f[v][0], f[u][1]+=f[v][0];
	}
	if(!cnt&&f[u][0]<1e9) --f[u][0];
	if(f[u][0]<=f[u][1]||cnt>1) f[u][1]=1e9;
	if(f[u][1]>=1e9&&f[u][0]>=1e9) flag=true;
}
void solve() {
	read(n);
	for(ui i=1; i<n; ++i) {
		read(x, y);
		E.A(x, y);
		E.A(y, x);
	}
	dfs(1, 0);
	read(m);
	while(m--) {
		build();
		fun(rt);
		if(flag) putchar('-'), putchar('1');
		else write(min(f[rt][0], f[rt][1]));
		LF;
		clear();
	}
}
}
int main() {
	M_K_W_ :: solve();
	return 0;
}

Leaf Color

思考如何建虚树,这道题建虚树的方式还算显而易见,即把相同颜色的点整合起来作为一棵虚树,考虑对于一棵树而言对于它的所有子图中满足所有叶子均为关键点的子图进行计数。

这个树形 dp 稍微有点技术含量(?),你发现正着直接做很难,考虑对子图进行计数,再减去存在叶子为非关键点的方案就好了,式子不写了,滚代码里去看了。

代码
C++
#include <bits/stdc++.h>
#define LF putchar('\n')
#define SP putchar(' ')
#define ll long long
#define ui unsigned int
#define N 400005
#define M 400005
#define K 18
#define mod 998244353
using namespace std;
namespace FastIO {
template<typename T> void read(T& x) {
	x=0; char ch=getchar(); while(ch<'0' || ch>'9') ch=getchar();
	while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+(ch&15), ch=getchar();
}
template<typename T, typename... Args> void read(T& x, Args&... args) { read(x); read(args...); }
template<typename T> void write(T x) {
	if(x==0) { putchar('0'); return ; }
	ui a[25], t=0; while(x) a[++t]=x%10, x/=10; while(t) putchar(a[t--]|48);
}
template<typename T, typename... Ts> void write(T x, Ts... args) { write(x); if(sizeof...(args)!=0) { SP; write(args...); } }
}
using namespace FastIO;
namespace M_K_W_ {
struct EDGE {
	struct Edge { ui to, nxt; } edge[M];
	ui head[N], idx;
	void A(const ui u, const ui v) {
		edge[++idx].to=v, edge[idx].nxt=head[u], head[u]=idx;
	}
} E, G;
#define Eh E.head
#define Ei E.idx
#define Ee E.edge
#define Ex for(ui i=Eh[u]; i; i=Ee[i].nxt)
#define Gh G.head
#define Gi G.idx
#define Ge G.edge
#define Gx for(ui i=Gh[u]; i; i=Ge[i].nxt)
ui n, x, y, c, sum, b[N], dfn, num[N], dep[N], fat[N][K], rt, f[N][2], ans;
bool vis[N];
vector<ui> F[N];
ui ad(const ui x) {
	return x<mod ? x : x-mod;
}
bool cmp(ui a, ui b) {
	return num[a]<num[b];
}
void dfs(const ui u, const ui fa) {
	dep[u]=dep[fat[u][0]=fa]+1, num[u]=++dfn;
	for(ui i=1, j=0; i<K; ++i, ++j)
		fat[u][i]=fat[fat[u][j]][j];
	Ex {
		const ui v=Ee[i].to;
		if(v==fa) continue;
		dfs(v, u);
	}
}
ui LCA(ui x, ui y) {
	if(dep[x]<dep[y]) swap(x, y);
	ui t=dep[x]-dep[y], r=0;
	while(t) {
		if(t&1)
			x=fat[x][r];
		t>>=1, ++r;
	}
	if(x==y) return x;
	for(ui i=K-1; ~i; --i)
		if(fat[x][i]!=fat[y][i])
			x=fat[x][i], y=fat[y][i];
	return fat[x][0];
}
void fun(const ui u) {
	f[u][1]=0, f[u][0]=1;
	Gx {
		const ui v=Ge[i].to;
		fun(v);
		f[u][0]=(ll)f[u][0]*(f[v][0]+1)%mod, f[u][1]=ad(f[v][0]+f[u][1]);
	}
	if(!vis[u]) f[u][0]=ad(f[u][0]+998244352), sum=ad(ad(sum+f[u][0])+mod-f[u][1]);
	else sum=ad(sum+f[u][0]);
}

void clear() {
	Gi=0;
	for(ui i=1; i<=c; ++i)
		Gh[b[i]]=vis[b[i]]=false;
}
void build(const ui k) {
	c=F[k].size();
	for(ui i=0; i<c; vis[b[++i]]=true)
		b[i+1]=F[k][i];
	sort(b+1, b+1+c, cmp);
	rt=LCA(b[1], b[c]), b[++c]=rt;
	for(ui i=1; num[b[i+1]]>num[b[i]]; ++i)
		b[++c]=LCA(b[i], b[i+1]);
	sort(b+1, b+1+c, cmp);
	c=unique(b+1, b+1+c)-(b+1);
	for(ui i=2; i<=c; ++i)
		G.A(LCA(b[i-1], b[i]), b[i]);
}
void solve() {
	read(n);
	for(ui i=1; i<=n; F[x].push_back(i++))
		read(x);
	for(ui i=1; i<n; ++i) {
		read(x, y);
		E.A(x, y);
		E.A(y, x);
	}
	dfs(1, 0);
	for(ui i=1; i<=n; ++i) {
		if(F[i].empty()) continue;
		build(i);
		sum=0;
		fun(rt);
		ans=ad(ans+sum);
		clear();
	}
	write(ans);
}
}
int main() {
	M_K_W_ :: solve();
	return 0;
}

Railway

这个可以强行用虚树做,挺轮椅的。

具体的,你考虑一种差分值,但是差分结果在这个关键点间的边的最终值都为 11,你这样反向处理出原差分值,扔到总差分值里去,最后对着总差分值求个真实值,大于真实值的边都记一下就好了。

代码
C++
#include <bits/stdc++.h>
#define LF putchar('\n')
#define SP putchar(' ')
#define ui int
#define N 1000005
#define M 1000005
#define K 20
#define INF 0x3f3f3f3f
#define mod 1000000007
using namespace std;
namespace FastIO {
template<typename T> void read(T& x) {
	x=0; char ch=getchar(); bool f=0; while(ch<'0' || ch>'9') { if(ch=='-') f=1; ch=getchar(); }
	while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+(ch&15), ch=getchar(); if(f) x=-x;
}
template<typename T, typename... Args> void read(T& x, Args&... args) { read(x); read(args...); }
template<typename T> void write(T x) {
	if(x==0) { putchar('0'); return ; } if(x<0) { putchar('-'); x=-x; }
	ui a[25], t=0; while(x) a[++t]=x%10, x/=10; while(t) putchar(a[t--]|48);
}
template<typename T, typename... Ts> void write(T x, Ts... args) { write(x); if(sizeof...(args)!=0) { SP; write(args...); } }
}
using namespace FastIO;
namespace M_K_W_ {
struct EDGE {
	struct Edge { ui to, nxt, id; } edge[M];
	ui head[N], idx;
	void A(const ui u, const ui v, const ui id=0) {
		edge[++idx].to=v, edge[idx].nxt=head[u], edge[idx].id=id, head[u]=idx;
	}
} E, G;
#define Eh E.head
#define Ei E.idx
#define Ee E.edge
#define Ex for(ui i=Eh[u]; i; i=Ee[i].nxt)
#define Gh G.head
#define Gi G.idx
#define Ge G.edge
#define Gx for(ui i=Gh[u]; i; i=Ge[i].nxt)
ui n, m, k, x, y, c, b[N], dfn, num[N], dep[N], fat[N][K], rt, cnt[N], tc[N], ans[N];
bool vis[N];
bool cmp(ui a, ui b) {
	return num[a]<num[b];
}
void dfs(const ui u, const ui fa) {
	dep[u]=dep[fat[u][0]=fa]+1, num[u]=++dfn;
	for(ui i=1, j=0; i<K; ++i, ++j)
		fat[u][i]=fat[fat[u][j]][j];
	Ex {
		const ui v=Ee[i].to;
		if(v==fa) continue;
		dfs(v, u);
	}
}
ui LCA(ui x, ui y) {
	if(dep[x]<dep[y]) swap(x, y);
	ui t=dep[x]-dep[y], r=0;
	while(t) {
		if(t&1)
			x=fat[x][r];
		t>>=1, ++r;
	}
	if(x==y) return x;
	for(ui i=K-1; ~i; --i)
		if(fat[x][i]!=fat[y][i])
			x=fat[x][i], y=fat[y][i];
	return fat[x][0];
}
void fun(const ui u) {
	tc[u]=0;
	Gx {
		const ui v=Ge[i].to;
		fun(v);
		++tc[u];
	}
	cnt[u]+=1-tc[u];
}
void Sum(const ui u, const ui fa, const ui j) {
	Ex {
		const ui v=Ee[i].to;
		if(v==fa)
			continue;
		Sum(v, u, Ee[i].id);
		cnt[u]+=cnt[v];
	}
	if(cnt[u]>=k)
		ans[++c]=j;
}
void clear() {
	Gi=0;
	for(ui i=1; i<=c; ++i)
		Gh[b[i]]=vis[b[i]]=false;
}
void build() {
	read(c);
	for(ui i=1; i<=c; vis[b[i++]]=true)
		read(b[i]);
	sort(b+1, b+1+c, cmp);
	rt=LCA(b[1], b[c]), b[++c]=rt;
	for(ui i=1; num[b[i+1]]>num[b[i]]; ++i)
		b[++c]=LCA(b[i], b[i+1]);
	sort(b+1, b+1+c, cmp);
	c=unique(b+1, b+1+c)-(b+1);
	for(ui i=2; i<=c; ++i)
		G.A(LCA(b[i-1], b[i]), b[i]);
}
void solve() {
	read(n, m, k);
	for(ui i=1; i<n; ++i) {
		read(x, y);
		E.A(x, y, i);
		E.A(y, x, i);
	}
	dfs(1, 0);
	while(m--) {
		build();
		fun(rt);
		--cnt[rt];
		clear();
	}
	c=0;
	Sum(1, 0, 0);
	sort(ans+1, ans+1+c);
	write(c);
	LF;
	for(ui i=1; i<=c; ++i)
		write(ans[i]), SP;
}
}
int main() {
	M_K_W_ :: solve();
	return 0;
}

大工程

巨大堆屎题。

分别堆出任意两关键点距离和、最近关键点和最长链即可。

代码
C++
#include <bits/stdc++.h>
#define LF putchar('\n')
#define SP putchar(' ')
#define ll long long
#define ui unsigned int
#define N 2000005
#define M 2000005
#define K 20
#define INF 0x3f3f3f3f
#define mod 1000000007
using namespace std;
namespace FastIO {
template<typename T> void read(T& x) {
	x=0; char ch=getchar(); while(ch<'0' || ch>'9') ch=getchar();
	while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+(ch&15), ch=getchar();
}
template<typename T, typename... Args> void read(T& x, Args&... args) { read(x); read(args...); }
template<typename T> void write(T x) {
	if(x==0) { putchar('0'); return ; } if(x<0) { putchar('-'); x=-x; }
	ui a[25], t=0; while(x) a[++t]=x%10, x/=10; while(t) putchar(a[t--]|48);
}
template<typename T, typename... Ts> void write(T x, Ts... args) { write(x); if(sizeof...(args)!=0) { SP; write(args...); } }
}
using namespace FastIO;
namespace M_K_W_ {
struct EDGE {
	struct Edge { ui to, nxt, w; } edge[M];
	ui head[N], idx;
	void A(const ui u, const ui v, const ui w=1) {
		edge[++idx].to=v, edge[idx].nxt=head[u], edge[idx].w=w, head[u]=idx;
	}
} E, G;
#define Eh E.head
#define Ei E.idx
#define Ee E.edge
#define Ex for(ui i=Eh[u]; i; i=Ee[i].nxt)
#define Gh G.head
#define Gi G.idx
#define Ge G.edge
#define Gx for(ui i=Gh[u]; i; i=Ge[i].nxt)
ui n, m, x, y, c, b[N], dfn, num[N], dep[N], fat[N][K], rt, maxn, minn, maxd[N], mxd[N], mind[N], mnd[N];
ll f[N], g[N], cnt[N];
ll d[N][K];
bool vis[N];
bool cmp(ui a, ui b) {
	return num[a]<num[b];
}
void dfs(const ui u, const ui fa) {
	dep[u]=dep[fat[u][0]=fa]+1, num[u]=++dfn;
	for(ui i=1, j=0; i<K; ++i, ++j)
		fat[u][i]=fat[fat[u][j]][j], d[u][i]=d[u][j]+d[fat[u][j]][j];
	Ex {
		const ui v=Ee[i].to;
		if(v==fa) continue;
		d[v][0]=Ee[i].w;
		dfs(v, u);
	}
}
ui LCA(ui x, ui y) {
	if(dep[x]<dep[y]) swap(x, y);
	ui t=dep[x]-dep[y], r=0;
	while(t) {
		if(t&1)
			x=fat[x][r];
		t>>=1, ++r;
	}
	if(x==y) return x;
	for(ui i=K-1; ~i; --i)
		if(fat[x][i]!=fat[y][i])
			x=fat[x][i], y=fat[y][i];
	return fat[x][0];
}
void fun(const ui u) {
	maxd[u]=mxd[u]=f[u]=g[u]=0, mind[u]=mnd[u]=INF, cnt[u]=0;
	vector<ll> F, D;
	Gx {
		const ui v=Ge[i].to, w=Ge[i].w;
		fun(v);
		f[u]+=f[v], g[u]+=g[v]+w*cnt[v], F.push_back(g[v]+w*cnt[v]), D.push_back(cnt[v]), cnt[u]+=cnt[v];
		if(mind[v]+w<mind[u]) mnd[u]=mind[u], mind[u]=mind[v]+w;
		else if(mind[v]+w<mnd[u]) mnd[u]=mind[v]+w;
		if(maxd[v]+w>maxd[u]) mxd[u]=maxd[u], maxd[u]=maxd[v]+w;
		else if(maxd[v]+w>mxd[u]) mxd[u]=maxd[v]+w;
		if(vis[u]) f[u]+=g[v]+w*cnt[v];
	}
	for(ui i=0; i<F.size(); ++i)
		f[u]+=(cnt[u]-D[i])*F[i];
	maxn=max(maxn, maxd[u]+mxd[u]), minn=min(minn, mind[u]+mnd[u]);
	if(vis[u])
		minn=min(minn, mind[u]), mind[u]=0, ++cnt[u];
}
ui getl(ui x, const ui y) {
	ui t=dep[x]-dep[y], r=0, res=0;
	while(t) {
		if(t&1)
			res+=d[x][r], x=fat[x][r];
		t>>=1, ++r;
	}
	return res;
}
void clear() {
	Gi=0;
	for(ui i=1; i<=c; ++i)
		Gh[b[i]]=vis[b[i]]=false;
}
void build() {
	read(c);
	for(ui i=1; i<=c; vis[b[i++]]=true)
		read(b[i]);
	sort(b+1, b+1+c, cmp);
	rt=LCA(b[1], b[c]), b[++c]=rt;
	for(ui i=1; num[b[i+1]]>num[b[i]]; ++i)
		b[++c]=LCA(b[i], b[i+1]);
	sort(b+1, b+1+c, cmp);
	c=unique(b+1, b+1+c)-(b+1);
	for(ui i=2, lca; i<=c; ++i) {
		lca=LCA(b[i-1], b[i]);
		G.A(lca, b[i], getl(b[i], lca));
	}
}
void solve() {
	read(n);
	for(ui i=1; i<n; ++i) {
		read(x, y);
		E.A(x, y);
		E.A(y, x);
	}
	dfs(1, 0);
	read(m);
	while(m--) {
		build();
		maxn=0, minn=INF;
		fun(rt);
		write(f[rt], minn, maxn);
		LF;
		clear();
	}
}
}
int main() {
	M_K_W_ :: solve();
	return 0;
}
原文