Explorer's Birthday
Practice
4.7 (7 votes)
Algorithms
Graphs
Medium
Problem
21% Success 982 Attempts 30 Points 2s Time Limit 256MB Memory 1024 KB Max Code

Today is Explorer's birthday, and two of his best friends Raful and Jambo gave him a tree with N vertices and \(N-1\) edges. After playing with it for a while, Explorer got bored, so Raful and Jambo gave him a task:

find the answer A modulo \(10^9 + 7\) - sum of all functions \(f(u, v)\) for all unordered pairs of vertices \((u, v)\) such that \(u != v\). The function \(f(u ,v)\) is the product of the weights of the edges on the shortest path between u and v. Note that pairs \((u, v)\) and \((v, u)\) are considered the same, so must only be counted once.

He easily solved it. Can you (the best programmer in Lalalandia) do it too?

Input format

The first line contains one integer N - the number of vertices.

The next \(N-1\) lines describe the edges: each contains three integers u, v and c. This means that there is the edge with weight c between vertices u and v.

Output format

The first line should contain the single integer A modulo \(10^9 +7\) - sum of all functions \(f(u, v)\).

Constraints:

\(1 \le N \le 5 * 10^5\)

\(1 \le u, v \le N\)

\(1 \le c \le 10^9\)

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:30
10 votes
Tags:
AlgorithmsApprovedGraphsMedium
Points:30
8 votes
Tags:
AlgorithmsApprovedDynamic ProgrammingMediumOpen
Points:30
10 votes
Tags:
Binary SearchGraphsAlgorithmsDepth First SearchDFS