Weighted Tree Function

3.5

2 votes
C++, Algorithms, Graph Representation, Graphs
Problem

You are given a weighted tree with N nodes and N1 edges. 

E is defined as i=Ni=1j=Nj=i+1F(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×i×j 

You are required to determine the value of E modulo 109+7.

Input format

  • The first line contains an integer N denoting the number of nodes in a tree.
  • The next N1 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 109+7.

Constraints

1N2×1051w1061u,vN

 

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation
  • F(1,2) = 10*1*2
  • F(1,3) = 2*1*3
  • F(2,3) = 10*2*3
  • E = F(1, 2) + F(1,3) + F(2,3) = 20 + 6 + 60 = 86
Editor Image

?