Given a tree with N vertices and N-1 Edges. Let’s define a function F(a, b) which is equal to the minimum edge weight in the path between node a & b. The task is to calculate the product of all such F(a, b). Here a&b are unordered pairs and a!=b.
So, basically, we need to find the value of:
In the input, we will be given the value of N and then N-1 lines follow. Each line contains 3 integers u, v, w denoting edge between node u and v and it’s weight w. Since the product will be very large, output it modulo 10^9+7.
Input : N = 4 1 2 1 1 3 3 4 3 2 Output : 12 Given tree is: 1 (1)/ \(3) 2 3 \(2) 4 We will calculate the minimum edge weight between all the pairs: F(1, 2) = 1 F(2, 3) = 1 F(1, 3) = 3 F(2, 4) = 1 F(1, 4) = 2 F(3, 4) = 2 Product of all F(i, j) = 1*3*2*1*1*2 = 12 mod (10^9 +7) =12 Input : N = 5 1 2 1 1 3 3 4 3 2 1 5 4 Output : 288
If we observe carefully then we will see that if there is a set of nodes in which minimum edge weight is w and if we add a node to this set that connects the node with the whole set by an edge of weight W such that W<w then path formed between recently added node to all nodes present in the set will have minimum weight W.
So, here we can apply Disjoint-Set Union concept to solve the problem.
First, sort the data structure according to decreasing weight. Initially assign all nodes as a single set. Now when we merge two sets then do the following:-
Product=(present weight)^(size of set1*size of set2).
We will multiply this product value for all edges of the tree.
Below is the implementation of the above approach:
Time Complexity : O(N*logN)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Count number of paths whose weight is exactly X and has at-least one edge of weight M
- Remove all outgoing edges except edge with minimum weight
- Shortest Path in a weighted Graph where weight of an edge is 1 or 2
- Find weight of MST in a complete graph with edge-weights either 0 or 1
- Query to find the maximum and minimum weight between two nodes in the given tree using LCA.
- Count of all prime weight nodes between given nodes in the given Tree
- Find the weight of the minimum spanning tree
- Count number of times each Edge appears in all possible paths of a given Tree
- Maximum weighted edge in path between two nodes in an N-ary tree using binary lifting
- Path with smallest product of edges with weight >= 1
- Check if removing an edge can divide a Binary Tree in two halves
- Maximum edge removal from tree to make even forest
- Maximum Possible Edge Disjoint Spanning Tree From a Complete Graph
- Tree, Back, Edge and Cross Edges in DFS of Graph
- Queries to find the count of shortest paths in a Tree that contains a given edge
- 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
- Count the nodes in the given tree whose weight is even parity
Improved By : sanjeev2552