Fantasia
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Problem Description
Professor Zhang has an undirected graph G with n vertices and m edges. Each vertex is attached with a weight wi. Let Gi be the graph after deleting the i-th vertex from graph G. Professor Zhang wants to find the weight of G1,G2,...,Gn.The weight of a graph G is defined as follows:1. If G is connected, then the weight of G is the product of the weight of each vertex in G.2. Otherwise, the weight of G is the sum of the weight of all the connected components of G.A connected component of an undirected graph G is a subgraph in which any two vertices are connected to each other by paths, and which is connected to no additional vertices in G.
Input
There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:The first line contains two integers n and m (2≤n≤105,1≤m≤2×105) -- the number of vertices and the number of edges.The second line contains n integers w1,w2,...,wn (1≤wi≤109), denoting the weight of each vertex.In the next m lines, each contains two integers xi and yi (1≤xi,yi≤n,xi≠yi), denoting an undirected edge.There are at most 1000 test cases and ∑n,∑m≤1.5×106.
Output
For each test case, output an integer S=(∑i=1ni⋅zi) mod (109+7), where zi is the weight of Gi.
Sample Input
1 3 2 1 2 3 1 2 2 3
Sample Output
20
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=5739
题目描述:
有n个节点,每个节点有个值,然后m条边构成可能不止一张张图,每张图的价值是每个节点的值的乘积,然后总的价值就是所有图的价值加起来。
现在要分别删除每个点,G1就是代表的删除编号为1的节点,所有图加起来的价值。 然后问你1*G[2] + 2*G[2] + ……+n*G[n]. 最后的值膜一个1e9+7。
题解:
这个题很显然就是求割点,如果不是割点,就直接删除这个点就好了,如果是割点就复杂一点,就需要将该点的子树中最多能够访问到该点的子树的值给处理出来。然后把子树分开,另外处理就好了,还是看代码分析吧。道理是这么说,但是中途写错了好多东西TAT,对着数据改到现在...唉....
代码:
#include#include #include #include #include #include #include #include #include #include #include