Minimum Weighted Subgraph With the Required Paths II Problem
Description
LeetCode Problem 3553.
You are given an undirected weighted tree with n nodes, numbered from 0 to n - 1. It is represented by a 2D integer array edges of length n - 1, where edges[i] = [u_i, v_i, w_i] indicates that there is an edge between nodes u_i and v_i with weight w_i.
Additionally, you are given a 2D integer array queries, where queries[j] = [src1_j, src2_j, dest_j].
Return an array answer of length equal to queries.length, where answer[j] is the minimum total weight of a subtree such that it is possible to reach dest_j from both src1_j and src2_j using edges in this subtree.
A subtree here is any connected subset of nodes and edges of the original tree forming a valid tree.
Example 1:
Input: edges = [[0,1,2],[1,2,3],[1,3,5],[1,4,4],[2,5,6]], queries = [[2,3,4],[0,2,5]]
Output: [12,11]
Explanation:
The blue edges represent one of the subtrees that yield the optimal answer.

- answer[0]: The total weight of the selected subtree that ensures a path from src1 = 2 and src2 = 3 to dest = 4 is 3 + 5 + 4 = 12.
- answer[1]: The total weight of the selected subtree that ensures a path from src1 = 0 and src2 = 2 to dest = 5 is 2 + 3 + 6 = 11.
Example 2:
Input: edges = [[1,0,8],[0,2,7]], queries = [[0,1,2]]
Output: [15]
Explanation:

- answer[0]: The total weight of the selected subtree that ensures a path from src1 = 0 and src2 = 1 to dest = 2 is 8 + 7 = 15.
Constraints:
3 <= n <= 10^5edges.length == n - 1edges[i].length == 30 <= u_i, v_i < n1 <= w_i <= 10^41 <= queries.length <= 10^5queries[j].length == 30 <= src1_j, src2_j, dest_j < nsrc1_j,src2_j, anddest_jare pairwise distinct.- The input is generated such that
edgesrepresents a valid tree.
Solution
Approach: Distances with LCA
In a tree the cheapest subtree that lets dest be reached from both src1 and src2 is the union of the paths between the three nodes. Each edge of that union lies on exactly two of the three pairwise paths, so its weight is
(dist(a, b) + dist(a, c) + dist(b, c)) / 2.
Every query therefore needs three distances, and dist(u, v) = d[u] + d[v] - 2 * d[lca(u, v)] where d is the distance from the root. Compute d with one iterative DFS, recording each node's preorder index. For u != v with tin[u] < tin[v], the LCA is the smallest-index parent among the preorder positions tin[u] + 1 .. tin[v], so a sparse table of range minimums answers each LCA in O(1).
Complexity: O((n + q) log n) time (building the sparse table dominates, each query is O(1)) and O(n log n) extra space.
class Solution:
def minimumWeight(self, edges: List[List[int]], queries: List[List[int]]) -> List[int]:
n = len(edges) + 1
g = [[] for _ in range(n)]
for u, v, w in edges:
g[u].append((v, w))
g[v].append((u, w))
# iterative DFS: preorder index, parent, distance from the root
tin = [0] * n
dist = [0] * n
parent = [-1] * n
order = []
stack = [0]
while stack:
u = stack.pop()
tin[u] = len(order)
order.append(u)
for v, w in g[u]:
if v != parent[u]:
parent[v] = u
dist[v] = dist[u] + w
stack.append(v)
# LCA of u != v with tin[u] < tin[v]: the smallest parent tin among preorder positions tin[u]+1..tin[v]
base = [tin[parent[x]] if parent[x] >= 0 else 0 for x in order]
st = [base]
j = 1
while (1 << j) <= n:
prev = st[-1]
half = 1 << (j - 1)
st.append([min(prev[i], prev[i + half]) for i in range(n - (1 << j) + 1)])
j += 1
def lca(u, v):
if u == v:
return u
l, r = tin[u], tin[v]
if l > r:
l, r = r, l
l += 1
b = (r - l + 1).bit_length() - 1
row = st[b]
return order[min(row[l], row[r - (1 << b) + 1])]
def d(u, v):
return dist[u] + dist[v] - 2 * dist[lca(u, v)]
return [(d(a, b) + d(a, c) + d(b, c)) // 2 for a, b, c in queries]