标签:
#include<iostream> #include<cstring> #include<cstdio> using namespace std; int temp,x,y,z,n,cnt,f[100001],son[100001],ans,root,sum,point[1000001],next[1000001],d[100001],num[10000001]; struct use{int st,en,val;}b[10000001]; bool isfocus[100001]; void add(int x,int y,int z){ z%=3; next[++cnt]=point[x];point[x]=cnt; b[cnt].st=x;b[cnt].en=y;b[cnt].val=z; } void findfocus(int x,int fa) { son[x]=1;f[x]=0; for (int i=point[x];i;i=next[i]) if (!isfocus[b[i].en]&&b[i].en!=fa) { findfocus(b[i].en,x); son[x]+=son[b[i].en]; f[x]=max(f[x],son[b[i].en]); } f[x]=max(f[x],sum-son[x]); if (f[x]<f[root]) root=x; } void getdeep(int x,int fa) { num[d[x]]++; for (int i=point[x];i;i=next[i]) if(!isfocus[b[i].en]&&b[i].en!=fa) { d[b[i].en]=(d[x]+b[i].val)%3; getdeep(b[i].en,x); } } int cal(int x,int now) { num[0]=num[1]=num[2]=0; d[x]=now; getdeep(x,0); return 2*num[1]*num[2]+num[0]*num[0]; } void work(int x) { ans+=cal(x,0); isfocus[x]=true; for (int i=point[x];i;i=next[i]) if (!isfocus[b[i].en]) { ans-=cal(b[i].en,b[i].val); sum=son[b[i].en];root=0; findfocus(b[i].en,0); work(root); } } int gcd(int a,int b){return b==0?a:gcd(b,a%b);} int main() { freopen("cckk.in","r",stdin); freopen("cckk.out","w",stdout); scanf("%d",&n); memset(isfocus,false,sizeof(isfocus)); for (int i=1;i<=n-1;i++) { scanf("%d%d%d",&x,&y,&z); add(x,y,z);add(y,x,z); } f[0]=sum=n; findfocus(1,0); work(root); temp=gcd(ans,n*n); cout<<ans/temp<<"/"<<n*n/temp<<endl; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/sunshinezff/article/details/46661329