#include using namespace std; #define rep(i,m,n) for(int (i)=(int)(m);i<(int)(n);++i) #define rep2(i,m,n) for(int (i)=(int)(n)-1;i>=(int)(m);--i) #define REP(i,n) rep(i,0,n) #define REP2(i,n) rep2(i,0,n) #define FOR(i,c) for(decltype((c).begin())i=(c).begin();i!=(c).end();++i) #define all(hoge) (hoge).begin(),(hoge).end() #define en '\n' using ll = long long; using ull = unsigned long long; template using vec = vector; template using vvec = vector>; typedef pair P; constexpr long long INF = 1LL << 60; constexpr int INF_INT = 1 << 25; constexpr long long MOD = (ll) 1e9 + 7; //constexpr long long MOD = 998244353LL; using ld=long double; static const ld pi = 3.141592653589793L; typedef vector Array; typedef vector Matrix; template inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } template inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } struct Edge { ll to, cap, rev; Edge(ll _to, ll _cap, ll _rev) { to = _to; cap = _cap; rev = _rev; } }; using Edges = vector; using Graph = vector; void add_edge(Graph& G, ll from, ll to, ll cap, bool revFlag, ll revCap) { G[from].push_back(Edge(to, cap, (ll)G[to].size())); if (revFlag)G[to].push_back(Edge(from, revCap, (ll)G[from].size() - 1)); } class CentroidDecomposition { Graph& g; ll n; vector sz, dead; public: CentroidDecomposition(Graph& _g) :g(_g),n(g.size()),sz(n, 1), dead(n, 0) {} ll szdfs(ll v, ll par) { sz[v] = 1; for (auto e : g[v]) if (e.to != par && !dead[e.to]) sz[v] += szdfs(e.to, v); return sz[v]; } void find(ll v, ll par, ll tmp, Array& cs) { ll ok = 1; for (auto e : g[v]) { if (e.to == par || dead[e.to]) continue; find(e.to, v, tmp, cs); ok &= (sz[e.to] <= tmp / 2); } ok &= (tmp - sz[v] <= tmp / 2); if (ok) cs.push_back(v); }; vector build(ll r) { ll tmp = szdfs(r, -1); vector cs; find(r, -1, tmp, cs); return cs; } inline ll get_sz(ll v) { return sz[v]; } inline void disable(ll v) { dead[v] = 1; } inline void enable(ll v) { dead[v] = 0; } inline ll alive(ll v) { return !dead[v]; } }; void solve(){ ll n,k; cin>>n>>k; Graph g(n); REP(i,n-1){ ll a,b,c; cin>>a>>b>>c; a--;b--; add_edge(g,a,b,c,true,c); } CentroidDecomposition cd(g); map mp; ll sum1=0, spre1=0; map p1,pre1; map,ll> p2,pre2; map p21,pre21; auto init =[&](){ mp.clear(); sum1=0; p1.clear(); p2.clear(); p21.clear(); }; auto initpre = [&](){ spre1=0; pre1.clear(); pre2.clear(); pre21.clear(); }; auto dfs = [&](auto &&self, int v, int p, int x){ mp[x]++; if(mp.size()==1){ auto it=mp.begin(); p1[it->first]++; sum1++; }else if(mp.size()==2){ auto it=mp.begin(); ll a=it->first; it++; ll b=it->first; p2[{a,b}]++; p21[a]++; p21[b]++; }else{ return; } if(cd.alive(v)){ for(auto e:g[v]){ if(e.to==p) continue; self(self, e.to, v, e.cap); } } mp[x]--; if(mp[x]==0) mp.erase(x); }; queue que; que.push(0); ll ans=0; while(que.size()){ int v=que.front();que.pop(); int r=cd.build(v)[0];//重心 initpre(); for(auto e:g[r]){ if(cd.alive(e.to)){ que.push(e.to); } init(); dfs(dfs, e.to, r, e.cap); //count for(auto i:p1){ ans+=(spre1-pre1[i.first])*i.second; ans+=(pre21[i.first])*i.second; } for(auto i:p2){ ans+=pre2[i.first]*i.second; ans+=pre1[i.first.first]*i.second; ans+=pre1[i.first.second]*i.second; } //add spre1+=sum1; for(auto i:p1){ //cout<>t;REP(i,t) solve(); return 0; }