http://acm.hdu.edu.cn/showproblem.php?pid=5739
Known as an a[i graph, each node has a value of []. The value of a connected graph is defined as the product of the value of all its nodes; the value of a non-connected graph is the sum of the value of all its connected components.
The value of the remainder of the graph after each point is removed separately. N<=1e5,m<=2*1e5.
First, the original image may have several connected components, which is well handled. Only one of the connected components is described below.
First of all, deleting a point after the graph may split into a number of parts, so we have to deal with the cut point and non-cut points separately.
First, the Tarjan is used to find out all the cut points, in this process can be in the way to remove each cut point, the original connected component of the sub-graph of the value of the split. This requires our Dfs tree for the Tarjan process
Have a good understanding. For one of the cut points in the graph, you will be removed after you delete the graph must be divided into a branch greater than one. In the dfs tree, each branch is either a subtrees tree that corresponds to u , or a section other than the subtrees tree of U.
We use a global variable mul, in the DFS process, each to a node will be mul multiply the value of this node, if the node U, found its sub-node in the Dfs tree v,low[v]<=dfn[u], then the subtree is the root of the V is deleted
A branch after U. The value of this branch is the current Mul/mem, Mul is mem before Dfs is made to V. Add all the branch values.
For the cut U of a non-Dfs tree root node, the branch also includes the portion of the subtree that is the root of u in the Dfs trees, divided by the sum of the total mul by the portion of its subtree.
This makes the query very simple.
It is important to note that if a connected component has only one node, the component disappears after deletion, and it is easy to calculate its value by 1 if it is not specially handled. Attention.
1#include <iostream>2#include <cstdio>3#include <string>4#include <cstring>5#include <algorithm>6#include <cmath>7#include <vector>8 using namespacestd;9 Ten Const intN =100010, M =200010, mod = 1e9+7; One intColor,tot,p,n,m,start; A intdfn[n],low[n],spot[m*2],nex[m*2],head[n],c[n]; - Long LongAll,mul,a[n],f[n],size[n],chmul[n]; - BOOLV[n], is[N]; the -vector<int>Have[n]; - - voidAddintXinty) { +NEX[++P] = head[x]; HEAD[X] = p; SPOT[P] =y; -NEX[++P] = Head[y]; Head[y] = p; SPOT[P] =x; + } A at Long LongMypow (Long LongAintb) - { - Long Longc =1; - for(; b; b>>=1) { - if(b&1) C = c*a%MoD; -A = a*a%MoD; in } - returnC; to } + - Long LongNiLong Longx) the { * returnMypow (x, mod-2); $ }Panax Notoginseng - voidTarjan (intx) the { +LOW[X] = dfn[x] = + +tot; AV[X] =1; theC[X] =color; + have[color].push_back (x); -Mul = mul*a[x]%MoD; $ Long LongMem; $ inttp,y,cnt =0; - for(TP = head[x]; TP; TP =NEX[TP]) - { they =SPOT[TP]; - if(!Dfn[y])Wuyi { theMEM =Mul; - Tarjan (y); WuLOW[X] =min (low[x], low[y]); - if(low[y]>=Dfn[x]) { Aboutcnt++; $ Long Longtemp = mul*ni (mem)%MoD; -F[X] = (f[x]+temp)%MoD; -(Chmul[x] *= temp)%=MoD; - } A } + Else if(V[y]) theLOW[X] =min (low[x], dfn[y]); - } $ if(Start==x && cnt>1|| Start!=x &&CNT) the is[X] =1; the } the the Long LongCalintx) - { in Long LongTemp,ret; thetemp = (all-size[c[x])%MoD; the if(! is[x]) { About if(have[c[x]].size () = =1) theRET =temp; the Else theret = (Temp+size[c[x]]*ni (a[x))%MoD; + } - ElseRET = temp+F[x]; the return(ret%mod+mod)%MoD;Bayi } the the intMain () - { - inttest,i,j,x,y; the for(Cin >> test; test--; ) { theCIN >> N >>m; the for(i =1; i<=n; i++) thescanf"%i64d", &a[i]); - the for(i =1; i<=n; i++) theHead[i] =0; thep =0;94 the for(i =1; i<=m; i++) { thescanf"%d%d", &x,&y); the Add (x, y);98 } About - for(i =1; i<=n; i++) {101F[i] =0, chmul[i] =1;102 is[I] =0;103Dfn[i] = Low[i] = V[i] = C[i] =0;104 } the 106tot = ALL = color =0;107 for(i =1; i<=n; i++)108 have[i].clear ();109 for(i =1; i<=n; i++) the if(!Dfn[i]) {111color++; theMul =1; Start =i; Tarjan (i);113all = (All+mul)%MoD; theSize[color] =Mul; the for(j =0; J) the {117x =Have[color][j];118 if(x!=start) {119(F[x] + = Mul*ni (chmul[x]*a[x]%mod))%=MoD; - }121 }122 }123 Long LongAns =0;124 for(i =1; i<=n; i++) { theAns = (ans+i*cal (i))%MoD;126 }127printf"%i64d\n", ans); - }129 return 0; the}
View Code
More than 2016 school #2 1006 Fantasia