树学杂谈1: 树的直径
关于树的直径
连接树上相距最远的两点的路径被称作这棵树的最长链。树的直径指这条最长链或者它的长度。
树的直径的求法
一般来将我们可以使用两种方法求数的直径,它们的时间复杂度都是
两次搜索
我们可以通过两次 DFS 或两次 BFS 来求树的直径,其原理非常简单:从任意一个节点
#define des edge[i].destinationVertex
inline void BFS(int x, int secondTime){
Reset();
queue<int> q;
q.push(x);
vertex[x].vis = true;
while(!q.empty()){
int cur = q.front();
q.pop();
for(int i=head[cur];i;i=edge[i].nextEdge){
if(!vertex[des].vis){
vertex[des].vis = true;
vertex[des].depth = vertex[cur].depth + edge[i].value;
q.push(des);
if(secondTime) vertex[des].father = cur;
}
}
}
for(int i=1;i<=n;i++)
if(treeLength < vertex[i].depth){
treeLength = vertex[i].depth;
deepest = i;
}
}
不同于接下来只能统计直径长度的树形 DP,在第二次搜索中,我们可以记录每一个点第一次被访问时的父亲节点,最后从搜索到的
树形 DP
设
对于 x 的任意两子节点
事实上我们没有必要使用
#define des edge[i].destinationVertex
inline void DP(int x){
vertex[x].vis = true;
for(int i=head[x];i;i=edge[i].nextEdge){
if(vertex[des].vis) continue;
DP(des);
treeLength = max(treeLength, vertex[x].depth + vertex[des].depth + edge[i].value);
vertex[x].depth = max(vertex[x].depth, vertex[des].depth + edge[i].value);
}
}
DP 方法的码量明显小于搜索方法,但是缺点也很明显,由于只关注状态的转移,DP 方法只能求出直径的长度,由于在树形图上搜索的时间复杂度也是
例题
一道我个人感觉挺好的题,这里当作例题来做:P3629 巡逻
题目大意
给定一棵 n 个节点的树,树上的每条边边权都为 1。我们规定“巡逻距离”是从 1 号点出发将树上所有遍历至少一次再回到起点所走过的边权和。现在在树上添加 1 或 2 条边,规定添加的边必须且只能走一次,求最短的巡逻距离。
思路
在还没有添加任何边的时候,巡逻距离显然是
添加一条边的时候也很简单,因为添加一条边相当于在图上添加了一个环,由于每条边都要巡逻到,我们要把这个环完整地走一遍。对于题目中给出的样例树,我们添加一条
所以添加一条边的时候,我们只需要求出树的直径长度
添加两条边时,我们按照上面的思路添加好第一条边,重点处理第二条边。添加第二条边就又增加了一个环,如果这个环与第一个环毫不相干,那么只需要按照上面的方法计算第二个环节约的路径长度。
但是当第二个环与第一个环有重合的边时,事情就没有那么简单了。对于题目中给出的样例树,再添加一条
我们可以认为添加一条边时求出的直径是“要被减少的距离”,因为有直径长度的边被少走了一次。现在添加第二条边时有些边少走的那一次被加回来了,我们可以将第一次求出的直径上的所有边权取负,这样在走第二个环的时候走上这些重合的边,将会在答案中减去这条边的边权,即“不仅没有再减少距离,反而还让本来被减少的边又被走了”。
再修改后的树上求直径添加第二条边,这个题就基本做完了。同添加第一条边一样,节省的路径长度是
回过头来看,第一次我们不仅需要求出树直径的长度,而且还需要求出直径的具体方案,因此使用两次搜索的方法;第二次只需要求出直径的长度,且因为修改后出现了负权边,所以要使用树形 DP 的方法求直径 —— 一次就把两种方法练了,这难道不是一道很妙的题吗?
Code
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<map>
#include<queue>
#define des edge[i].destinationVertex
using namespace std;
const int MaxN = 100005;
struct Edge{
int destinationVertex, nextEdge, value;
}edge[MaxN * 2];
struct Vertex{
int depth, father;
bool vis;
}vertex[MaxN];
int head[MaxN], tot, n, k, deepest, treeLength;
bool vis[MaxN];
inline void AddEdge(int u, int v){
tot++;
edge[tot].destinationVertex = v;
edge[tot].nextEdge = head[u];
edge[tot].value = 1;
head[u] = tot;
}
inline void Reset(){
for(int i=1;i<=n;i++){
vertex[i].depth = 0;
vertex[i].vis = false;
}
}
inline void BFS(int x, int secondTime){
Reset();
queue<int> q;
q.push(x);
vertex[x].vis = true;
while(!q.empty()){
int cur = q.front();
q.pop();
for(int i=head[cur];i;i=edge[i].nextEdge){
if(!vertex[des].vis){
vertex[des].vis = true;
vertex[des].depth = vertex[cur].depth + edge[i].value;
q.push(des);
if(secondTime) vertex[des].father = cur;
}
}
}
for(int i=1;i<=n;i++)
if(treeLength < vertex[i].depth){
treeLength = vertex[i].depth;
deepest = i;
}
}
inline void Change(int x){
while(vertex[x].father){
int fa = vertex[x].father;
for(int i=head[fa];i;i=edge[i].nextEdge){
if(des == x){
edge[i].value = -1;
break;
}
}
for(int i=head[x];i;i=edge[i].nextEdge){
if(des == fa){
edge[i].value = -1;
break;
}
}
x = fa;
}
}
inline void DP(int x){
vertex[x].vis = true;
for(int i=head[x];i;i=edge[i].nextEdge){
if(vertex[des].vis) continue;
DP(des);
treeLength = max(treeLength, vertex[x].depth + vertex[des].depth + edge[i].value);
vertex[x].depth = max(vertex[x].depth, vertex[des].depth + edge[i].value);
}
}
int main(){
scanf("%d%d",&n,&k);
for(int i=1;i<n;i++){
int u, v;
scanf("%d%d",&u,&v);
AddEdge(u,v), AddEdge(v,u);
}
if(k == 1){
BFS(1, 0);
BFS(deepest, 0);
printf("%d", 2 * n - vertex[deepest].depth - 1);
}
else{
BFS(1, 0);
BFS(deepest, 1);
int firstLength = vertex[deepest].depth;
Change(deepest);
Reset();
treeLength = 0;
DP(1);
printf("%d", 2 * n - treeLength - firstLength);
}
return 0;
}