#P7827. A Very Easy Graph Problem
A Very Easy Graph Problem
A Very Easy Graph Problem
Problem Description
An undirected connected graph has nodes and edges, The -th edge’s length is . Each node has a value , which is either or . You need to calculate:
$$\sum_{i=1}^{n}\sum_{j=1}^{n}d(i,j)\times [a_i=1\wedge a_j=0] $$indicates the shortest distance between and . is the Iverson bracket. indicates . Because the answer may be too large, please output the answer modulo .
Input
The first line contains one integer (),indicating the number of test cases. The second line contains two ingeters (). The third line contains positive integers or ) —— the value of the nodes. The following lines contain two ingeters , and the -th line represents the i-th undirected edge’s length is , between node and . The sum of is no more than .
Output
Print a single integer—— the value of the answer modulo .
Sample Input
1
3 2
0 1 0
3 1
3 2
Sample Output
10
Source
2020 Multi-University Training Contest 6