1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| #include <bits/stdc++.h> using namespace std; using LL = long long; #define FOR(i, x, y) for (decay<decltype(y)>::type i = (x), _##i = (y); i < _##i; ++i) #define FORD(i, x, y) for (decay<decltype(x)>::type i = (x), _##i = (y); i > _##i; --i) #ifdef zerol #define dbg(args...) do { cout << "\033[32;1m" << #args << " -> "; err(args); } while (0) #else #define dbg(...) #endif void err() { cout << "\033[39;0m" << endl; } template<template<typename...> class T, typename t, typename... Args> void err(T<t> a, Args... args) { for (auto x: a) cout << x << ' '; err(args...); } template<typename T, typename... Args> void err(T a, Args... args) { cout << a << ' '; err(args...); } // ----------------------------------------------------------------------------- const int N = 5E5 + 100; const LL M1 = 921997837, M2 = 988244353; struct E { int to, w; }; vector<E> G[N]; map<pair<LL, LL>, LL> mp;
LL p2(LL x, LL m) { LL ret = 1, t = 2; for (; x; x >>= 1, t = t * t % m) if (x & 1) ret = ret * t % m; return ret; }
void dfs(int u, int fa, LL m1, LL m2) { mp[{m1, m2}]++; for (E& e: G[u]) { int v = e.to; if (v == fa) continue; dfs(v, u, m1 ^ p2(e.w, M1), m2 ^ p2(e.w, M2)); } } inline LL C2(LL x) { return x * (x - 1) / 2; }
int main() { int T; cin >> T; while (T--) { int n; cin >> n; mp.clear(); FOR (i, 1, n + 1) G[i].clear(); FOR (_, 1, n) { int u, v, w; scanf("%d%d%d", &u, &v, &w); G[u].push_back({v, w}); G[v].push_back({u, w}); } dfs(1, 1, 0, 0); LL ans = 0; for (auto& x: mp) ans += C2(x.second); ans = C2(n) - ans; cout << ans << endl; } }
|