跳到正文

算法·理论

树的直径

Luna_From_Starry约 11 分钟
原文
文章信息与版本

文章信息

发布
2026年3月14日
收录
2026年3月17日
更新
2026年9月23日
篇幅
约 11 分钟

版本

  1. v2当前
  2. v1

树的直径是什么

树的直径是树中最长的一条简单路径

某些定理

  1. 树的直径不唯一。 证明:得利用另一个定理

  2. 树的直径的端点一定是一个度数为 11 的点(叶子 or 根)。 证明: 方法:反证法

我们假设树的直径(树中最长的一条简单路径)的端点不是一个度数为 11 的点,那么一定有一端可以继续向外延伸,使得这条树的直径(树中最长的一条简单路径)的端点不是一个度数为 11 的点的路径不是最优的

  1. 树的直径有多条,那么所有直径都交汇于”树的中心”。 注意:这里的树的中心还没学,可以认为是一点

  2. 树上的任意点 ii,距离最远的点 xx 一定是树的直径的某个端点。

证明:

我们可以这么想:树的直径的某个端点一定是度数为 11 的点。 那么距离我们的点 ii 最远的点如果度数不为 11,就可以继续延伸,就不是距离最远的点了。 所以距离点 ii 最远的点也一定是度数为 11 的点,度数为 11 的点就是树的直径的端点了。

同时可以证明1,因为每一个点得有一个最远的点,再找最远的点是有可能不一样的

树的直径怎么求

第一种 Floyd

树的直径是两点之间最长的距离,直接 Floyd 求处两点之间的距离然后比大小找最大值就拿下了

第二种 dfs

同1的证明方法

模版

虽说只有这个有模版
C++
#include<bits/stdc++.h>
#define int long long
#define Code using
#define by namespace
#define cmy std
#define AK const int
#define problem signed
#define T1 main
Code by cmy;
AK N = 100005;
AK M = 100005;
AK INF = 1e18;
int v[N], ans = -INF, id;
struct node{
	int v, w;
};
vector<node> g[N];
void add_edge(int x, int y, int z){
	g[x].push_back({y, z});
	g[y].push_back({x, z});
}
void dfs(int st, int now){
	v[st] = 1;
	if(now >= ans){
		ans = now;
		id = st;
	}
	for(auto it : g[st]){
		if(v[it.v] == 0){
			dfs(it.v, now + it.w);
		}
	}
}
problem T1(){
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n, m;
	cin >> n;
	m = n - 1;
	for(int i = 1; i <= m; i++){
		int x, y, z;
		cin >> x >> y >> z;
		add_edge(x, y, z);
	}
	dfs(1, 0);
	fill(v + 1, v + 1 + n, 0);
	ans = 0;
	dfs(id, 0);
	cout << ans;
	return 0;
}

第三种 树形DP

思路

树的中心的最长链和次长链对应的端点,就是树的直径。 树的中心一定在树的直径上,那么我们找到一个离他最远的点,再找一个离他次远的点,那么这两个点的最长路,必然是最长的路径了,就是树的直径。

因为我们不知道树的中心怎么求,所以我们把每一个点都试一遍。

我们维护所有点 ii 的最长链 dis1idis1_i 和次长链 dis2idis2_i,取 maxmax,即为直径。

我们用树形DP维护这个 dis1dis1 和 dis2dis2

我自己写的时候都懵了

所以……

详(jian)细(lue)版(也叫做过程)

首先我们搬回树的直径的定义

  1. 树的直径不唯一。

  2. 树的直径的端点一定是一个度数为 11 的点(叶子 or 根)。

  3. 树的直径有多条,那么所有直径都交汇于”树的中心”。

  4. 树上的任意点 ii,距离最远的点 xx 一定是树的直径的某个端点。

6.我们说树的直径也可以是树的一个中心的不共边的最长链与次长链之和。

我们看哪个定理可以帮助我们推出这个思路

C++
树的中心的最长链和次长链对应的端点,就是树的直径。
树的中心一定在树的直径上,

那么我们找到一个离他最远的点,

再找一个离他次远的点,

那么这两个点的最长路,必然是最长的路径了,就是树的直径。

因为我们不知道树的中心怎么求,所以我们把每一个点都试一遍。

我们维护所有点 $i$ 的最长链 $dis1_i$

和次长链 $dis2_i$,取 $max$,即为直径。

我们用树形DP维护这个 $dis1$ 和 $dis2$

