題目連接:點這裏c++
大體題意:給你一顆樹,求出全部邊權和能被3整除的路徑數git
用一個pos數組來記錄路徑長度mod 3以後的數量數組
則答案爲pos[1] * pos[2] * 2+pos[3]*pos[3]ide
最後去除同子樹內的答案便可spa
#include<bits/stdc++.h> #define ll long long #define inf 2147483647 using namespace std; const int N=2e4+1; int rt,cnt,szt,head[N],sz[N],mx[N]; int n,ans,tot,dis[N],vis[N],pos[3]; struct Edge{int nxt,to,val;}edge[N<<1]; void ins(int x,int y,int z){ edge[++cnt].nxt=head[x]; edge[cnt].to=y;edge[cnt].val=z; head[x]=cnt; } int gcd(int x,int y){return y==0?x:gcd(y,x%y);} void getrt(int x,int f){ sz[x]=1,mx[x]=0; for(int i=head[x];i;i=edge[i].nxt){ int y=edge[i].to; if(y==f||vis[y]) continue; getrt(y,x);sz[x]+=sz[y]; mx[x]=max(mx[x],sz[y]); }mx[x]=max(mx[x],szt-sz[x]); if(mx[x]<mx[rt]) rt=x; } void getdis(int x,int f,int d){ dis[++tot]=d;pos[d%3]++; for(int i=head[x];i;i=edge[i].nxt){ int y=edge[i].to; if(y==f||vis[y]) continue; getdis(y,x,d+edge[i].val); } } int calc(int x,int d){ pos[0]=pos[1]=pos[2]=0; tot=0;getdis(x,0,d); return pos[1]*pos[2]*2+pos[0]*pos[0]; } void divide(int x){ ans+=calc(x,0);vis[x]=1; for(int i=head[x];i;i=edge[i].nxt){ int y=edge[i].to; if(!vis[y]){ ans-=calc(y,edge[i].val); rt=0,szt=sz[y],getrt(y,0); divide(rt); } } } int read(){ int x=0,f=1;char ch=getchar(); while(!isdigit(ch)){if(ch=='-')f=-f;ch=getchar();} while(isdigit(ch)){x=x*10+ch-48;ch=getchar();} return x*f; } int main(){ n=read(); for(int i=1;i<n;i++){ int x=read(),y=read(),z=read(); ins(x,y,z);ins(y,x,z); } szt=n;mx[rt]=inf; getrt(1,0);divide(rt); int d=gcd(ans,n*n); printf("%d/%d",ans/d,n*n/d); return 0; }