2152: Cong cocoa time limit:3 Sec Memory limit:259 MB
submit:2424 solved:1277
[Submit] [Status] [Discuss] Description
Cong and But the two brothers, they often fight for some trivial things, such as only the last ice bar at home and both want to eat, two people want to play computer (but their home only a computer) ... In the face of this problem, the general situation of stone scissors cloth, but they have been tired of this low IQ game. Their father was annoyed by their quarrel, so he invented a new game: by the father on the paper to draw N "Point", and n-1 "side" to connect the N "point" (in fact, this is a tree). And each "edge" has a number. Next by Cong and cocoa then select a point (of course, they do not see the tree at the point of view), if the two points between all sides of the number and add up exactly is a multiple of 3, then sentenced Cong win, otherwise cocoa win. Cong is very fond of thinking about the problem, after each game will carefully study the tree, want to know how to win the picture of their own probability of how much. Now please help to find out this value to verify that Cong's answer is correct.
Input
The 1th line of the input contains 1 positive integer n. The following n-1 lines, 3 integers x, y, W for each line, indicate an edge between the X and Y points, and the number above is W.
Output
This probability is output in the form of a "A/b", where A and B must be coprime. If the probability is 1, output "1/1").
Sample Input5
1 2 1
1 3 2
1 4 1
2 5 3
Sample Output13/25
"Sample description"
The 13 set of point pairs are (2,2) (2,3) (2,5) (3,2) (3,3) (3,4) (3,5) (4,3) (5,2) (5,3).
"Data Size"
For 100% of data, n<=20000. Template Questionsconsider the path through a point, cou[i] represents the number of points in depth%3==i, cou[0]*cou[0]+cou[1]*cou[2]*2, and subtract the same subtrees tree
Note to set sum ah to set sum ah to set sum ah to set sum ah to set sum ah to set sum ah to set sum ah to set sum AH
#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>using namespacestd;Const intn=2e4+5, inf=1e9;intRead () {CharC=getchar ();intx=0, f=1; while(c<'0'|| C>'9'){if(c=='-') f=-1; C=GetChar ();} while(c>='0'&&c<='9') {x=x*Ten+c-'0'; C=GetChar ();} returnx*F;}intn,u,v,w;structedge{intV,w,ne;} E[n<<1];intH[n],cnt;inlinevoidInsintUintVintW) {CNT++; E[CNT].V=v;e[cnt].w=w;e[cnt].ne=h[u];h[u]=CNT; CNT++; E[CNT].V=u;e[cnt].w=w;e[cnt].ne=h[v];h[v]=CNT;}intsize[n],f[n],rt,vis[n],sum;voidDFSRT (intUintFA) {Size[u]=1; f[u]=0; for(intI=h[u];i;i=e[i].ne) { intv=e[i].v; if(vis[v]| | V==FA)Continue; DFSRT (V,u); Size[u]+=Size[v]; F[u]=Max (F[u],size[v]); } F[u]=max (f[u],sum-Size[u]); if(F[u]<f[rt]) rt=u;}intdeep[n],cou[4];voidDfsdeep (intUintFA) {Cou[deep[u]]++; for(intI=h[u];i;i=e[i].ne) { intv=e[i].v; if(vis[v]| | V==FA)Continue; DEEP[V]= (DEEP[U]+E[I].W)%3; Dfsdeep (V,u); }}intCalintUintNow ) {Deep[u]=now%3; memset (Cou,0,sizeof(cou)); Dfsdeep (U,0); returncou[0]*cou[0]+cou[1]*cou[2]*2;}intans;voidDfssol (intu) {//printf ("Sol%d\n", u);vis[u]=1; Ans+=cal (U,0); for(intI=h[u];i;i=e[i].ne) { intv=e[i].v; if(Vis[v])Continue; Ans-=cal (V,E[I].W); Sum=Size[v]; RT=0;d Fsrt (V,0); v=RT; Dfssol (v); }}inlineintgcdintAintb) {returnb==0? A:GCD (b,a%b);}intMain () {//freopen ("In.txt", "R", stdin);n=read (); for(intI=1; i<n;i++) U=read (), V=read (), w=read (), ins (u,v,w); Sum=N; f[0]=INF; RT=0;d FSRT (1,0); Dfssol (RT); intG=GCD (ans,n*N); printf ("%d/%d", ans/g,n*n/g);}
Bzoj 2152: Cong cocoa [Point Division]