Given a connected undirected weighted graph with N nodes and M edges. The task is to perform given queries and find the weight of the minimum spanning tree. Queries are of three types:
- query(1) -> Find the weight of the minimum spanning tree.
- query(2, x, y) -> Change the weight of the edge between the nodes x and y to 0.
- query(3, x, y) -> Restore the weight of the edge between the nodes x and y to its original weight.
query(2, 1, 2),
query(3, 1, 2),
query(2, 3, 4),
Approach: Let’s first compute MST of the initial graph before performing any queries and let T be this MST. The crucial observation is that at any point while handling the queries, the weight of the MST of the current graph can be computed by running Kruskal’s algorithm on edges with zero weight at this point and edges of T. So, keep edges with weight zero in a data structure and after query of type 2 and type 3 compute weight of minimum spanning tree.
Below is the implementation of the above approach:
- Find the minimum spanning tree with alternating colored edges
- Applications of Minimum Spanning Tree Problem
- Boruvka's algorithm for Minimum Spanning Tree
- Prim’s Minimum Spanning Tree (MST) | Greedy Algo-5
- Kruskal's Minimum Spanning Tree using STL in C++
- Minimum Product Spanning Tree
- Reverse Delete Algorithm for Minimum Spanning Tree
- Minimum spanning tree cost of given Graphs
- Minimum Spanning Tree using Priority Queue and Array List
- Kruskal’s Minimum Spanning Tree Algorithm | Greedy Algo-2
- Query to find the maximum and minimum weight between two nodes in the given tree using LCA.
- Maximum Possible Edge Disjoint Spanning Tree From a Complete Graph
- Spanning Tree With Maximum Degree (Using Kruskal's Algorithm)
- Product of minimum edge weight between all pairs of a Tree
- Find minimum weight cycle in an undirected graph
- Queries to find the Minimum Weight from a Subtree of atmost D-distant Nodes from Node X
- Number of Paths of Weight W in a K-ary tree
- Count the nodes in the given tree whose weight is prime
- Count the nodes in the given tree whose weight is even
- Count the nodes in the given tree whose weight is a power of two
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : sanjeev2552