#include using namespace std; /*^ vector extensions */ template vector concat(vector a, vector b) { a.insert(a.end(), b.begin(), b.end()); return a; } template struct _Matrix_type { typedef vector::type> type; }; template struct _Matrix_type { typedef T type; }; template struct _Matrix { static auto build(size_t s) { return vector(s); } template static auto build(size_t f, Args... args) { return vector::type>(f, _Matrix::build(args...)); } }; template auto buildMatrix(Args... args) { return _Matrix::build(args...); } /* vector extensions $*/ /*^ generic definitions */ template struct _RecurFun : F { _RecurFun(F&& f) : F(forward(f)) {} template decltype(auto) operator()(Args&&... args) const { return F::operator()(*this, forward(args)...); } }; template decltype(auto) RecurFun(F&& f) { return _RecurFun { forward(f) }; } /* generic definitions $*/ /*^ pbds */ #if __has_include() #include using namespace __gnu_pbds; const int RandomIntSeed = chrono::high_resolution_clock::now().time_since_epoch().count(); struct HashInt { int operator()(int x) const { return x ^ RandomIntSeed; } }; template> using Gmap = gp_hash_table; #endif /* pbds $*/ struct Centroid { vector> graph; vector size, dead; Centroid(const vector> &g) : size(g.size()), dead(g.size()), graph(g) {} vector findCentroids(int root) { vector centroids; RecurFun([&](auto buildSize, int u, int p) -> void { size[u] = 1; for (int v : graph[u]) if (v != p && !dead[v]) buildSize(v, u), size[u] += size[v]; })(root, -1); RecurFun([&](auto buildCentroids, int u, int p) -> void { int ok = (size[root] - size[u]) * 2 <= size[root]; for (int v : graph[u]) if (v != p && !dead[v]) buildCentroids(v, u), ok &= size[v] * 2 <= size[root]; if (ok) centroids.push_back(u); })(root, -1); assert(1 <= centroids.size() && centroids.size() <= 2); return centroids; } }; int main() { ios::sync_with_stdio(false); int N, K; { cin >> N >> K; } vector> G(N); vector> C(N); { for (int u = 0; u + 1 < N; ++u) { int U, V, Z; { cin >> U >> V >> Z; --U, --V, --Z; } G[U].push_back(V); G[V].push_back(U); C[U][V] = Z; C[V][U] = Z; } } int64_t res = 0; { Centroid cd(G); queue que({ cd.findCentroids(0)[0] }); int all = 0; // single color vector cnt(K), cntx(K); vector> cnt2(K); while (que.size()) { int u = que.front(); que.pop(); for (int v : G[u]) if (!cd.dead[v]) { RecurFun([&](auto count, int u, int p, int a, int b) -> void { for (int v : G[u]) if (v != p && !cd.dead[v]) { if (a == C[u][v] || b == C[u][v]) count(v, u, a, b); else if (a == -1) count(v, u, C[u][v], b); } if (a == -1) res += (all - cnt[b]) + cntx[b]; else res += 1 + cnt[a] + cnt[b] + cnt2[a][b]; })(v, u, -1, C[u][v]); RecurFun([&](auto update, int u, int p, int a, int b) -> void { for (int v : G[u]) if (v != p && !cd.dead[v]) { if (a == C[u][v] || b == C[u][v]) update(v, u, a, b); else if (a == -1) update(v, u, C[u][v], b); } if (a == -1) ++cnt[b], ++all; else ++cntx[a], ++cntx[b], ++cnt2[a][b], ++cnt2[b][a]; })(v, u, -1, C[u][v]); } for (int v : G[u]) if (!cd.dead[v]) { RecurFun([&](auto update, int u, int p, int a, int b) -> void { for (int v : G[u]) if (v != p && !cd.dead[v]) { if (a == C[u][v] || b == C[u][v]) update(v, u, a, b); else if (a == -1) update(v, u, C[u][v], b); } if (a == -1) --cnt[b], --all; else --cntx[a], --cntx[b], --cnt2[a][b], --cnt2[b][a]; })(v, u, -1, C[u][v]); } cd.dead[u] = 1; for (int v : G[u]) if (!cd.dead[v]) que.push(cd.findCentroids(v)[0]); } } cout << res << endl; }