Weighted Tree Function
Practice
4 (3 votes)
C++
Algorithms
Graph representation
Graphs
Problem
71% Success 743 Attempts 50 Points 1s Time Limit 256MB Memory 1024 KB Max Code
You are given a weighted tree with \(N\) nodes and \(N - 1\) edges.
\(E\) is defined as \(\sum_\limits{i=1}^{i = N} \sum_\limits{j=i +1}^{j = N} F(i,j)\). Also, \(F(i, j)\) is equal to = (Maximum value of the edge that is present on the simple path between node \(i\) and \(j\)) \(\times i \times j\)
You are required to determine the value of \(E\) modulo \(10^9 + 7\).
Input format
- The first line contains an integer \(N\) denoting the number of nodes in a tree.
- The next \(N - 1\) lines contain three space-separated integers \(u, v, w\) denoting an edge between node \(u\) and \(v\) with weight \(w\).
Output format
Print the required value of \(E\) modulo \(10^9 + 7\).
Constraints
\(1 \le N \le 2 \times 10^5 \\ 1 \le w \le 10^6 \\ 1 \le u, v \le N\)
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
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
26 votes
Tags:
Ad-hocAlgorithmsBasic ProgrammingMediumOpenRoot
Points:50
5 votes
Tags:
AlgorithmsGraph RepresentationGraphsC++
Points:50
5 votes
Tags:
AlgorithmsApprovedGraphsHardOpen
Editorial
Login to unlock the editorial