#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using lint = long long; using pint = pair; using plint = pair; struct fast_ios { fast_ios(){ cin.tie(nullptr), ios::sync_with_stdio(false), cout << fixed << setprecision(20); }; } fast_ios_; #define ALL(x) (x).begin(), (x).end() #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, const V& val, int len) { vec.assign(len, val); } template void ndarray(vector& vec, const V& val, int len, Args... args) { vec.resize(len), for_each(begin(vec), end(vec), [&](T& v) { ndarray(v, val, args...); }); } template bool chmax(T &m, const T q) { return m < q ? (m = q, true) : false; } template bool chmin(T &m, const T q) { return m > q ? (m = q, true) : false; } int floor_lg(long long x) { return x <= 0 ? -1 : 63 - __builtin_clzll(x); } template std::pair operator+(const std::pair &l, const std::pair &r) { return std::make_pair(l.first + r.first, l.second + r.second); } template std::pair operator-(const std::pair &l, const std::pair &r) { return std::make_pair(l.first - r.first, l.second - r.second); } template std::vector sort_unique(std::vector vec) { sort(vec.begin(), vec.end()), vec.erase(unique(vec.begin(), vec.end()), vec.end()); return vec; } template int arglb(const std::vector &v, const T &x) { return std::distance(v.begin(), std::lower_bound(v.begin(), v.end(), x)); } template int argub(const std::vector &v, const T &x) { return std::distance(v.begin(), std::upper_bound(v.begin(), v.end(), x)); } template IStream &operator>>(IStream &is, std::vector &vec) { for (auto &v : vec) is >> v; return is; } template OStream &operator<<(OStream &os, const std::vector &vec); template OStream &operator<<(OStream &os, const std::array &arr); template OStream &operator<<(OStream &os, const std::unordered_set &vec); template OStream &operator<<(OStream &os, const pair &pa); template OStream &operator<<(OStream &os, const std::deque &vec); template OStream &operator<<(OStream &os, const std::set &vec); template OStream &operator<<(OStream &os, const std::multiset &vec); template OStream &operator<<(OStream &os, const std::unordered_multiset &vec); template OStream &operator<<(OStream &os, const std::pair &pa); template OStream &operator<<(OStream &os, const std::map &mp); template OStream &operator<<(OStream &os, const std::unordered_map &mp); template OStream &operator<<(OStream &os, const std::tuple &tpl); template OStream &operator<<(OStream &os, const std::vector &vec) { os << '['; for (auto v : vec) os << v << ','; os << ']'; return os; } template OStream &operator<<(OStream &os, const std::array &arr) { os << '['; for (auto v : arr) os << v << ','; os << ']'; return os; } #if __cplusplus >= 201703L template std::istream &operator>>(std::istream &is, std::tuple &tpl) { std::apply([&is](auto &&... args) { ((is >> args), ...);}, tpl); return is; } template OStream &operator<<(OStream &os, const std::tuple &tpl) { os << '('; std::apply([&os](auto &&... args) { ((os << args << ','), ...);}, tpl); return os << ')'; } #endif template OStream &operator<<(OStream &os, const std::unordered_set &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template OStream &operator<<(OStream &os, const std::deque &vec) { os << "deq["; for (auto v : vec) os << v << ','; os << ']'; return os; } template OStream &operator<<(OStream &os, const std::set &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template OStream &operator<<(OStream &os, const std::multiset &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template OStream &operator<<(OStream &os, const std::unordered_multiset &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template OStream &operator<<(OStream &os, const std::pair &pa) { return os << '(' << pa.first << ',' << pa.second << ')'; } template OStream &operator<<(OStream &os, const std::map &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; } template OStream &operator<<(OStream &os, const std::unordered_map &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; } #ifdef HITONANODE_LOCAL const string COLOR_RESET = "\033[0m", BRIGHT_GREEN = "\033[1;32m", BRIGHT_RED = "\033[1;31m", BRIGHT_CYAN = "\033[1;36m", NORMAL_CROSSED = "\033[0;9;37m", RED_BACKGROUND = "\033[1;41m", NORMAL_FAINT = "\033[0;2m"; #define dbg(x) std::cerr << BRIGHT_CYAN << #x << COLOR_RESET << " = " << (x) << NORMAL_FAINT << " (L" << __LINE__ << ") " << __FILE__ << COLOR_RESET << std::endl #define dbgif(cond, x) ((cond) ? std::cerr << BRIGHT_CYAN << #x << COLOR_RESET << " = " << (x) << NORMAL_FAINT << " (L" << __LINE__ << ") " << __FILE__ << COLOR_RESET << std::endl : std::cerr) #else #define dbg(x) ((void)0) #define dbgif(cond, x) ((void)0) #endif struct CentroidDecomposition { int NO_PARENT = -1; 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. CentroidDecomposition(int v = 0) : V(v), E(0), to(v), par(v, NO_PARENT), chi(v), subtree_size(v) {} CentroidDecomposition(const std::vector> &to_) : CentroidDecomposition(to_.size()) { for (int i = 0; i < V; i++) { for (auto j : to_[i]) { if (i < j) { add_edge(i, j); } } } } void add_edge(int v1, int v2) { to[v1].emplace_back(v2, E), to[v2].emplace_back(v1, E), 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); } std::pair 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]); } std::vector _cd_vertices; void _centroid_decomposition(int now) { fix_root(now); now = detect_centroids(now).first; _cd_vertices.emplace_back(now); /* do something */ for (auto p : to[now]) { int nxt, eid; std::tie(nxt, eid) = p; if (available_edge[eid] == 0) continue; available_edge[eid] = 0; _centroid_decomposition(nxt); } } std::vector centroid_decomposition(int x) { _cd_vertices.clear(); _centroid_decomposition(x); return _cd_vertices; } }; int main() { int N, K; cin >> N >> K; CentroidDecomposition cd(N); vector> to(N); REP(e, N - 1) { int u, v, c; cin >> u >> v >> c; --u, --v, --c; cd.add_edge(u, v); to.at(u).emplace_back(v, c); to.at(v).emplace_back(u, c); } vector alive(N, 1); lint ret = 0; for (const int root : cd.centroid_decomposition(0)) { alive.at(root) = 0; unordered_map cnt1; int cnt1sum = 0; map cnt2; unordered_map cnt2half; vector st; auto rec = [&](auto &&self, int now, int prv, int c1, int c2) -> void { // assert(c1 > c2); if (c1 >= 0 and c2 >= 0) ++ret; if (c2 >= 0) { if (cnt2.count(minmax(c1, c2))) ret += cnt2[minmax(c1, c2)]; if (cnt1.count(c1)) ret += cnt1[c1]; if (cnt1.count(c2)) ret += cnt1[c2]; } else { ret += cnt1sum - (cnt1.count(c1) ? cnt1[c1] : 0); if (cnt2half.count(c1)) ret += cnt2half[c1]; } for (auto [nxt, nxtc] : to[now]) { if (nxt == prv) continue; if (!alive.at(nxt)) continue; if (c2 >= 0 and c1 != nxtc and c2 != nxtc) continue; if (nxtc == c1 or nxtc == c2) { self(self, nxt, now, c1, c2); } else if (c2 == -1 and c1 != nxtc) { self(self, nxt, now, c1, nxtc); } else { exit(1); } } st.emplace_back(c1, c2); }; for (auto [nxt, c] : to.at(root)) { if (!alive.at(nxt)) continue; rec(rec, nxt, root, c, -1); for (auto [c1, c2] : st) { if (c2 >= 0) cnt2[minmax(c1, c2)]++, cnt2half[c1]++, cnt2half[c2]++; if (c2 < 0) cnt1[c1]++, cnt1sum++; } st.clear(); } dbg(make_tuple(root, ret)); } cout << ret << endl; }