Hdu5739fantasia (second field of multi-school 1006) cut point + inverse element

Source: Internet
Author: User

Fantasia

Time limit:10000/5000 MS (java/others) Memory limit:65536/65536 K (java/others)


Problem Descriptionprofessor Zhang have an undirected graphGWithNVertices andmEdges. Each vertex are attached with a weightWi . LetGi Being the graph after deleting theI-th Vertex from graphG. Professor Zhang wants to find the weight ofg1,G2,.. . ,Gn .

The weight of a graphGis defined as follows:

1. IfGis connected, then the weight ofGis the product of the weight of all vertex inG.
2. Otherwise, the weight ofGIs the sum of the weight of the connected components ofG.

A connected component of an undirected graph G be a subgraph in which any and vertices are connected to each Other by paths, and which are connected to no additional vertices in G.

Inputthere is multiple test cases. The first line of input contains an integerT, indicating the number of test cases. For each test case:

The first line contains integersNandm ( Span id= "mathjax-span-79" class= "mn" >2≤ n≤ 10 5,1 ≤m ≤2 x 105) -The number of vertices and the number of edges.

The second line containsNIntegersW1,w2,.. . ,wn (1≤wi≤9) , denoting the weight of each vertex.

In the next m lines, each contains the integersxi andyi (1≤xi,yi≤n,xi≠yi) , denoting an undirected edge.

There is at the most1000  test cases And ∑n,< Span id= "mathjax-span-171" class= "Mo" >∑m ≤1.5 x106 .

Outputfor each test case, output an integerS=(∑I=1ni-Zi) mod (9+7) , where zi is the weight of GI.

Sample Input13 21 2 31 22 3

Sample Output20 Title Address: http://acm.hdu.edu.cn/showproblem.php?pid=5739 Title Description: There are n nodes, each node has a value, and then the M-bar may be more than one picture, The value of each graph is the product of the value of each node, and then the total value is the sum of the values of all the graphs. Now to delete each point separately, G1 is the node that represents the deletion number 1, and the value of all the graphs added together. Then ask you 1*g[2] + 2*g[2] + ... +n*g[n]. The last value of the film is a 1e9+7. Problem: This question is obviously to seek cut point, if not cut point, directly delete this point is good, if it is cut point is more complex, it is necessary to the point of the subtree in the sub-tree can be accessed up to the value of the sub-tree to deal with it. Then the tree separate, the other processing is good, or to see the code analysis it. The truth is that, but halfway write a lot of things wrong tat, the data to the present ... Alas.... Code:  
 #include <cstdio> #include <cmath> #include <iostream> #include <algorithm> #include <vector > #include <stack> #include <cstring> #include <queue> #include <set> #include <string> #include <map> #define INF 9223372036854775807 #define INF 9e7+5 #define PI ACOs ( -1) using namespace std; typedef long Long LL; typedef double DB; const int MAXN = 1e5 + 5; const INT mod = 1e9 + 7; Const DB EPS = 1e-9; ll VA[MAXN], W[MAXN], Sum, ANS[MAXN]; int PRE[MAXN], dfs_tim, tot, N, M, Low[maxn], T, VEP[MAXN]; BOOL VIS[MAXN]; Vector<int> G[MAXN];     void Init () {memset (Vis, false, sizeof (VIS));     memset (pre, 0, sizeof (pre));     Sum = tot = Dfs_tim = 0; for (int i = 1; I <= n; i++) g[i].clear ();     }//Fast power, for inverse yuan with LL pow_mod (ll A, ll B, ll P) {LL ret = 1;         while (b) {if (b & 1) ret = (ret * a)% P;         A = (A * a)% P;     b >>= 1; } return ret; }//Fermat theorem to find the inverse element ll inv (ll x) {return pow_mod (x, MOD-2, MoD);     }//write well first, do not bother each module void Add (ll &x, ll y) {x = x + y; x = (x + mod)% MoD;     }//The value of each graph is processed mainly by void Find (int x) {va[x] = w[x];         for (int i = 0; i < g[x].size (); i++) {int u = g[x][i];         if (Vis[u]) continue; Vis[u] = true;         Find (U);     VA[X] = va[x] * va[u]% mod; }} ll dfs (int x, int fa, int root) {//current node, parent node and root node low[x] = pre[x] = ++dfs_tim;//pre array record access time ans[x] = INV (W[x] ); Delete the node that is accessed at this time, CLD = 0;     ll sum = 0, res = w[x], pro = 1;         for (int i = 0; i < g[x].size (); i++) {int u = g[x][i];             if (!pre[u]) {cld++; ll tmp = DFS (U, x, root); TMP returns the value of the subtree for u low[x] = min (low[x], low[u]); Update the earliest ancestor if (Low[u] >= pre[x]) that the X node can access {//If you have access to X for this subtree, then the x node is deleted, and the subtree of U is separated by the add (sum, t  MP);  Sum means that the x node is deleted, X will be separated by the value of the subtree ans[x] = ans[x] * INV (TMP)% MoD; Same as above delete node, means delete this subtree} res = res * tmp % MoD; Find the value of the subtree} else if (u! = FA) Low[x] = min (low[x], pre[u]);  For a node that accesses earlier than the current node, the update can access the oldest node}//TT represents the value of the other graphs except this one and ll TT = (Sum-va[root] + MoD)% MoD;    VA[ROOR]*ANS[X] ans[x] is already inverse, so this sentence ans[x] = va[root] * ans[x]% mod; Represents an X node and a separate subtree after the value of the graph is deleted if (FA = =-1 && ans[x] = = 1) ans[x] = 0;                                                For a picture, if his child nodes are all deleted, we//ans[x] is 1, but in fact it should be 0, so     Need a special sentence, such as a picture 1-2, 1-3. Add (Ans[x], TT); Add (ans[x], sum); Add additional and deleted subtree if (FA = =-1) {if (CLD = = 1) {//For the first ancestor, if he has only one child//son, then he is not cut, learn to cut points should be learned               Over qaq ans[x] = va[root] * INV (w[x])% MoD;         Add (Ans[x], TT); } else if (g[x].size () = = 0) {ans[x] = TT;//If this is an outlier, the deletion is directly the value of the other graph}} return res;     } void Solve () {cin >> n >> m;     Init (); for (int i =1; I <= N;     i++) scanf ("%i64d", &w[i]);         for (int i = 1; I <= m; i++) {int u, v; scanf ("%d%d", &u, &v);         G[u].push_back (v);     G[v].push_back (U);         } for (int i = 1; I <= n; i++) {if (vis[i]) continue;         Vis[i] = true; Vep[++tot] = i; Find (i); The VEP array is used to store the start node of each graph to be accessed add (Sum, va[i]); The total value of all graphs, va[i] represents the total value of this graph} for (int i = 1; I <= tot; i++) {DFS (vep[i],-1, vep[i]), the//-1 position represents the parent node, for the most     The starting point of the father is set to-1} ll pri = 0; for (ll i = 1; I <= n; i++) {Add (PRI, i*ans[i]%mod);//Find last value} cout << pri << Endl;}   int main () {//cin.sync_with_stdio (false);    Freopen ("Tt.txt", "R", stdin);    Freopen ("Hh.txt", "w", stdout);    CIN >> T;    while (t--) solve (); return 0;}

  

Hdu5739fantasia (second field of multi-school 1006) cut point + inverse element

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.