2846. Minimum Edge Weight Equilibrium Queries in a Tree
There is an undirected tree with
n
nodes labeled from0
ton - 1
. You are given the integern
and a 2D integer arrayedges
of lengthn - 1
, whereedges[i] = [ui, vi, wi]
indicates that there is an edge between nodesui
andvi
with weightwi
in the tree.You are also given a 2D integer array
queries
of lengthm
, wherequeries[i] = [ai, bi]
. For each query, find the minimum number of operations required to make the weight of every edge on the path fromai
tobi
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
ai
tobi
is a sequence of distinct nodes starting with nodeai
and ending with nodebi
such that every two adjacent nodes in the sequence share an edge in the tree.Return an array
answer
of lengthm
whereanswer[i]
is the answer to theith
query.
c++
1 |
|