求树的直径的三种姿态
转:https://www.cnblogs.com/ywjblog/p/9254997.html
树的直径
给定一棵树,树中每条边都有一个权值,树中两点之间的距离定义为连接两点的路径边权之和。树中最远的两个节点之间的距离被称为树的直径,连接这两点的路径被称为树的最长链。后者通常也可称为直径,即直径是一个
树形DP求树的直径
设1号节点为根,"N个点N-1条边的无向图"就可以看做“有根树”
设d[x]表示从节点x出发走向以x为根的子树,能够到达的最远节点的距离。设x的子节点为y1,y2, y3, ..., yt,edge(x, y)表示边权,显然有"
d[x] = max{d[yi] + edge(x, yi)}(1 <= i <= t)
接下来,我们可以考虑对每个节点x求出"经过节点x的最长链的长度"f[x],整棵树的直径就是max{f[x]}(1 <= x <= n)
对于x的任意两个节点yi和yj,"经过节点x的最长链长度"可以通过四个部分构成:从yi到yi子树中的最远距离,边(x, yi),边(x, yj),从yj到yj子树中的最远距离。设j < i,因此:
f[x] = max{d[yi] + d[yj] + edge(x, yi) + edge(x, yj)}(1 <= j < i <= t)
但是我们没有必要使用两层循环来枚举i, j。在计算d[x]的霍城,子节点的循环将要枚举到i时d[x]恰好就保存了从节点x出发走向“以yj(j < i)为根的子树”,能够到达的最远节点的距离,这个距离就是max{d[yi] +edge(x, yi)}(1
<= j < i)。所以我们先用d[x] + d[yi] + edge(x, yi)更新f[x],再用d[yi] + edge(x, yi)更新d[x]即可
void dp(int x) { v[x] = 1; for(int i = head[x]; i; i = net[i]) { int y = ver[i]; if(v[y]) continue; dp(y); ans = max(ans, d[x] + d[y] + edge[i]); d[x] = max(d[x], d[y] + edge[i]); } }
两次BFS(DFS)求树的直径
通过两次BFS或者两次DFS也可以求树的直径,并且更容易计算出直径上的具体节点
详细地说,这个做法包含两步:
1.从任意节点出发,通过BFS和DFS对树进行一次遍历,求出与出发点距离最远的节点记为p
2.从节点p出发,通过BFS或DFS再进行一次遍历,求出与p距离最远的节点,记为q。
从p到q的路径就是树的一条直径。因为p一定是直径的一端,否则总能找到一条更长的链,与直径的定义矛盾。显然地脑洞一下即可。p为直径的一端,那么自然的,与p最远的q就是直径的另一端。
在第2步的遍历中,可以记录下来每个点第一次被访问的前驱节点。最后从q递归到p,即可得到直径的具体方案
DFS
#include<bits/stdc++.h> using namespace std; const int maxn = 100086; struct picture { int y, v, net; int pre; }e[maxn]; int lin[maxn], len = 0; int n, m, dis[maxn]; bool vis[maxn]; int start, end; inline int read() { int x = 0, y = 1; char ch = getchar(); while(!isdigit(ch)) { if(ch == '-') y = -1; ch = getchar(); } while(isdigit(ch)) { x = (x << 1) + (x << 3) + ch - '0'; ch = getchar(); } return x * y; } inline void insert(int xx, int yy, int vv) { e[++len].y = yy; e[len].v = vv; e[len].net = lin[xx]; e[len].pre = xx; lin[xx] = len; } void dfs(int st) { vis[st] = 1; for(int i = lin[st]; i; i = e[i].net) { int to = e[i].y; if(!vis[to]) { dis[to] = dis[st] + e[i].v; dfs(to); } } } int main() { memset(vis, 0, sizeof(vis)); memset(dis, 0x3f3f3f, sizeof(dis)); n = read(), m = read(); for(int i = 1; i <= m; ++i) { int x, y, v; x = read(), y = read(), v = read(); insert(x, y, v); insert(y, x, v); } dis[1] = 0; dfs(1); int maxx = -1000; for(int i = 1; i <= n; ++i) if(dis[i] > maxx && dis[i] != 1061109567) { maxx = dis[i]; start = i; } cout << maxx << ' ' << start << '\n'; memset(vis, 0, sizeof(vis)); memset(dis, 0x3f3f3f,sizeof(dis)); dis[start] = 0; dfs(start); maxx = -1000; for(int i = 1; i <= n; ++i) if(dis[i] > maxx && dis[i] != 1061109567) { maxx = dis[i]; end = i; } cout << start << ' ' << maxx << ' ' << end << '\n'; return 0; } DFS求树的直径
BFS
#include<bits/stdc++.h> #define ll long long using namespace std; const int maxn = 100086; const ll inf = 1061109567; struct picture { int y, net, v; int pre; }e[maxn]; int n, m; int lin[maxn], len = 0; int dis[maxn]; int q[maxn], head = 0, tail = 0; int start, end; bool vis[maxn]; inline int read() { int x = 0, y = 1; char ch = getchar(); while(!isdigit(ch)) { if(ch == '-') y = -1; ch = getchar(); } while(isdigit(ch)) { x = (x << 1) + (x << 3) + ch - '0'; ch = getchar(); } return x * y; } inline void insert(int xx, int yy, int vv) { e[++len].pre = xx; e[len].y = yy; e[len].v = vv; e[len].net = lin[xx]; lin[xx] = len; } inline void bfs(int st) { head = tail = 0; vis[st] = 1; q[++tail] = st; while(head < tail) { //cout << head << '\n'; for(int i = lin[q[++head]]; i; i = e[i].net) { int to = e[i].y; if(!vis[to]) { dis[to] = dis[q[head]] + e[i].v; vis[to] = 1; q[++tail] = to; } } } } int main() { n = read(), m = read(); for(int i = 1; i <= m; ++i) { int x, y, v; x = read(), y = read(), v = read(); insert(x, y, v); insert(y, x, v); } memset(dis, 0x3f3f3f, sizeof(dis)); memset(vis, 0, sizeof(vis)); dis[1] = 0; bfs(1); int maxx = -1000; for(int i = 1; i <= n; ++i) if(dis[i] > maxx && dis[i] != inf) { start = i; maxx = dis[i]; } cout << maxx << ' ' << start << '\n'; memset(dis, 0x3f3f3f, sizeof(dis)); memset(vis, 0, sizeof(vis)); dis[start] = 0; bfs(start); maxx = -1000; for(int i = 1; i <= n; ++i) if(dis[i] > maxx && dis[i] != inf) { end = i; maxx = dis[i]; } cout << start << ' ' << maxx << ' ' << end << '\n'; return 0; } BFS求树的直径