Advertisement
yicongli

BZ3697

Mar 4th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.17 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define gc c=getchar()
  6. #define r(x) read(x)
  7. #define ll long long
  8.  
  9. template<typename T>
  10. inline void read(T&x){
  11.     x=0;T k=1;char gc;
  12.     while(!isdigit(c)){if(c=='-')k=-1;gc;}
  13.     while(isdigit(c)){x=x*10+c-'0';gc;}x*=k;
  14. }
  15.  
  16. const int N=1e5+7;
  17.  
  18. int root,SIZE;
  19. int siz[N];
  20. int s[N];
  21.  
  22. int ecnt;
  23. int fir[N],nex[N<<1],to[N<<1],w[N<<1];
  24.  
  25. inline void addedge(int u,int v,int c){
  26.     nex[++ecnt]=fir[u],fir[u]=ecnt,to[ecnt]=v,w[ecnt]=c;
  27.     nex[++ecnt]=fir[v],fir[v]=ecnt,to[ecnt]=u,w[ecnt]=c;
  28. }
  29.  
  30. bool vis[N];
  31.  
  32. void getroot(int x,int f){
  33.     siz[x]=1;
  34.     s[x]=0;
  35.     for(int i=fir[x];i;i=nex[i]){
  36.         int v=to[i];
  37.         if(v==f||vis[v])continue;
  38.         getroot(v,x);
  39.         siz[x]+=siz[v];
  40.         s[x]=max(s[x],siz[v]);
  41.     }
  42.     s[x]=max(s[x],SIZE-siz[x]);
  43.     if(s[x]<s[root])root=x;
  44. }
  45.  
  46. ll ans;
  47.  
  48. int pool[N*10];
  49. int *F[2]={pool+N*1,pool+N*3};
  50. int *G[2]={pool+N*5,pool+N*7};
  51. int *cnt=pool+N*9;
  52.  
  53. int dis[N];
  54.  
  55. int maxdep;
  56.  
  57. void getdist(int x,int f,int dep){
  58.     maxdep=max(maxdep,dep);
  59.     F[bool(cnt[dis[x]])][dis[x]]++;
  60.     cnt[dis[x]]++;
  61.     for(int i=fir[x];i;i=nex[i]){
  62.         int v=to[i];
  63.         if(v==f||vis[v])continue;
  64.         dis[v]=dis[x]+w[i];
  65.         getdist(v,x,dep+1);
  66.     }
  67.     cnt[dis[x]]--;
  68. }
  69.  
  70. void solve(int x){
  71.     vis[x]=1;
  72.     int mx=0;
  73.     G[0][0]=1;
  74.     for(int i=fir[x];i;i=nex[i]){
  75.         int v=to[i];
  76.         if(vis[v])continue;
  77.         dis[v]=w[i];
  78.         maxdep=0;
  79.         getdist(v,0,1);
  80.         ans+=(ll)(G[0][0]-1)*F[0][0];
  81.         for(int i=-maxdep;i<=maxdep;++i){
  82.             ans+=(ll)G[0][i]*F[1][-i];
  83.             ans+=(ll)G[1][i]*F[0][-i];
  84.             ans+=(ll)G[1][i]*F[1][-i];
  85.         }
  86.         for(int i=-maxdep;i<=maxdep;++i){
  87.             G[0][i]+=F[0][i];
  88.             G[1][i]+=F[1][i];
  89.             F[0][i]=F[1][i]=0;
  90.         }
  91.         mx=max(mx,maxdep);
  92.     }
  93.     for(int i=-mx;i<=mx;++i){
  94.         G[0][i]=G[1][i]=0;
  95.     }
  96.     for(int i=fir[x];i;i=nex[i]){
  97.         int v=to[i];
  98.         if(vis[v])continue;
  99.         s[root=0]=SIZE=siz[v];
  100.         getroot(v,0);
  101.         solve(root);
  102.     }
  103. }
  104.  
  105. int main(){
  106. //  freopen(".in","r",stdin);
  107. //  freopen(".out","w",stdout);
  108.     int n;r(n);
  109.     for(int i=1;i<n;++i){
  110.         int u,v,c;
  111.         r(u),r(v),r(c);
  112.         addedge(u,v,c?1:-1);
  113.     }
  114.     s[root=0]=SIZE=n;
  115.     getroot(1,0);
  116.     solve(root);
  117.     printf("%lld\n",ans);
  118.     return 0;
  119. }
  120. /*
  121. 7
  122. 1 2 0
  123. 3 1 1
  124. 2 4 0
  125. 5 2 0
  126. 6 3 1
  127. 5 7 1
  128.  
  129. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement