#include using namespace std; #define int long long #define REP(i,m,n) for(int i=(m);i<(n);i++) #define rep(i,n) REP(i,0,n) #define pb push_back #define all(a) a.begin(),a.end() #define rall(c) (c).rbegin(),(c).rend() #define mp make_pair #define endl '\n' //#define vec vector //#define mat vector > #define fi first #define se second #define double long double typedef long long ll; typedef unsigned long long ull; typedef pair pll; //typedef long double ld; typedef complex Complex; const ll INF=1e9+7; const ll MOD=998244353; const ll inf=INF*INF; const ll mod=MOD; const ll MAX=200010; const double PI=acos(-1.0); typedef vector > mat; typedef vector vec; vector >ma(MAX); vector >G(MAX); ll sum=0; vectorsz(MAX); ll n; void dfs(ll i,ll p,ll s){ maptmp; ll cnt=1; for(auto e:G[i]){ if(e.fi==p)continue; dfs(e.fi,i,s^e.se); cnt+=sz[e.fi]; } for(auto e:G[i]){ if(e.fi==p)continue; ll z=sz[e.fi]; sum+=ma[e.fi][s].fi*(cnt-z); if(ma[e.fi].size()>tmp.size()){ swap(tmp,ma[e.fi]); } for(auto u:ma[e.fi]){ if(u.fi!=s)sum+=u.se.fi*tmp[u.fi].se+u.se.se*tmp[u.fi].fi; tmp[u.fi].fi+=u.se.fi; tmp[u.fi].se+=u.se.se; } } sum+=tmp[s].se; sum+=(n-cnt)*tmp[s].fi; tmp[s].fi=1; tmp[s].se=cnt; swap(ma[i],tmp); sz[i]=cnt; } void solve(){ cin>>n; rep(i,n-1){ ll a,b,c;cin>>a>>b>>c; a--;b--; G[a].pb(mp(b,c)); G[b].pb(mp(a,c)); } dfs(0,-1,0); ll ans=n*(n-1)-sum; cout<