Title:
For a tree with edge weight, how many pairs of simple path weights and module 3 are 0
Train of thought:
For simple tree divide and conquer, each time the center of gravity is processed, the distance to the center of gravity is processed, then the direct square of 0, the multiplication of 1 and 2 and the multiplication of 2 (because (2, 3), (3, 2) are two pairs), then the illegal logarithm is deleted, and then the center of gravity is deleted.
Repeat the above process
Mistakes and Reflections:
If you find it difficult to divide trees, it's better to brush them slowly
And how they got to over 100...
Code:
#include<bits/stdc++.h>
#define fi first
#define se second
using namespace std;
const int N = 20010;
struct EDGE{
int to,next,val;
}e[N*2];
int first[N],n,tot=0,si[N],maxn[N],num[3];
bool did[N];
inline int read()
{
int x=0;char ch=getchar();
while(ch<'0'||ch>'9')ch=getchar();
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x;
}
void addedge(int x,int y,int z){
e[tot].to=y;
e[tot].val=z;
e[tot].next=first[x];
first[x]=tot++;
e[tot].to=x;
e[tot].val=z;
e[tot].next=first[y];
first[y]=tot++;
}
void dfs1(int now,int fa){
si[now]=1;
maxn[now]=0;
for(int i=first[now];i!=-1;i=e[i].next)
if(e[i].to!=fa&&!did[e[i].to]){
dfs1(e[i].to,now);
si[now]+=si[e[i].to];
maxn[now]=max(maxn[now],si[e[i].to]);
}
}
void dfs3(int now,int fa,int& root,int& num,int t){
int MA=max(maxn[now],si[t]-si[now]);
if(MA<num){
num=MA;
root=now;
}
for(int i=first[now];i!=-1;i=e[i].next)
if(e[i].to!=fa&&!did[e[i].to])
dfs3(e[i].to,now,root,num,t);
}
void dfs2(int now,int fa,int tlen){
tlen%=3;
for(int i=first[now];i!=-1;i=e[i].next)
if(e[i].to!=fa&&!did[e[i].to])
dfs2(e[i].to,now,tlen+e[i].val);
num[tlen]++;
}
int cal(int now,int d){
memset(num,0,sizeof(num));
dfs2(now,-1,d);
int ans=num[0]*num[0];
ans+=num[1]*num[2]*2;
return ans;
}
int solve(int now){
int root,num=1e9;
dfs1(now,-1);
dfs3(now,-1,root,num,now);
int ans=cal(root,0);
did[root]=true;
for(int i=first[root];i!=-1;i=e[i].next)
if(!did[e[i].to]){
ans-=cal(e[i].to,e[i].val);
ans+=solve(e[i].to);
}
return ans;
}
int main(){
memset(first,-1,sizeof(first));
n=read();
for(int i=0,u,v,w;i<n-1;i++){
u=read(),v=read(),w=read();
addedge(u,v,w);
}
int a=solve(1);
int b=n*n;
int c=__gcd(a,b);
a/=c;
b/=c;
printf("%d/%d\n",a,b);
}