Tree Of Trees
Problem
You are given edge-weighted trees having nodes respectively. You also have an array of positive integers.
You need to connect these trees using edges such that each of these edges have weights equal to for some and each of the must be used exactly once. Note that after connecting these edges, a single large tree will form consisting of nodes. Find the minimum possible sum of distances between each pair of nodes in the final tree .
Since this value can be large, output the value mod .
Input Format
- The first line of input will contain a single integer , denoting the number of trees.
- Then, you are given the trees. The description of each tree is as follows:
- The first line contains the integer , the number of nodes in -th tree.
- The next lines describe the edges of the -th tree. The -th of these lines contains space-separated integers and , describing that there is an edge of weight connecting the nodes and in tree .
- The final line contains space-separated integers, .
Output Format
For each test case, output on a new line the minimum sum of distances between each pair of nodes in tree , modulo .
Sample Input
Input
Output
2 3 1 2 1 2 3 3 4 1 2 2 1 3 1 1 4 3
1
72
Comments
Post a Comment