6.我们说树的直径也可以是树的一个中心的不共边的最长链与次长链之和。

这个可以

我们可以通过这棵树的子树的最长链来确定自己的最长链和次长链。

如果我们子树的最长链加上自己到这颗子树的边权比自己的最长链长,那么我们先把次长链更新为这个节点的最长链,然后再把最长链更新为新的最长链。

如果新的最长链只是比我们的次长链大,就只更新次长链。

最后,我们就枚举树的中心,找最长的最长链和次长链之和

那么代码就是

C++
#include<bits/stdc++.h>
#define int long long
#define Code using
#define by namespace
#define cmy std
#define AK const int
#define problem signed
#define T1 main
Code by cmy;
AK N = 100005;
AK M = 100005;
AK INF = 1e18;
int v[N], ans = -INF, id;
int dis_max[N], dis_second[N];
struct node{
	int v, w;
};
vector<node> g[N];
void add_edge(int x, int y, int z){
	g[x].push_back({y, z});
	g[y].push_back({x, z});
}
void dfs(int st, int fa){
	for(int i = 0; i < g[st].size(); i++){
		int nxt = g[st][i].v, w  = g[st][i].w;
		if(fa == nxt){
			continue;
		}
		dfs(nxt, st);
		if(dis_max[nxt] + w > dis_max[st]){
			dis_second[st] = dis_max[st];
			dis_max[st] = dis_max[nxt] + w;
		}else if(dis_max[nxt] + w > dis_second[st]){
			dis_second[st] = dis_max[nxt] + w;
		}
	}
	ans = max(ans, dis_max[st] + dis_second[st]);
	return ;
}
problem T1(){
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n, m;
	cin >> n >> m;
	for(int i = 1; i <= m; i++){
		int x, y, z;
		cin >> x >> y;
		z = 1;
		add_edge(x, y, z);
	}
	dfs(1, -1);
	cout << ans;
	return 0;
}

那么…

例题

B4016 树的直径

手切模板

two DFS

C++
#include<bits/stdc++.h>
#define int long long
#define Code using
#define by namespace
#define cmy std
#define AK const int
#define problem signed
#define T1 main
Code by cmy;
AK N = 100005;
AK M = 100005;
AK INF = 1e18;
int v[N], ans = -INF, id;
struct node{
	int v, w;
};
vector<node> g[N];
void add_edge(int x, int y, int z){
	g[x].push_back({y, z});
	g[y].push_back({x, z});
}
void dfs(int st, int now){
	v[st] = 1;
	if(now >= ans){
		ans = now;
		id = st;
	}
	for(auto it : g[st]){
		if(v[it.v] == 0){
			dfs(it.v, now + it.w);
		}
	}
}
problem T1(){
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n, m;
	cin >> n;
	m = n - 1;
	for(int i = 1; i <= m; i++){
		int x, y, z;
		cin >> x >> y >> z;
		add_edge(x, y, z);
	}
	dfs(1, 0);
	fill(v + 1, v + 1 + n, 0);
	ans = 0;
	dfs(id, 0);
	cout << ans;
	return 0;
}

树形DP

C++
#include<bits/stdc++.h>
#define int long long
#define Code using
#define by namespace
#define cmy std
#define AK const int
#define problem signed
#define T1 main
Code by cmy;
AK N = 100005;
AK M = 100005;
AK INF = 1e18;
int v[N], ans = -INF, id;
int dis_max[N], dis_second[N];
struct node{
	int v, w;
};
vector<node> g[N];
void add_edge(int x, int y, int z){
	g[x].push_back({y, z});
	g[y].push_back({x, z});
}
void dfs(int st, int fa){
	for(int i = 0; i < g[st].size(); i++){
		int nxt = g[st][i].v, w  = g[st][i].w;
		if(fa == nxt){
			continue;
		}
		dfs(nxt, st);
		if(dis_max[nxt] + w > dis_max[st]){
			dis_second[st] = dis_max[st];
			dis_max[st] = dis_max[nxt] + w;
		}else if(dis_max[nxt] + w > dis_second[st]){
			dis_second[st] = dis_max[nxt] + w;
		}
	}
	ans = max(ans, dis_max[st] + dis_second[st]);
	return ;
}
problem T1(){
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n, m;
	cin >> n >> m;
	for(int i = 1; i <= m; i++){
		int x, y, z;
		cin >> x >> y;
		z = 1;
		add_edge(x, y, z);
	}
	dfs(1, -1);
	cout << ans;
	return 0;
}
原文