点分治傻逼题。。。。
#include<bits/stdc++.h>
#define MAXN 50005
using namespace std;
int n,h[MAXN],tot,dis[MAXN],sz[MAXN],maxl[MAXN],ZX,vis[MAXN],a1,a2;
int dui[MAXN],js,jjs,tp[4],ans[4];
struct node{
int from,to,cost,next;
}e[MAXN << 1];
void add(int x , int y , int z){
tot++;
e[tot].from = x;
e[tot].to = y;
e[tot].cost = z % 3;
e[tot].next = h[x];
h[x] = tot;
}
void getsz(int now , int fa){
sz[now] = 1 , maxl[now] = 0;
for(int i = h[now] ; i != (-1) ; i = e[i].next){
if(e[i].to == fa || vis[e[i].to])continue;
getsz(e[i].to , now);
sz[now] += sz[e[i].to];
if(sz[e[i].to] > sz[maxl[now]])maxl[now] = e[i].to;
}
}
void getzx(int now , int fa , int yl){
for(int i = h[now] ; i != (-1) ; i = e[i].next){
if(e[i].to == fa || vis[e[i].to])continue;
getzx(e[i].to , now , yl);
}
maxl[now] = max(sz[maxl[now]] , sz[yl] - sz[maxl[now]]);
if(maxl[now] < maxl[ZX])ZX = now;
}
void getdis(int now , int fa){
for(int i = h[now] ; i != (-1) ; i = e[i].next){
if(e[i].to == fa || vis[e[i].to])continue;
dis[e[i].to] = (dis[now] + e[i].cost) % 3;
getdis(e[i].to , now);
}
js++;
dui[js] = dis[now] % 3;
}
void calc(int now){
js = 0;
for(int i = h[now] ; i != (-1) ; i = e[i].next){
if(vis[e[i].to])continue;
dis[e[i].to] = e[i].cost , jjs = js;
getdis(e[i].to , now);
for(int j = jjs + 1 ; j <= js ; j++)for(int k = 0 ; k <= 2 ; k++)ans[(k + dui[j]) % 3] += tp[k];
for(int j = jjs + 1 ; j <= js ; j++)tp[dui[j] % 3]++;
}
for(int i = 0 ; i <= 2 ; i++)ans[i] += tp[i];
memset(tp , 0 , sizeof(tp));
}
void solve(int now){
calc(now);vis[now] = 1;getsz(now , now);
for(int i = h[now] ; i != (-1) ; i = e[i].next){
if(vis[e[i].to])continue;
ZX = 0;getzx(e[i].to , now , e[i].to);
solve(ZX);
}
}
void init(){
tot = 0;
memset(h , -1 , sizeof(h));
cin>>n;int x,y,w;maxl[0] = 99999999;
for(int i = 1 ; i < n ; i++){
cin>>x>>y>>w;
add(x , y , w);
add(y , x , w);
}
getsz(1 , 1) , ZX = 0 , getzx(1 , 1 , 1);
solve(ZX);
ans[0] = ans[0] * 2 + n;
a1 = ans[0] , a2 = n * n;
for(int i = 2 ; i <= min(a1 , a2) ; i++){
while(a1 % i == 0 && a2 % i == 0){
a1 /= i;
a2 /= i;
}
}
cout<<a1<<"/"<<a2<<endl;
}
int main(){
init();
}