跳到正文

算法·理论

树的直径综合题

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

文章信息

收录
2025年12月12日
更新
2026年8月9日
篇幅
约 20 分钟

版本

  1. v1当前

上次课我们学习了树的直径的定义,一些定理和两种写法(还有一种Floyd的写法没什么用)。

其中一种DP(还是树形DP)写法其实并没有深入思考,只是浅浅的过了一遍思路,这次,我们就详(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;
}

P8602 [蓝桥杯 2013 省 A] 大臣的旅费

板子+路程总和

路程总和其实就是10 * 1 + (1) + 10 + (1 + 2)+ …\dots + 10 + (1 + 2 + …\dots + n)

也就是10n + (n + 1)n

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;
struct node{
	int v, w;
}; 
vector<node> g[N];
void add_edge(int x, int y, int w){
	g[x].push_back({y, w});
	g[y].push_back({x, w});
}
int n, m, id, ans, vis[N];
void dfs(int st, int sum){
	vis[st] = 1;
	if(sum > ans){
		id = st;
		ans = sum;
	}
	for(int i = 0; i < g[st].size(); i++){
		if(vis[g[st][i].v] == 0){
			dfs(g[st][i].v, sum + g[st][i].w);
		}
	}
	return ;
}
problem T1(){
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin >> n;
	m = n - 1;
	for(int i = 1; i <= m; i++){
		int x, y, w;
		cin >> x >> y >> w;
		add_edge(x, y, w);
	}
	dfs(1, 0);
	fill(vis + 1, vis + 1 + n, 0);
	dfs(id, 0);
	cout << ans * 10 + ans * (ans + 1) / 2;
	return 0;
}

Computer

这道题第一眼看上去O(n ^ 2)暴力,但你交上去就会发现TLE摆在你面前。

所以我们再找找树的直径的特性里有没有线索。

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

我们可以找到任意一条直径的两个端点,然后我们看他和直径的哪两个端点近。

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 disx[N], disy[N], 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);
		}
	}
} 
void dfs1(int st, int now){
//	cout << st << " ";
	v[st] = 1;
	disx[st] = now;
	for(auto it : g[st]){
		if(v[it.v] == 0){
			dfs1(it.v, now + it.w);
		}
	}
} 
void dfs2(int st, int now){
	v[st] = 1;
	disy[st] = now;
	for(auto it : g[st]){
		if(v[it.v] == 0){
			dfs2(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;
	while(cin >> n){
		ans = 0;
		fill(disx + 1, disx + 1 + n, 0);
		fill(disy + 1, disy + 1 + n, 0);
		for(int i = 1; i <= n; i++){
			g[i].clear(); 
		}
		for(int i = 2; i <= n; i++){
			int y, z;
			cin >> y >> z;
			add_edge(y, i, z);
		}
		fill(v + 1, v + 1 + n, 0);
		dfs(1, 0);
		fill(v + 1, v + 1 + n, 0);
		int now = id;
		dfs(now, 0);
		fill(v + 1, v + 1 + n, 0);
		int now2 = id;
		dfs1(now, 0);
		fill(v + 1, v + 1 + n, 0);	
		dfs2(now2, 0);
//		cout << now << " " << now2 << " ";
		for(int i = 1; i <= n; i++){
			cout << max(disx[i], disy[i]) << "\n";
		}
	}
	return 0;
}
/*
5
1 1
2 1
3 1
1 1
^Z
*/

Anton and Tree

这道题是一个树的直径的题。

为什么?

我们把连通块缩成一个点后,一个点直接连接的所有点和这个点都是不同颜色的,那么我们想染色,就要把这个点和周围的染成一个颜色。

染色一次合并一次连通块。

所以我们每次染色可以减少一层,左边一部分右边一部分。

为了利益最大化,我们会选择可以让两边都缩成一个连通块,也就是减少两边的树的长度的点来染色。

那么答案就出来了:缩点后的树的直径÷2缩点后的树的直径 \div 2

Tree Tag

这题看着像一个博弈论,但是实际根本不用。

首先,如果 A 和 B 之间的距离小于等于 dada,说明 A 一步就可以抓到,A 必胜。

然后,B走到某个点后一定会折返,因为 A 把 B 逼到角落了,B 只能穿过 A 折返来逃跑,但是跑出去的距离一定要大于 dada,不然一跑出去就被抓了。

还有一种情况 A 可以抓到 B,就是 A 可以直接蹲在树的中心,如果 da≥⌈直径2⌉da \ge \lceil \frac{直径}{2} \rceil,那么 B 就无论躲在哪里,A 只要往中心一站,都可以抓到。

那么,如果以上几种情况都不能满足,B 就可以和 A 一直周旋,所以 B 赢。

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, dis[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 now){
	v[st] = 1;
	if(now >= ans){
		ans = now;
		id = st;
	}
	for(int i = 0; i < g[st].size(); i++){
		node it = g[st][i];
		if(v[it.v] == 0){
			dis[it.v] = dis[st] + it.w;
			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 t;
	cin >> t;
	while(t--){
		int n, m, a, b, da, db;
		cin >> n >> a >> b >> da >> db;
		for(int i = 1; i <= n; i++){
			g[i].clear();
			v[i] = 0;
			dis[i] = 0;
		}
		for(int i = 1; i < n; i++){
			int x, y, z;
			cin >> x >> y;
			z = 1;
			add_edge(x, y, z);
		}
		fill(v + 1, v + 1 + n, 0);
		ans = 0;
		dfs(a, 0);
		int ss = dis[b];
		fill(v + 1, v + 1 + n, 0);
		ans = 0;
		dfs(id, 0);
		if(da >= ceil(ans / 2.0) || da >= ss || da * 2 >= db){
			cout << "Alice\n";
			continue;
		}
		cout << "Bob\n";
	}
	return 0;
}

P3174 [HAOI2009] 毛毛虫

抽

象

毛毛虫:你可以看作一条链,上面挂了很多直接边,也就是长度为 11 的链。

所以他问我们最大的毛毛虫是多少。

所以我们只用把求树的直径的过程稍微改动一捏捏,就拿下了!

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 = 1000005;
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 + g[st].size() - 1);
		}
	}
}
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 = 1;
		cin >> x >> y;
		add_edge(x, y, z);
	}
	dfs(1, g[1].size());
	fill(v + 1, v + 1 + n, 0);
	dfs(id, g[id].size());
	cout << ans + 1;
	return 0;
}
原文