#include using namespace std; using lint = long long int; using pint = pair; using plint = pair; struct fast_ios { fast_ios(){ cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_; #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((lint)(x).size()) #define POW2(n) (1LL << (n)) #define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i=i##_begin_;i--) #define REP(i, n) FOR(i,0,n) #define IREP(i, n) IFOR(i,0,n) template void ndarray(vector &vec, int len) { vec.resize(len); } template void ndarray(vector &vec, int len, Args... args) { vec.resize(len); for (auto &v : vec) ndarray(v, args...); } template bool mmax(T &m, const T q) { if (m < q) {m = q; return true;} else return false; } template bool mmin(T &m, const T q) { if (m > q) {m = q; return true;} else return false; } template pair operator+(const pair &l, const pair &r) { return make_pair(l.first + r.first, l.second + r.second); } template pair operator-(const pair &l, const pair &r) { return make_pair(l.first - r.first, l.second - r.second); } template istream &operator>>(istream &is, vector &vec){ for (auto &v : vec) is >> v; return is; } ///// This part below is only for debug, not used ///// template ostream &operator<<(ostream &os, const vector &vec){ os << "["; for (auto v : vec) os << v << ","; os << "]"; return os; } template ostream &operator<<(ostream &os, const deque &vec){ os << "deq["; for (auto v : vec) os << v << ","; os << "]"; return os; } template ostream &operator<<(ostream &os, const set &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template ostream &operator<<(ostream &os, const unordered_set &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template ostream &operator<<(ostream &os, const multiset &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template ostream &operator<<(ostream &os, const unordered_multiset &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template ostream &operator<<(ostream &os, const pair &pa){ os << "(" << pa.first << "," << pa.second << ")"; return os; } template ostream &operator<<(ostream &os, const map &mp){ os << "{"; for (auto v : mp) os << v.first << "=>" << v.second << ","; os << "}"; return os; } template ostream &operator<<(ostream &os, const unordered_map &mp){ os << "{"; for (auto v : mp) os << v.first << "=>" << v.second << ","; os << "}"; return os; } #define dbg(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ") " << __FILE__ << endl; ///// END ///// vector> edge; vector color; vector uv; lint ret; struct Tree { int NO_PARENT = -1; using pint = std::pair; int V; int E; std::vector> to; // (node_id, edge_id) std::vector par; // parent node_id par[root] = -1 std::vector> chi; // children id's std::vector subtree_size; // size of each subtree std::vector available_edge; // If 0, ignore the corresponding edge. Tree() : Tree(0) {} Tree(int v) : V(v), E(0), to(v), par(v, NO_PARENT), chi(v), subtree_size(v) {} std::vector color; void add_edge(int v1, int v2, int c) { to[v1].emplace(v2, E); to[v2].emplace(v1, E); color.emplace_back(c); E++; available_edge.emplace_back(1); } int _dfs_fixroot(int now, int prv) { chi[now].clear(); subtree_size[now] = 1; for (auto nxt : to[now]) { if (nxt.first != prv and available_edge[nxt.second]) { par[nxt.first] = now; chi[now].push_back(nxt.first); subtree_size[now] += _dfs_fixroot(nxt.first, now); } } return subtree_size[now]; } void fix_root(int root) { par[root] = NO_PARENT; _dfs_fixroot(root, -1); } //// Centroid Decpmposition //// std::vector centroid_cand_tmp; void _dfs_detect_centroids(int now, int prv, int n) { bool is_centroid = true; for (auto nxt : to[now]) { if (nxt.first != prv and available_edge[nxt.second]) { _dfs_detect_centroids(nxt.first, now, n); if (subtree_size[nxt.first] > n / 2) is_centroid = false; } } if (n - subtree_size[now] > n / 2) is_centroid = false; if (is_centroid) centroid_cand_tmp.push_back(now); } pint detect_centroids(int r) // ([centroid_node_id1], ([centroid_node_id2]|-1)) { centroid_cand_tmp.clear(); while (par[r] != NO_PARENT) r = par[r]; int n = subtree_size[r]; _dfs_detect_centroids(r, -1, n); if (centroid_cand_tmp.size() == 1) return std::make_pair(centroid_cand_tmp[0], -1); else return std::make_pair(centroid_cand_tmp[0], centroid_cand_tmp[1]); } lint ret; unordered_map mp; void cddfs(int now, int prv, int c1, int c2) { for (auto p : to[now]) { int nxt = p.first, eid = p.second; if (nxt == prv or available_edge[eid] == 0) continue; int c = color[eid]; int c2new = c2; if (c2 >= 0 and c != c2 and c != c1) continue; if (c != c1) c2new = c; mp[c2new]++; cddfs(nxt, now, c1, c2new); } } void centroid_decomposition(int now) { fix_root(now); pint p = detect_centroids(now); int s = p.first, t = p.second, steid = -1; if (t < 0) { fix_root(s); int maxi_size = 0; for (auto p : to[s]) { int nxt, eid; std::tie(nxt, eid) = p; // if (available_edge[eid] == 0) continue; if (mmax(maxi_size, subtree_size[nxt])) { t = nxt, steid = eid; } } } else { for (auto p : to[s]) { if (p.first == t) steid = p.second; } } if (t < 0) return; to[s].erase(pint(t, steid)); to[t].erase(pint(s, steid)); mp.clear(); mp[-1] = 1; cddfs(s, t, color[steid], -1); unordered_map mps = mp; mp.clear(); mp[-1] = 1; cddfs(t, s, color[steid], -1); lint nb_mps_0 = mps[-1]; for (auto p : mp) { if (p.first >= 0 and mps.count(p.first)) ret += 1LL * p.second * mps[p.first]; if (p.first >= 0) ret += 1LL * p.second * nb_mps_0; } lint nb_mp_0 = mp[-1]; for (auto p : mps) { if (p.first >= 0) ret += nb_mp_0 * p.second; } centroid_decomposition(s); centroid_decomposition(t); } }; int main() { int N, K; cin >> N >> K; Tree tree(N); REP(e, N - 1) { int u, v, c; cin >> u >> v >> c; tree.add_edge(u - 1, v - 1, c); } tree.ret = 0; tree.centroid_decomposition(0); cout << tree.ret << endl; }