Motivation
MST, or Minimum Spanning Tree, is an acyclic subgraph of a graph, where the total weight of its edges is minimal. Their properties are often used in problems with edge-weighted graphs. But what about node-weighted graphs? A problem structured on edges can often be modified to one based on nodes. In such situations, data structures and algorithms analogous to those used for edges can be applied. Let's look at one such problem!
The Minimax problem
The minimax path problem involves finding the minimum of the maximum edge weights among all possible paths between two vertices, $$$i$$$ and $$$j$$$. And it turns out that, the minimax path always is a part of the MST!
Hence the solution involves finding the MST of the graph, and because trees only have one simple path between two vertices, either through DFS or through precomputation answering the query of $$$i$$$ $$$j$$$ in $$$O(n)$$$ or $$$O(\log(n))$$$ time.
Vertex Minimax problem
This is similar to the minimax problem, but instead of minimizing the maximum weighted edge along all paths, this deals in minimizing the maximum weighted node along all paths between two vertices. Let's see how our node analogy for MST deals this problem for us!
The Algorithm
In the original problem, we constructed a tree that had filtered out all the non-optimal paths between every pair of vertices leaving only the ones we want. Let's try to recreate the same! In our case "optimal" path would be one whose heaviest node is minimized.
Multiple paths between two vertices arise in the presence of cycles. So making a tree from a graph comes down to choosing which edge to cut in each cycle, hence let's observe a cycle in detail.
The Strategy
In a cycle, there are two simple paths between each pair of vertices and out of the two, the one with the heavier maximum weighted node, will always pass through the heaviest node in the cycle. Hence, we must cut any one of the two edges connected to the heaviest node in each cycle!
1851G - Vlad and the Mountains
I would urge you to try to solve it assuming the queries are online and relate to what we have learnt.
Here is my submission link for reference : 217042760
Note
The editorial's solution involves offline queries, do check that out too!
Conclusion
I don't know if this was common knowledge and couldn't find anything like it in my limited search on the net, but seemed like an interesting find :)
Please let me know if there are other problems that can be solved using this, I will link them in the blog!
UPD — 1
vgtcross mentions an elegant alternative approach in the following comment. This also aids in the understanding of the algorithm for vertex MST as the rules of adding an edge when seen through the lens of this approach makes the algorithm identical to the kruskal's algorithm for MST!
Yeet Sir orz. Another problem on the same trick is 609E - Minimum spanning tree for each edge.
Hey from what I understand, this uses the original MST with LCA right? I don't see how it uses the vertex MST.
You are right. I just remembered LCA and MST and thought that this problem was related.
In Case of equal weights of a.first and b.first....do we have to sort by weight[a.second]<weight[b.second]
If the weights of nodes are equal then order of processing doesn't matter as if there exists a cycle with two vertices of maximum weight, then both paths for each pair of vertices in the cycle will have the same cost!
In the question mentioned above by you...lets take the first testcase first example...how will you decide between adding (3,2) aur (3,6) as adding (3,2) will affect the answer...so that why i used the second node for sorting
Actually it wouldn't, as if you see my code we add an edge only if the second vertex has been visited already i.e has a lower weight than the first vertex. Now if both 2 and 6 have weights lower than 3, then it wouldn't matter if we process 3-2 first or 3-6 since 3 is the heavy vertex.
I believe there exist a much simpler way to think about this:
We know that building the MST for an edge-weighted graph is easy. But we have a vertex-weighted graph. Can we convert this into an edge-weighted graph?
Consider an edge $$$(u, v)$$$ connecting two vertices with weights $$$w_u$$$ and $$$w_v$$$. If this edge is used in our path, our path must contain both vertices $$$u$$$ and $$$v$$$. This edge causes the path to have a maximum vertex value of at least $$$\max(w_u, w_v)$$$. Thus, we can just set the weight of this edge equal to $$$\max(w_u, w_v)$$$. Do the same for all edges, and just run your standard MST algorithm.
I didn't think of this, It's very elegant ! Thanks a lot for sharing this!
ya really cool...upvoted...maybe u can link this comment in your blog
Really Nice Problem....did it using hld on tree(made by using above mentioned concept)217554715...thanks for sharing these...learned something new as well as interesting