Minimum Edge Weight Equilibrium Queries in a Tree - Problem

Imagine you're a network engineer tasked with optimizing data transmission paths in a tree-structured network. You have an undirected tree with n nodes labeled from 0 to n-1, where each edge has a specific weight representing transmission cost.

Given the tree structure through edges[i] = [ui, vi, wi] (indicating an edge between nodes ui and vi with weight wi), you need to answer multiple queries about path optimization.

For each query [ai, bi], you must find the minimum number of edge weight modifications needed to make all edges on the path from node ai to node bi have the same weight. You can change any edge's weight to any value in one operation.

Key Points:

  • Each query is independent - the tree resets after each query
  • The path between any two nodes in a tree is unique
  • You want to minimize operations, so keep the most frequent weight unchanged

Input & Output

example_1.py โ€” Basic Tree Path
$ Input: n = 7, edges = [[0,1,1],[1,2,1],[2,3,1],[3,4,2],[4,5,2],[5,6,2]], queries = [[0,3],[3,6]]
โ€บ Output: [0, 0]
๐Ÿ’ก Note: For query [0,3]: path has edges with weights [1,1,1] - all same, so 0 operations. For query [3,6]: path has edges with weights [2,2,2] - all same, so 0 operations.
example_2.py โ€” Mixed Weights
$ Input: n = 8, edges = [[1,2,6],[1,3,4],[2,4,6],[2,5,3],[3,6,6],[3,0,8],[6,7,2]], queries = [[4,6],[0,4],[1,7]]
โ€บ Output: [1, 2, 3]
๐Ÿ’ก Note: For [4,6]: path weights [6,4,6] - keep two 6's, change one 4. For [0,4]: path weights [8,4,6] - all different, keep any one. For [1,7]: path weights [4,6,2] - all different, change 2 operations.
example_3.py โ€” Single Node Query
$ Input: n = 3, edges = [[0,1,1],[1,2,2]], queries = [[0,0],[1,1]]
โ€บ Output: [0, 0]
๐Ÿ’ก Note: When source equals destination, the path has no edges, so 0 operations needed.

Visualization

Tap to expand
LCAABCDw=2w=1w=2w=2Query Path: C โ†’ DAnalysis:Path weights: [2, 2, 1, 2] (Cโ†’Aโ†’LCAโ†’Bโ†’D)Weight frequency: w=2 appears 3 times, w=1 appears 1 timeOptimal strategy: Keep weight 2 unchanged, modify 1 edge๐ŸŽฏ Result: 4 total edges - 3 max frequency = 1 operation needed
Understanding the Visualization
1
Build Tree Structure
Create adjacency list representation of the tree with weighted edges
2
Preprocess for Fast Queries
Use binary lifting to enable O(log n) LCA queries with weight frequency tracking
3
Find Path Weights
For each query, find LCA and combine weight frequencies from both paths
4
Calculate Minimum Operations
Keep the most frequent weight unchanged, modify all others
Key Takeaway
๐ŸŽฏ Key Insight: Use binary lifting for O(log n) LCA queries and keep the most frequent edge weight unchanged to minimize modifications

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n log n + m log n)

O(n log n) for preprocessing binary lifting, O(log n) per query for LCA

n
2n
โšก Linearithmic
Space Complexity
O(n log n)

Space for binary lifting table and weight frequency arrays at each level

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค n โ‰ค 104
  • edges.length == n - 1
  • edges[i].length == 3
  • 0 โ‰ค ui, vi โ‰ค n - 1
  • 1 โ‰ค wi โ‰ค 26
  • 1 โ‰ค queries.length โ‰ค 2 * 104
  • queries[i].length == 2
  • 0 โ‰ค ai, bi โ‰ค n - 1
Asked in
Google 45 Amazon 32 Meta 28 Microsoft 25
47.2K Views
Medium-High Frequency
~25 min Avg. Time
1.7K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen