跳到正文

个人记录

树相关

Rachel_in约 9 分钟
原文
文章信息与版本

文章信息

发布
2019年11月15日
收录
2026年1月20日
更新
2026年11月8日
篇幅
约 9 分钟

版本

  1. v2当前
  2. v1

树相关

树就是树。

读边:

做夜梦的时候都能打出来

C++
inline void Add(int u,int v){
    nxt[++cnt]=fir[u];
    fir[u]=cnt;
    go[cnt]=v;
}

还可以用vector来存边(有时可能要对边排序)。

dfs序

就是入栈的顺序,可以自行操作,可以把子节点提到父亲前面来卡常(?)满足一个子树内的dfs序都是连续的,所以可以完成一些对子树内操作的要求。

直径

树上最长的链,没负权的时候可以用两遍dfs求,因为每个点距离每个点最远的就是直径上两个点之一。

如果有负边的话就dp吧。

LCA

最近公共祖先。

倍增法:

C++
#include<bits/stdc++.h>
using namespace std;
inline int read(){
	int res=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-f;ch=getchar();}
	while(ch>='0'&&ch<='9'){res=(res<<3)+(res<<1)+ch-'0';ch=getchar();}
	return res*f;
}
void write(int x){
	if(x<0) putchar('-'),x=-x;
	if(x>9) write(x/10);
	putchar(x%10|'0');
}
const int N=500005;
int fir[N],nxt[N<<1],go[N<<1],cnt,fa[N][20],dep[N];
int n,root,Q;
inline void Add(int u,int v){
	nxt[++cnt]=fir[u];
	fir[u]=cnt;
	go[cnt]=v;
}
void dfs(int u,int f){
	dep[u]=dep[f]+1;
	for(int e=fir[u];e;e=nxt[e]){
		int v=go[e];
		if(v==f) continue;
		fa[v][0]=u;
		dfs(v,u);
	}
}
int main(){
	n=read();Q=read();root=read();
	for(int i=1;i<n;i++){
		int u=read(),v=read();
		Add(u,v);Add(v,u);
	}
	dfs(root,0);
	for(int j=1;j<=17;j++)
		for(int i=1;i<=n;i++)
			fa[i][j]=fa[fa[i][j-1]][j-1];
	while(Q--){
		int x=read(),y=read();
		if(dep[x]<dep[y]) swap(x,y);
		for(int i=17;i>=0;i--)
			if(dep[fa[x][i]]>=dep[y]) x=fa[x][i];
		if(x==y){
			write(x);putchar('\n');continue;
		}
		for(int i=17;i>=0;i--){
			if(fa[x][i]!=fa[y][i]){
				x=fa[x][i];y=fa[y][i];
			}
		}
		write(fa[x][0]);putchar('\n');
	}
	return 0;
}

树剖版:

C++
#include<bits/stdc++.h>
using namespace std;
inline int read(){
	int res=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-f;ch=getchar();}
	while(ch>='0'&&ch<='9'){res=(res<<3)+(res<<1)+ch-'0';ch=getchar();}
	return res*f;
}
void write(int x){
	if(x<0) putchar('-'),x=-x;
	if(x>9) write(x/10);
	putchar(x%10|'0');
}
const int N=500005;
int fir[N],nxt[N<<1],go[N<<1],cnt,son[N],size[N],dep[N],tf[N],fa[N];
int n,root,Q;
inline void Add(int u,int v){
	nxt[++cnt]=fir[u];
	fir[u]=cnt;
	go[cnt]=v;
}
void dfs1(int u,int f){
	dep[u]=dep[f]+1;
	size[u]=1;
	for(int e=fir[u];e;e=nxt[e]){
		int v=go[e];
		if(v==f) continue;
		fa[v]=u;
		dfs1(v,u);
		size[u]+=size[v];
		if(size[son[u]]<size[v]) son[u]=v;
	}
}
void dfs2(int u,int topf){
	tf[u]=topf;
	if(!son[u]) return ;
	dfs2(son[u],topf);
	for(int e=fir[u];e;e=nxt[e]){
		int v=go[e];
		if(v==fa[u]||v==son[u]) continue;
		dfs2(v,v);
	}
}
inline int LCA(int x,int y){
	while(tf[x]!=tf[y]){
		if(dep[tf[x]]<dep[tf[y]]) swap(x,y);
		x=fa[tf[x]];
	}
	return dep[x]<dep[y]?x:y;
}
int main(){
	n=read();Q=read();root=read();
	for(int i=1;i<n;i++){
		int u=read(),v=read();
		Add(u,v);Add(v,u);
	}
	dfs1(root,0);
	dfs2(root,root);
	while(Q--){
		int u=read(),v=read();
		write(LCA(u,v));putchar('\n');
	}
}

[USACO15DEC]最大流Max Flow

树上差分。

C++
#include<bits/stdc++.h>
using namespace std;
inline int read(){
	int res=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-f;ch=getchar();}
	while(ch>='0'&&ch<='9'){res=(res<<3)+(res<<1)+ch-'0';ch=getchar();}
	return res*f;
}
void write(int x){
	if(x<0) putchar('-'),x=-x;
	if(x>9) write(x/10);
	putchar(x%10|'0');
}
const int N=50005;
int fir[N],nxt[N<<1],go[N<<1],cnt,sz[N],tf[N];
int dep[N],size[N],son[N],fa[N];
int n,k,ans;
inline void Add(int u,int v){
	nxt[++cnt]=fir[u];
	fir[u]=cnt;
	go[cnt]=v;
}
void dfs1(int u,int f){
	dep[u]=dep[f]+1;
	size[u]=1;
	for(int e=fir[u];e;e=nxt[e]){
		int v=go[e];
		if(v==f) continue;
		fa[v]=u;
		dfs1(v,u);
		size[u]+=size[v];
		if(size[son[u]]<size[v]) son[u]=v;
	}
}
void dfs2(int u,int topf){
	tf[u]=topf;
	if(!son[u]) return ;
	dfs2(son[u],topf);
	for(int e=fir[u];e;e=nxt[e]){
		int v=go[e];
		if(v==son[u]||v==fa[u]) continue;
		dfs2(v,v);
	}
}
inline int LCA(int x,int y){
	while(tf[x]^tf[y]){
		if(dep[tf[x]]<dep[tf[y]]) swap(x,y);
		x=fa[tf[x]];
	}
	return dep[x]<dep[y]?x:y;
}
void dfs(int u,int fa){
	for(int e=fir[u];e;e=nxt[e]){
		int v=go[e];
		if(v==fa) continue;
		dfs(v,u);
		sz[u]+=sz[v];
	}
}
int main(){
	n=read();k=read();
	for(int i=1;i<n;i++){
		int u=read(),v=read();
		Add(u,v);Add(v,u);
	}
	dfs1(1,0);
	dfs2(1,1);
	for(int i=1;i<=k;i++){
		int x=read(),y=read(),lca=LCA(x,y);
		sz[x]++;sz[y]++;sz[lca]--;sz[fa[lca]]--;
	}
	dfs(1,0);
	ans=0;
	for(int i=1;i<=n;i++){
		ans=max(ans,sz[i]);
	}
	write(ans);
	return 0;
}
原文