#define _USE_MATH_DEFINES #include using namespace std; #define FOR(i,m,n) for(int i=(m);i<(n);++i) #define REP(i,n) FOR(i,0,n) #define ALL(v) (v).begin(),(v).end() using ll = long long; const int INF = 0x3f3f3f3f; const ll LINF = 0x3f3f3f3f3f3f3f3fLL; const double EPS = 1e-8; const int MOD = 1000000007; // const int MOD = 998244353; const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1}; const int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1}; template inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; } template inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; } struct IOSetup { IOSetup() { cin.tie(nullptr); ios_base::sync_with_stdio(false); cout << fixed << setprecision(20); } } iosetup; using CostType = int; struct Edge { int src, dst; CostType cost; Edge(int src, int dst, CostType cost = 0) : src(src), dst(dst), cost(cost) {} inline bool operator<(const Edge &rhs) const { return cost != rhs.cost ? cost < rhs.cost : dst != rhs.dst ? dst < rhs.dst : src < rhs.src; } inline bool operator<=(const Edge &rhs) const { return !(rhs < *this); } inline bool operator>(const Edge &rhs) const { return rhs < *this; } inline bool operator>=(const Edge &rhs) const { return !(*this < rhs); } }; struct CentroidDecomposition { int root; vector > comp; vector par; CentroidDecomposition(const vector > &graph) : graph(graph) { int n = graph.size(); alive.assign(n, true); subtree.resize(n); comp.resize(n); par.assign(n, -1); root = build(0); } private: const vector > graph; vector alive; vector subtree; int build(int s) { int centroid = search_centroid(-1, s, calc_subtree(-1, s) / 2); alive[centroid] = false; for (const Edge &e : graph[centroid]) { if (alive[e.dst]) { comp[centroid].emplace_back(build(e.dst)); par[e.dst] = centroid; } } alive[centroid] = true; return centroid; } int calc_subtree(int par, int ver) { subtree[ver] = 1; for (const Edge &e : graph[ver]) { if (e.dst != par && alive[e.dst]) subtree[ver] += calc_subtree(ver, e.dst); } return subtree[ver]; } int search_centroid(int par, int ver, int half) { for (const Edge &e : graph[ver]) { if (e.dst != par && alive[e.dst]) { if (subtree[e.dst] > half) return search_centroid(ver, e.dst, half); } } return ver; } }; int main() { int n, k; cin >> n >> k; vector > graph(n); REP(_, n - 1) { int u, v, c; cin >> u >> v >> c; --u; --v; --c; graph[u].emplace_back(u, v, c); graph[v].emplace_back(v, u, c); } CentroidDecomposition cd(graph); vector visited(n, false); ll ans = 0; function dfs = [&](int ver) { visited[ver] = true; map mp1, two; ll one = 0; map, ll> mp2; for (const Edge &e : graph[ver]) { if (visited[e.dst]) continue; map nx_mp1; map, ll> nx_mp2; map now; now[e.cost] = 1; ans += one - (mp1.count(e.cost) == 1 ? mp1[e.cost] : 0) + two[e.cost]; // cout << e.dst << ' ' << one << ' ' << ans << '\n'; nx_mp1[e.cost] = 1; function dfs2 = [&](int p, int v) { for (const Edge &d : graph[v]) { if (d.dst == p || visited[d.dst]) continue; ++now[d.cost]; if (now.size() == 1) { int x = now.begin()->first; ans += one - (mp1.count(x) == 1 ? mp1[x] : 0) + two[x]; ++nx_mp1[x]; } else if (now.size() == 2) { int x = now.begin()->first, y = next(now.begin())->first; ans += (mp1.count(x) == 1 ? mp1[x] : 0) + (mp1.count(y) == 1 ? mp1[y] : 0) + (mp2.count({x, y}) == 1 ? mp2[{x, y}] : 0) + 1; ++nx_mp2[{x, y}]; } // cout << d.dst << ' ' << ans << '\n'; if (now.size() <= 2) dfs2(v, d.dst); --now[d.cost]; if (now[d.cost] == 0) now.erase(d.cost); } }; dfs2(ver, e.dst); for (auto pr : nx_mp1) { mp1[pr.first] += pr.second; one += pr.second; } for (auto pr : nx_mp2) { mp2[pr.first] += pr.second; two[pr.first.first] += pr.second; two[pr.first.second] += pr.second; } } // cout << ver << ' ' << ans << '\n'; for (int e : cd.comp[ver]) dfs(e); }; dfs(cd.root); cout << ans << '\n'; return 0; }