Class Solution
-
- All Implemented Interfaces:
public final class Solution2846 - Minimum Edge Weight Equilibrium Queries in a Tree\.
Hard
There is an undirected tree with
nnodes labeled from0ton - 1. You are given the integernand a 2D integer arrayedgesof lengthn - 1, where <code>edgesi = u<sub>i</sub>, v<sub>i</sub>, w<sub>i</sub></code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> with weight <code>w<sub>i</sub></code> in the tree.You are also given a 2D integer array
queriesof lengthm, where <code>queriesi = a<sub>i</sub>, b<sub>i</sub></code>. For each query, find the minimum number of operations required to make the weight of every edge on the path from <code>a<sub>i</sub></code> to <code>b<sub>i</sub></code> equal. In one operation, you can choose any edge of the tree and change its weight to any value.Note that:
Queries are independent of each other, meaning that the tree returns to its initial state on each new query.
The path from <code>a<sub>i</sub></code> to <code>b<sub>i</sub></code> is a sequence of distinct nodes starting with node <code>a<sub>i</sub></code> and ending with node <code>b<sub>i</sub></code> such that every two adjacent nodes in the sequence share an edge in the tree.
Return an array
answerof lengthmwhereanswer[i]is the answer to the <code>i<sup>th</sup></code> query.Example 1:
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,2,6,0,6]
Output: 0,0,1,3
Explanation: In the first query, all the edges in the path from 0 to 3 have a weight of 1. Hence, the answer is 0.
In the second query, all the edges in the path from 3 to 6 have a weight of 2. Hence, the answer is 0. In the third query, we change the weight of edge 2,3 to 2. After this operation, all the edges in the path from 2 to 6 have a weight of 2. Hence, the answer is 1.
In the fourth query, we change the weights of edges 0,1, 1,2 and 2,3 to 2. After these operations, all the edges in the path from 0 to 6 have a weight of 2. Hence, the answer is 3. For each queriesi, it can be shown that answeri is the minimum number of operations needed to equalize all the edge weights in the path from a<sub>i</sub> to b<sub>i</sub>.
Example 2:
Input: n = 8, edges = \[\[1,2,6],1,3,4,2,4,6,2,5,3,3,6,6,3,0,8,7,0,2], queries = \[\[4,6],0,4,6,5,7,4]
Output: 1,2,2,3
Explanation: In the first query, we change the weight of edge 1,3 to 6. After this operation, all the edges in the path from 4 to 6 have a weight of 6. Hence, the answer is 1.
In the second query, we change the weight of edges 0,3 and 3,1 to 6. After these operations, all the edges in the path from 0 to 4 have a weight of 6. Hence, the answer is 2.
In the third query, we change the weight of edges 1,3 and 5,2 to 6. After these operations, all the edges in the path from 6 to 5 have a weight of 6. Hence, the answer is 2.
In the fourth query, we change the weights of edges 0,7, 0,3 and 1,3 to 6. After these operations, all the edges in the path from 7 to 4 have a weight of 6. Hence, the answer is 3. For each queriesi, it can be shown that answeri is the minimum number of operations needed to equalize all the edge weights in the path from a<sub>i</sub> to b<sub>i</sub>.
Constraints:
<code>1 <= n <= 10<sup>4</sup></code>
edges.length == n - 1edges[i].length == 3<code>0 <= u<sub>i</sub>, v<sub>i</sub>< n</code>
<code>1 <= w<sub>i</sub><= 26</code>
The input is generated such that
edgesrepresents a valid tree.<code>1 <= queries.length == m <= 2 * 10<sup>4</sup></code>
queries[i].length == 2<code>0 <= a<sub>i</sub>, b<sub>i</sub>< n</code>
-
-
Constructor Summary
Constructors Constructor Description Solution()
-