Chandu and his Gurudevs
Practice
4 (636 votes)
Medium
Graph theory
Sorting
Ready
Approved
Disjoint set
Problem
62% Success 1975 Attempts 30 Points 1s Time Limit 256MB Memory 1024 KB Max Code

Chandu is a problem setter at HackerEarth. He used to call all his seniors, Gurudev. Once Gurudevs realized Chandu is getting lazy, and they asked to him to set a problem. So he came up with a problem. He explained the problem to Gurudevs.

Given a undirected graph G with N nodes. The graph G has 2 properties:

  1. G is connected
  2. G has N-1 edges

Then he defined a function F such that F(a, b) is equal to the maximum edge weight in the path from a to b.

Now the problem is quite simple. We need to find the sum of the F(a, b) for all a, b such that 1 <= a, b <= N (mod 109 + 7).

As we all know that Chandu is lazy. Chandu submitted the problem but he did not submit the solution of the problem. So he asked you to write the solution for him.

Input:
First line contains an integer T, number of test cases.
First line of each test case consists of an integer N, number of nodes.
Next N-1 lines follows. ith line contains three space separated integers ai, bi and ci which denotes that there is an edge from ai to bi with edge weight ci.

Output:
For each test case print a single integer that is equal to the sum of the F(a, b) for all a, b such that 1 <= a, b <= N (mod 109 + 7).

Constraints:
1 <= T <= 105
1 <= N <= 2 * 105
0 <= ci <= 108
1 <= ai, bi <= N
sum of N over all the testcases will be <= 2 * 106

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:20
13 votes
Tags:
Linear SearchImplementationAlgorithmsGreedy algorithm
Points:20
111 votes
Tags:
Ad-HocApprovedEasyOpenString Manipulation
Points:30
3 votes
Tags:
MathematicsMediumOpenApprovedProbability and Statistics