#include using namespace std; #define ll long long #define rep(i,n) for(int i=0;i=0;i--) #define rrep2(i,n,k) for(int i=n-1;i>=n-k;i--) #define vll(n,i) vector(n,i) #define v2ll(n,m,i) vector>(n,vll(m,i)) #define v3ll(n,m,k,i) vector>>(n,v2ll(m,k,i)) #define v4ll(n,m,k,l,i) vector>>>(n,v3ll(m,k,l,i)) #define all(v) v.begin(),v.end() #define chmin(k,m) k = min(k,m) #define chmax(k,m) k = max(k,m) #define Pr pair #define Tp tuple #define riano_ std::ios::sync_with_stdio(false);std::cin.tie(nullptr) using Graph = vector>; const ll mod = 998244353; template struct modint{ uint64_t val; constexpr modint(const int64_t val_=0) noexcept:val((val_%int64_t(mod)+int64_t(mod))%int64_t(mod)){} constexpr modint operator-() const noexcept{ return modint(*this)=mod-val; } constexpr modint operator+(const modint rhs) const noexcept{ return modint(*this)+=rhs; } constexpr modint operator-(const modint rhs) const noexcept{ return modint(*this)-=rhs; } constexpr modint operator*(const modint rhs) const noexcept{ return modint(*this)*=rhs; } constexpr modint operator/(const modint rhs) const noexcept{ return modint(*this)/=rhs; } constexpr modint &operator+=(const modint rhs) noexcept{ val+=rhs.val; val-=((val>=mod)?mod:0); return (*this); } constexpr modint &operator-=(const modint rhs) noexcept{ val+=((val>=1; } return (*this)*=now; } modint & operator++(){ val++; if (val == mod) val = 0; return *this; } modint operator++(int){ modint res = *this; ++*this; return res; } constexpr bool operator==(const modint rhs) noexcept{ return val==rhs.val; } constexpr bool operator!=(const modint rhs) noexcept{ return val!=rhs.val; } friend constexpr ostream &operator<<(ostream& os,const modint x) noexcept{ return os<<(x.val); } friend constexpr istream &operator>>(istream& is,modint& x) noexcept{ uint64_t t; is>>t,x=t; return is; } }; typedef modint mint; mint pw(long long a,long long b,long long m = mod){ if(a%m==0) return mint(0); if(b==0) return mint(1); else if(b%2==0){ long long x = pw(a,b/2,m).val; return mint(x*x); } else{ long long x = pw(a,b-1,m).val; return mint(a*x); } } mint modinv(long long a, long long m = mod) { long long b = m, u = 1, v = 0; while (b) { long long t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } u %= m; return mint(u); } #define vm(n,i) vector(n,i) #define v2m(n,m,i) vector>(n,vm(m,i)) #define v3m(n,m,k,i) vector>>(n,v2m(m,k,i)) #define v4m(n,m,k,l,i) vector>>>(n,v3m(m,k,l,i)) map> prv; auto cc = vll(2e5+1,0); auto ss = vll(2e5+1,0); ll ans = 0; map top_cc; map top_ss; //map add template void add(map &cnt,T a,ll n = 1){ if(cnt.count(a)) cnt[a] += n; else cnt[a] = n; } //tree #define type long long type el = 0; vector emp = {}; struct tree { long long N; vector>> G; vector depth; vector subtree_size; //部分木のサイズ vector deg; vector par; vector vis; vector dp; //DPの結果 vector> dp_cand; long long len; long long euler_time; long long limit_len = 2e9; vector from_e1; //直径の端点からの距離 vector from_e2; //直径の端点からの距離 long long cen,end1,end2,diam; //中心、直径の端点、直径 vector> fr; vector> bc; vector from_par; vector reroot_dp; //全方位DPの結果 tree(long long n) { N = n; G = vector>>(N); depth = vector(N,-1); par = vector(N,-1); subtree_size = vector(N,1); deg = vector(N,0); } //DP 操作: 書き換え必須 void dp_operation(long long s){ } //Euler Tour で何かする場合は書き足す void dfs_(long long s,long long i,bool dp_){ len++; euler_time++; ll d = depth[s]; if(prv.count(d)){ prv[d].push_back(s); } else{ prv[d] = {s}; } for(auto[nx,cost]:G[s]){ if(len>=limit_len) break; if(vis[nx]==i) continue; ss[s] = 0; cc[s] = 0; depth[nx] = depth[s] ^ cost; vis[nx] = i; par[nx] = s; dfs_(nx,i,dp_); ans -= cc[s]*(ss[s]+(N-1)-subtree_size[nx]); } if(par[s]!=-1){ subtree_size[par[s]] += subtree_size[s]; } if(dp_) dp_operation(s); len--; prv[d].pop_back(); if(prv[d].size()!=0){ ll k = prv[d].size()-1; ll p1 = prv[d][k]; cc[p1]++; ss[p1] += subtree_size[s]; } else{ add(top_cc,d); add(top_ss,d,subtree_size[s]); } } //c は辺の重み void unite(long long a,long long b,long long c = 1){ G[a].emplace_back(b,c); G[b].emplace_back(a,c); deg[a]++; deg[b]++; } //lim は探索長さ制限 dfs_count は探索累積回数(前回訪問済情報を残す場合) void dfs(long long rt,bool dp_ = false,long long lim = 2e9,long long dfs_count = 0){ if(dfs_count==0) vis.assign(N,-1); if(dp_){ dp.assign(N,el); dp_cand.assign(N,emp); } limit_len = lim; euler_time = -1; len = -1; vis[rt] = dfs_count; depth[rt] = 0; dfs_(rt,dfs_count,dp_); } long long diameter(void){ dfs(1); from_e2 = depth; long long mx = -1; for(int i=0;imx){ end1 = i; mx = from_e2[i]; } } dfs(end1); from_e1 = depth; mx = -1; for(int i=0;imx){ end2 = i; mx = from_e1[i]; } } dfs(end2); from_e2 = depth; diam = mx; center(); return diam; } long long center(void){ for(int i=0;i adj(long long i){ vector res; for(auto[x,c]:G[i]){ res.push_back(x); } return res; } }; int main() { riano_; //ll ans = 0; ll N; cin >> N; ans = N*(N-1); //main関数内で tree tr(N+1); rep(i,N-1){ ll a,b,c; cin >> a >> b >> c; tr.unite(a,b,c); } tr.dfs(1); for(auto[x,c]:top_cc){ ans -= (c-1)*top_ss[x]; } cout << ans << endl; }