#include using namespace std; using ll = long long; using PII = pair; #define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i) #define REP(i, n) FOR(i, 0, n) #define ALL(x) x.begin(), x.end() template void chmin(T &a, const T &b) { a = min(a, b); } template void chmax(T &a, const T &b) { a = max(a, b); } struct FastIO {FastIO() { cin.tie(0); ios::sync_with_stdio(0); }}fastiofastio; #ifdef DEBUG_ #include "../program_contest_library/memo/dump.hpp" #else #define dump(...) #endif const ll INF = 1LL<<60; int main(void) { ll n, k; cin >> n >> k; vector> g(n); REP(i, n-1) { ll u, v, c; cin >> u >> v >> c; u--, v--, c--; g[u].push_back({v, c}); g[v].push_back({u, c}); } vector sz(n), dead(n); auto find_centroid = [&](ll root) { auto get_size = [&](auto &&self, ll v, ll p) -> void { sz[v] = 1; for(auto to: g[v]) if(to.first != p && !dead[to.first]) { self(self, to.first, v); sz[v] += sz[to.first]; } }; get_size(get_size, root, -1); auto dfs = [&](auto &&self, ll v, ll p) -> ll { for(auto to: g[v]) if(to.first != p && !dead[to.first]) { if(sz[to.first] > sz[root]/2) return self(self, to.first, v); } return v; }; return dfs(dfs, root, -1); }; auto get_edges = [&](ll v) -> vector> { vector> ret; auto dfs = [&](auto &&self, ll v, ll p, PII col) -> void { PII now = col; for(auto to: g[v]) if(to.first != p && !dead[to.first]) { if(to.second == now.first || to.second == now.second) { ret.back().push_back(now); self(self, to.first, v, now); } else if(now.first == -1) { now.first = to.second; if(now.first > now.second) swap(now.first, now.second); ret.back().push_back(now); self(self, to.first, v, now); } } }; for(auto to: g[v]) if(!dead[to.first]) { ret.emplace_back(); ret.back().emplace_back(-1, to.second); dfs(dfs, to.first, -1, PII(-1, to.second)); sort(ALL(ret.back())); } return ret; }; ll ans = 0, ans2 = 0; auto centroid_decomposition = [&](auto &&self, ll root) -> void { ll c = find_centroid(root); dead[c] = true; for(auto to: g[c]) if(!dead[to.first]) { self(self, to.first); } // (色1,色2) を含むパスとマージさせられるパスは (色1,色2) or (-1,色1) or (-1,色2) のどれか // (色1,色2) のペアが何個あるのか数えたい auto colors = get_edges(c); vector flat; for(auto v: colors) for(auto p: v) flat.push_back(p); sort(ALL(flat)); dump(c); dump(colors); dump(flat); dump(ans, "pre"); for(auto v: colors) for(auto p: v) { dump(p, ans, ans2); // 2色存在するペアと合わせられるものを数える if(p.first != -1) { // 頂点cまでのパスを数える ans++; // 同じ色のペア { auto [lb, ub] = equal_range(ALL(flat), p); ans2 += ub - lb; auto [lb2, ub2] = equal_range(ALL(v), p); ans2 -= ub2 - lb2; // 同じ方向のパスから2回選ぶのはだめ } // (-1,色1) { auto [lb, ub] = equal_range(ALL(flat), PII(-1, p.first)); ans2 += ub - lb; auto [lb2, ub2] = equal_range(ALL(v), PII(-1, p.first)); ans2 -= ub2 - lb2; } // (-1,色2) { auto [lb, ub] = equal_range(ALL(flat), PII(-1, p.second)); ans2 += ub - lb; auto [lb2, ub2] = equal_range(ALL(v), PII(-1, p.second)); ans2 -= ub2 - lb2; } } // (-1, 色1) と合わせられるやつを探す else { // (-1, 色i) (色i!=色1) { auto ub = upper_bound(ALL(flat), PII(-1, INF)); auto lb = lower_bound(ALL(flat), PII(-1, 0)); ans2 += ub - lb; auto ub2 = upper_bound(ALL(v), PII(-1, INF)); auto lb2 = lower_bound(ALL(v), PII(-1, 0)); ans2 -= ub2 - lb2; auto [lb3, ub3] = equal_range(ALL(flat), PII(-1, p.second)); ans2 -= ub3 - lb3; auto [lb4, ub4] = equal_range(ALL(v), PII(-1, p.second)); ans2 += ub4 - lb4; } // (色1, 色*) { auto ub = upper_bound(ALL(flat), PII(p.second, INF)); auto lb = lower_bound(ALL(flat), PII(p.second, 0)); ans2 += ub - lb; auto ub2 = upper_bound(ALL(v), PII(p.second, INF)); auto lb2 = lower_bound(ALL(v), PII(p.second, 0)); ans2 -= ub2 - lb2; } } dump(ans, ans2); } dump(ans, "nxt"); dead[c] = false; }; centroid_decomposition(centroid_decomposition, 0); cout << ans + ans2/2 << endl; return 0; }