#P11010. [2016杭电多校]Fantasia
[2016杭电多校]Fantasia
Fantasia
Problem Description
Professor Zhang has an undirected graph with vertices and edges. Each vertex is attached with a weight . Let be the graph after deleting the -th vertex from graph . Professor Zhang wants to find the weight of . The weight of a graph is defined as follows:
- If is connected, then the weight of is the product of the weight of each vertex in .
- Otherwise, the weight of is the sum of the weight of all the connected components of . A connected component of an undirected graph is a subgraph in which any two vertices are connected to each other by paths, and which is connected to no additional vertices in .
Input
There are multiple test cases. The first line of input contains an integer , indicating the number of test cases. For each test case: The first line contains two integers and -- the number of vertices and the number of edges. The second line contains integers , denoting the weight of each vertex. In the next m lines, each contains two integers and , denoting an undirected edge. There are at most test cases and .
Output
For each test case, output an integer $S = (\sum\limits_{i=1}^{n}i\cdot z_i) \text{ mod } (10^9 + 7)$, where is the weight of .
Sample Input
1
3 2
1 2 3
1 2
2 3
Sample Output
20
Author
zimpha
Source
2016 Multi-University Training Contest 2