Fantasia
Time limit5sMemory limit64 MB
For each vertex i, compute the weight of the graph with i removed, where a connected graph weighs the product of its vertex weights and a disconnected one weighs the sum of its components.
- Level
Medium7 of 10
- Topics
- Graph, DFS, Brute force, Math
- Solved
- No attempts yet
Problem
Professor Zhang has an undirected graph with vertices and edges. Each vertex has an integer weight . Let be the graph obtained by deleting the -th vertex from graph . Professor Zhang wants to find the weights 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 weights of all the connected components of .
A connected component of an undirected graph is a subgraph in which any two vertices are connected by a path, and no other vertex in is connected to any vertex from by a path.
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.
Each of the next lines contains two integers and (, ) denoting an undirected edge.
There are at most test cases, the sum of in all the test cases is at most , and the sum of in all the test cases is also at most .
Output
For each test case, output the integer modulo , where is the weight of .