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
92
93
94
95
96
97
98
99
| #include <cstdio>
#include <algorithm>
#define ll long long
using namespace std;
ll gcd(ll a,ll b){// a < b;
return a == 0?b:gcd(b%a,a);
}
const int MAXN = 110000;
struct Edge{
int from,to;
int len,nex;
}edge[MAXN];int ecnt = 2;
int fir[MAXN];
void addedge(int a,int b,int l){
edge[ecnt] = (Edge){a,b,l,fir[a]};
fir[a] = ecnt++;
edge[ecnt] = (Edge){b,a,l,fir[b]};
fir[b] = ecnt++;
}
//----
int n,m;
int f[MAXN],siz[MAXN],vis[MAXN];
int rt,sz;
int num[3],tmp[3];
ll ans = 0;
void getroot(int nown,int fa){
siz[nown] = 1,f[nown] = 0;
for(int nowe = fir[nown];nowe;nowe = edge[nowe].nex){
int v = edge[nowe].to;
if(vis[v] || v == fa) continue;
getroot(v,nown);
siz[nown] += siz[v];
f[nown] = max(f[nown],siz[v]);
}
f[nown] = max(f[nown],sz - siz[nown]);
if(f[nown] < f[rt]) rt = nown;
}
void getdeep(int nown,int fa,int d){
num[d]++;
for(int nowe = fir[nown];nowe;nowe = edge[nowe].nex){
int v = edge[nowe].to,l = edge[nowe].len;
if(vis[v] || v == fa) continue;
getdeep(v,nown,(d+l)%3);
}
}
void work(int nown){
tmp[0] = 1;tmp[1] = tmp[2] = 0;
for(int nowe = fir[nown];nowe;nowe = edge[nowe].nex){
int v = edge[nowe].to,l = edge[nowe].len;
if(vis[v]) continue;
num[0] = num[1] = num[2] = 0;
getdeep(v,nown,l);
ans -= num[0]*num[0] + 2*num[1]*num[2];
tmp[0] += num[0],tmp[1]+=num[1],tmp[2]+=num[2];
}
ans += tmp[0] * tmp[0] + 2*tmp[1] * tmp[2];
}
void solve(int nown){
vis[nown] = 1;
work(nown);
for(int nowe = fir[nown];nowe;nowe = edge[nowe].nex){
int v = edge[nowe].to;
if(vis[v]) continue;
f[rt = 0] = sz = siz[v];
getroot(v,rt);
solve(rt);
}
}
void init(){
scanf("%d",&n);
int a,b,c;
for(int i = 1;i<=n-1;i++){
scanf("%d %d %d",&a,&b,&c);
addedge(a,b,c%3);
}
}
void solve(){
f[rt = 0] = sz = n;
getroot(1,rt);
solve(rt);
ll ans2 = n*n;
printf("%lld/%lld\n",ans/gcd(ans,ans2),ans2/gcd(ans,ans2));
}
int main(){
init();
solve();
return 0;
}
|