本文共 2355 字,大约阅读时间需要 7 分钟。
一棵树型无向带权图的次大直径是指所有两点间距离中的第二长,包括相同长度的路径。为了求解次大直径,我们可以参考以下步骤:
首先,使用广度优先搜索(BFS)从任意节点出发,找到最远的节点u。接着,再次从u出发,进行BFS找到最远的节点v。这样,u和v就是直径的两个端点。
从u出发,排除v,进行BFS,找到最远的节点a。同样地,从v出发,排除u,进行BFS,找到最远的节点b。次大直径即为a到u的距离与b到v的距离中的较大者。
根据树的性质,任何最长路径都必然连接直径的两个端点。因此,从任意节点出发的最远点必定是直径的一个端点。
import java.util.*;public class Solution { private long res; public long getSecondDiameter(int[][] edge) { int n = edge.length + 1; Map > graph = buildGraph(edge); int far1 = bfs(0, graph, -1, new boolean[n]); int far2 = bfs(far1, graph, -1, new boolean[n]); bfs(far1, graph, far2, new boolean[n]); bfs(far2, graph, far1, new boolean[n]); return res; } private int bfs(int v, Map > graph, int u, boolean[] visited) { Queue queue = new LinkedList<>(); queue.offer(new long[]{v, 0}); visited[v] = true; int farV = v; long farDis = 0; while (!queue.isEmpty()) { int size = queue.size(); for (int i = 0; i < size; i++) { long[] cur = queue.poll(); int nextV = (int) cur[0]; if (graph.containsKey(nextV)) { for (int[] next : graph.get(nextV)) { if (next[0] != u && !visited[next[0]]) { if (next[1] + cur[1] > farDis) { farDis = next[1] + cur[1]; farV = next[0]; } visited[next[0]] = true; queue.offer(new long[]{next[0], next[1] + cur[1]}); } } } } } if (u != -1) { res = Math.max(res, farDis); } return farV; } private Map > buildGraph(int[][] edges) { Map > graph = new HashMap<>(); for (int[] edge : edges) { graph.putIfAbsent(edge[0], new ArrayList<>()); graph.get(edge[0]).add(new int[]{edge[1], edge[2]}); graph.putIfAbsent(edge[1], new ArrayList<>()); graph.get(edge[1]).add(new int[]{edge[0], edge[2]}); } return graph; }} 该算法通过两次BFS找到直径,再从直径端点出发计算次大直径,确保了在树结构下的高效计算。
转载地址:http://adjs.baihongyu.com/