Temporary Tree
Practice
4 (5 votes)
Graphs
Depth first search
Algorithms
Problem
41% Success 780 Attempts 30 Points 1s Time Limit 256MB Memory 1024 KB Max Code

You are gifted a tree consisting of \(N\) vertices. There are two types of edges each with a weight \(val\) and denoted by a \(type\) value that is either \(0\) or \(1\). A pair \((i,j)\), where \(i<j\) is called GOOD if, while traversing from vertex \(i\) to vertex \(j\), we never pass through an edge of \(type\) 0. 

Your task is to calculate the sum of the path’s weight of all the GOOD pairs present in the tree. Print the final answer modulo \(10^9 + 7\).

Input Format:

  • The first line of input contains an integer \(T\), denoting the number of test cases.
  • The first line of each test case contains the integer \(N\).
  • The next \(N\) - 1 line contains four integers \(A\), \(B\), \(type\) and \(val\) where \(A\) and \(B\) represents the vertices of the tree, \(type\) represent the type of the edge and \(val\) define the edge weight.

Output format: 

For each test case, print an integer denoting the sum of the path's weight of all the GOOD pairs modulo \(10^9 + 7\).

Constraints:

\(1 \leq T \leq 10 \\ 1 \leq N \leq 10^5 \\ 1 \leq A, B \leq N \\ 0 \leq type \leq 1 \\ 0 \leq val \leq 1000\)

It is guaranteed that the input graph forms a tree. 

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
16 votes
Tags:
AlgorithmsApprovedGraphsMediumOpenTrees
Points:30
2 votes
Tags:
AlgorithmsDepth First SearchGraphsMedium
Points:30
5 votes
Tags:
AlgorithmsGraphsDynamic ProgrammingTreesNumber theoryDepth First Search