#include //#include //#include using namespace std; #define DEBUG(x) cerr<<#x<<": "< #define vl vector #define vii vector< vector > #define vll vector< vector > #define vs vector #define pii pair #define pis pair #define psi pair #define pll pair template pair operator+(const pair &s, const pair &t) { return pair(s.first + t.first, s.second + t.second); } template pair operator-(const pair &s, const pair &t) { return pair(s.first - t.first, s.second - t.second); } template ostream& operator<<(ostream& os, pair p) { os << "(" << p.first << ", " << p.second << ")"; return os; } #define X first #define Y second #define rep(i,n) for(int i=0;i<(n);i++) #define rep1(i,n) for(int i=1;i<=(n);i++) #define rrep(i,n) for(int i=(n)-1;i>=0;i--) #define rrep1(i,n) for(int i=(n);i>0;i--) #define REP(i,a,b) for(int i=a;i bool chmax(T &a, const T &b) { if (a bool chmin(T &a, const T &b) { if (a>b) { a = b; return 1; } return 0; } #define UNIQUE(v) v.erase(std::unique(v.begin(), v.end()), v.end()); const ll inf = 1000000001; const ll INF = (ll)1e18 + 1; const long double pi = 3.1415926535897932384626433832795028841971L; #define Sp(p) cout<> G; //�O���t�̗אڃ��X�g�\�� vector isused; //���̒��_�����łɕ����Ɏg���Ă��邩 vi subtreesize; //�����؂̃T�C�Y,�X�V����� vi parent; vi centroid; //�d�S�����ɒlj�����Ă��� CentroidDecomposition(int n) : n(n) { G.resize(n); isused.resize(n); subtreesize.resize(n); parent.resize(n); } void add_edge(int u, int v, int c) { G[u].push_back(pii(v, c)); G[v].push_back(pii(u, c)); } void dfs(int now, int par, pii c, map &mp) { mp[c]++; rep (i, G[now].size()) { int ch = G[now][i].first; int color = G[now][i].second; if (ch == par) continue; pii c2 = c; if (c2.first == color or c2.second == color) {} else if (c2.first == -1) c2.first = color; else if (c2.second == -1) { c2.second = color; if (c2.first > c2.second) swap(c2.first, c2.second); } else continue; dfs(ch, now, c2, mp); } } int search_centroid(int now, int par, int size) { subtreesize[now] = 1; parent[now] = par; bool iscentroid = true; for (int i = 0; i < G[now].size(); i++) { int child = G[now][i].first; if (child == par) continue; if (isused[child]) continue; int child_size = search_centroid(child, now, size); if (child_size > size / 2) iscentroid = false; subtreesize[now] += child_size; } if (size - subtreesize[now] > size / 2) { iscentroid = false; } if (iscentroid) { centroid.push_back(now); } return subtreesize[now]; } //���ɂ���ĕς�� map solve(int root, int par, int size) { //DEBUG(pii(root, par)); search_centroid(root, -1, size); int cent = centroid.back(); //DEBUG(cent); isused[cent] = true; map sum; vi mono(k), num(k); ll mono_sum = 0; rep(i, G[cent].size()) { int ch = G[cent][i].first; int color = G[cent][i].second; if (isused[ch]) { continue; } map mp; if (parent[ch] == cent) { mp = solve(ch, cent, subtreesize[ch]); } else { mp = solve(ch, cent, size - subtreesize[cent]); } map mp2; for (auto itr = mp.begin(); itr != mp.end(); itr++) { pii c = itr->first; if (c.first == color or c.second == color) { mp2[c] += itr->second; } else if (c.first == -1) { c.first = color; mp2[c] += itr->second; } else if (c.second == -1) { c.second = color; if (c.first > c.second) swap(c.first, c.second); mp2[c] += itr->second; } } for (auto itr = mp2.begin(); itr != mp2.end(); itr++) { pii c = itr->first; //UG(c); if (c.second == -1) { int x = c.first; //DEBUG(make_pair(pii(mono[x], num[x]), mono_sum)); ans += (mono_sum - mono[x] + num[x]) * itr->second; } else { int x = c.first; int y = c.second; //DEBUG(make_pair(pii(mono[x], mono[y]), sum[pii(x, y)])); ans += (1 + mono[x] + mono[y] + sum[pii(x, y)]) * itr->second; } } for (auto itr = mp2.begin(); itr != mp2.end(); itr++) { pii c = itr->first; sum[itr->first] += itr->second; if (c.second == -1) { int x = c.first; mono[x] += itr->second; mono_sum += itr->second; } else { int x = c.first, y = c.second; num[x] += itr->second; num[y] += itr->second; } } } map res; dfs(root, par, pii(-1, -1), res); /* DEBUG(cent); for (auto itr = res.begin(); itr != res.end(); itr++) { DEBUG(*itr); } //*/ return res; } }; signed main() { fio(); cin >> n >> k; CentroidDecomposition tree(n); rep (i, n - 1) { int u, v, c; cin >> u >> v >> c; u--; v--; c--; tree.add_edge(u, v, c); } tree.solve(0, -1, n); cout << ans << endl; }