#include #include #include #include #include using namespace std; using Graph = vector< vector< pair > >; int main() { int N, K; scanf("%d%d", &N, &K); Graph G(N); for(int i=0; i+1 num(N), dead(N); auto get_size = [&](auto&& f, int cur, int par) -> void { num[cur] = 1; for(auto e : G[cur]) { int to = e.first; if(to == par or dead[to]) continue; f(f, to, cur); num[cur] += num[to]; } }; auto find_centroid = [&](int root) { get_size(get_size, root, -1); int V = num[root]; auto dfs = [&](auto&& f, int cur, int par) -> int { for(auto e : G[cur]) { int to = e.first; if(to == par or dead[to]) continue; if(num[to] > V / 2) return f(f, to, cur); } return cur; }; return dfs(dfs, root, -1); }; auto get_edges = [&](int cur) -> vector< vector< pair > > { vector< vector< pair > > res; auto dfs = [&](auto &&f, int cur, int par, pair edge) -> void { for(auto e : G[cur]) { pair tmp = edge; int to, color; tie(to, color) = e; if(to == par or dead[to]) continue; if(tmp.first == color or tmp.second == color) { res.back().emplace_back(tmp); f(f, to, cur, tmp); } else if(tmp.first == -1) { tmp.first = color; if(tmp.first > tmp.second) swap(tmp.first, tmp.second); res.back().emplace_back(tmp); f(f, to, cur, tmp); } } }; for(auto e : G[cur]) { int to, color; tie(to, color) = e; if(dead[to]) continue; res.emplace_back(); res.back().emplace_back(-1, color); dfs(dfs, to, cur, make_pair(-1, color)); sort(res.back().begin(), res.back().end()); } return res; }; long long int ans = 0, ans_dbl = 0; auto rec = [&](auto &&f, int root) -> void { int c = find_centroid(root); dead[c] = true; for(auto e : G[c]) { int to = e.first; if(dead[to]) continue; f(f, to); } // c を通るものを数える auto vec = get_edges(c); vector< pair > flat; for(auto v : vec) for(auto e : v) flat.emplace_back(e); sort(flat.begin(), flat.end()); for(const auto &v : vec) { for(const auto &e : v) { if(e.first != -1 and e.second != -1) { // それ単体でも答えになりうる ans++; // 完全に一致するもの (2 回数えられる) { auto ub = upper_bound(flat.begin(), flat.end(), e); auto lb = lower_bound(flat.begin(), flat.end(), e); ans_dbl += ub - lb; } { auto ub = upper_bound(v.begin(), v.end(), e); auto lb = lower_bound(v.begin(), v.end(), e); ans_dbl -= ub - lb; } // first だけ { pair one(-1, e.first); { auto ub = upper_bound(flat.begin(), flat.end(), one); auto lb = lower_bound(flat.begin(), flat.end(), one); ans += ub - lb; } { auto ub = upper_bound(v.begin(), v.end(), one); auto lb = lower_bound(v.begin(), v.end(), one); ans -= ub - lb; } } // second だけ { pair one(-1, e.second); { auto ub = upper_bound(flat.begin(), flat.end(), one); auto lb = lower_bound(flat.begin(), flat.end(), one); ans += ub - lb; } { auto ub = upper_bound(v.begin(), v.end(), one); auto lb = lower_bound(v.begin(), v.end(), one); ans -= ub - lb; } } } else { // 一色だけなら、他の一色だけのものと合わせられるかも int c = e.second; { pair p1(-1, 1 << 28); pair p2(-1, -1); auto ub = upper_bound(flat.begin(), flat.end(), p1); auto lb = lower_bound(flat.begin(), flat.end(), p2); ans_dbl += ub - lb; } { pair p1(-1, 1 << 28); pair p2(-1, -1); auto ub = upper_bound(v.begin(), v.end(), p1); auto lb = lower_bound(v.begin(), v.end(), p2); ans_dbl -= ub - lb; } { pair p(-1, c); auto ub = upper_bound(flat.begin(), flat.end(), p); auto lb = lower_bound(flat.begin(), flat.end(), p); ans_dbl -= ub - lb; } { pair p(-1, c); auto ub = upper_bound(v.begin(), v.end(), p); auto lb = lower_bound(v.begin(), v.end(), p); ans_dbl += ub - lb; } } } } dead[c] = false; }; rec(rec, 0); printf("%lld\n", ans + ans_dbl / 2); return 0; }