Title Description
A force network can be thought of as a non-circular graph that may have a heavy edge but no self-loop. Each edge has an attribute and a weighted value. The property may be one of the R, G, b Three, representing the type of force on this edge. The weight value is a positive integer representing the strength of the force on the edge. The core of the force technology is to combine R, G and b three different forces to produce a single, easy-to-use force. In order to evaluate an energy network, JYY needs to find all the ternary rings that meet the requirements (three sides of the end-to-end), where R, G, and b three are each. The energy generated by a ternary ring is the product of the weights of the three sides. Now for the given force network, Jyy wants to know what the total energy of this network is. The total energy of the network is the sum of all the energies that satisfy the requirements of the ternary ring.
Input
The first line consists of two positive integers n, M. Represents the total number of vertices and the total number of edges of the force network. Next m line, each line contains three positive integers ui,vi,wi and one character Ci. Represents an edge between a numbered UI and a vertex of VI that has a property for the CI weight of wi. N≤50,000,m≤100,000,1≤? Wi≤10^6
Output
The output line is an integer representing the value of the total energy modulus 10^9+7 of the Force network.
Sample input
4 6
1 2 2 R
2 4 3 G
4 3 5 R
3 1 7 G
1 4 B
2 3 B
Sample output
828
Customary chunking (the first time do not unexpectedly but this is a routine, not only in the figure, in the mathematical rules and so on also has the application), the degree is bigger than the $\sqrt n$ Big Point, the rest is called the small point.
Only a large three-point triple-ring direct violence, and then for each dot, enumerate its two out-side hash judgment. The complexity of the first edge of the enumeration is the same as the number of edges, and the complexity of the enumeration of the second edge is the degree of the number of edges in the small point, i.e. no more than $o (m\sqrt N) $.
1#include <map>2#include <cmath>3#include <cstdio>4 #defineRep (i,l,r) for (int i=l; i<=r; i++)5typedefLong Longll;6 using namespacestd;7 8 Const intn=50100, mod=1000000007;9 structdata{Ten intx, y, Z, data () {} OneDataintAintBintc) {x=a,y=b,z=C;} A BOOL operator< (ConstData &a)Const{returnx = = a.x? y = = A.y? Z < a.z:y < a.y:x <a.x;} - }; -Map<data,ll>MP; the intN,m,si,x,y,z,t,head[n],to[n <<2],val[n <<2],opt[n <<2],next[n <<2],cnt,d[n],id[ -],tot; - Charstr[5]; - - voidAddintXintYintVintc) {to[++cnt]=y,val[cnt]=v,opt[cnt]=c,next[cnt]=head[x],head[x]=CNT;} + - intMain () { +ll ans=0; scanf"%d%d", &n,&m), si= (int) sqrt (m); ARep (I,1, M) { atscanf"%d%d%d%s",&x,&y,&z,str); -T= (str[0] =='R'?1: str[0] =='G'?2:3); -Add (X,y,z,t), add (y,x,z,t), d[x] ++,d[y] + + ; -(Mp[data (x,y,t)] + = z)%= mod, (Mp[data (y,x,t)] + = Z)%=MoD; - } -Rep (I,1, N)if(D[i] >= si) id[++tot]=i; inRep (I,1, tot) Rep (J,1, tot) rep (k,1, tot) -Ans= (Ans+mp[data (ID[I],ID[J),1)]*mp[data (Id[i],id[k],2)]%mod*mp[data (Id[j],id[k],3)])%MoD; toRep (I,1, N)if(D[i] <si) + for(intJ=head[i]; J; j=Next[j]) - if(D[to[j]] >= si | | to[j] >i) the for(intK=NEXT[J]; K k=Next[k]) * if(Opt[k]!=opt[j] && (d[to[k]]>=si | | to[k]>i)) $Ans= (Ans+mp[data (to[j],to[k),6-opt[j]-opt[k]) (]*val[j]%mod*val[k])%MoD;Panax Notoginsengprintf"%lld\n", ans); - return 0; the}
[JSOI2017] Force (chunked +map (hash))