1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
| #include <algorithm> #include <iostream> #include <cstring> #include <cstdio> #include <vector> #include <stack> #include <queue> #include <cmath> #include <set> #include <map> #define mp make_pair #define pb push_back #define pii pair<int,int> #define link(x) for(edge *j=h[x];j;j=j->next) #define inc(i,l,r) for(int i=l;i<=r;i++) #define dec(i,r,l) for(int i=r;i>=l;i--) const int MAXN=3e5+10; const double eps=1e-8; #define ll long long using namespace std; const int inf=1e9; struct edge{int t,v;edge*next;}e[MAXN<<1],*h[MAXN],*o=e; void add(int x,int y,int vul){o->t=y;o->v=vul;o->next=h[x];h[x]=o++;} ll read(){ ll x=0,f=1;char ch=getchar(); while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();} while(isdigit(ch))x=x*10+ch-'0',ch=getchar(); return x*f; }
int n; int base,key,pos,sz[MAXN],maxx[MAXN],rt; bool vis[MAXN]; void get_root(int x,int pre){ sz[x]=1;maxx[x]=0; link(x){ if(vis[j->t]||j->t==pre)continue; get_root(j->t,x); sz[x]+=sz[j->t];maxx[x]=max(maxx[x],sz[j->t]); } maxx[x]=max(maxx[x],base-sz[x]); if(key>maxx[x])key=maxx[x],rt=x; }
int t[MAXN][4]; ll dis[MAXN]; void dfs(int x,int pre){ t[x][0]=t[x][1]=t[x][2]=0; t[x][dis[x]%3]++; link(x){ if(vis[j->t]||j->t==pre)continue; dis[j->t]=dis[x]+j->v; dfs(j->t,x); t[x][0]+=t[j->t][0];t[x][1]+=t[j->t][1];t[x][2]+=t[j->t][2]; } }
ll ans;
void solve(int x,int pre){ vis[x]=1;dis[x]=0; dfs(x,0); t[x][0]=t[x][1]=t[x][2]=0;t[x][0]++; link(x){ if(vis[j->t]||j->t==pre)continue; ans+=1ll*t[x][0]*t[j->t][0]; ans+=1ll*t[x][1]*t[j->t][2]; ans+=1ll*t[x][2]*t[j->t][1]; t[x][0]+=t[j->t][0];t[x][1]+=t[j->t][1];t[x][2]+=t[j->t][2]; } link(x){ if(vis[j->t]||j->t==pre)continue; key=inf;base=sz[j->t];get_root(j->t,x); solve(rt,0); } }
int main(){ n=read(); int x,y,z; inc(i,2,n)x=read(),y=read(),z=read(),add(x,y,z),add(y,x,z); ans=0; base=n;key=inf;get_root(1,0); solve(rt,0); ll t1=2*ans+n;ll t2=1ll*n*n; ll t=__gcd(t1,t2);t1/=t;t2/=t; printf("%lld/%lld\n",t1,t2); return 0; }
|