#include using namespace std; using ll = long long; #define rep(i,n) for(int i=0;i> n; vector>> g(n); rep(i, n - 1) { int a, b, c; cin >> a >> b >> c; --a, --b; g[a].emplace_back(b, c); g[b].emplace_back(a, c); } vector siz(n, 1); ll ans = ll(n) * (n - 1); auto dfs = [&](auto&& f, int cur, int par, int XOR)->map> { map> L;//{y,cnt} for (auto [to, weight] : g[cur]) { if (to == par)continue; auto R = f(f, to, cur, XOR ^ weight); if (R.count(XOR)) { ans -= ll(R[XOR].second) * (n - siz[to]);//case-2 } if (L.size() > R.size())swap(L, R); for (auto [l_XOR, val] : L) { auto [y, cnt] = val; if (l_XOR != XOR && R.count(l_XOR)) { ans -= ll(cnt) * R[l_XOR].first;//case-3-1 ans -= ll(R[l_XOR].second) * y;//case-3-2 } R[l_XOR].first += y; R[l_XOR].second += cnt; } swap(L, R); siz[cur] += siz[to]; } if (L.count(XOR)) { ans -= L[XOR].first;//case-1 } L[XOR] = { siz[cur],1 }; return move(L); }; dfs(dfs, 0, -1, 0); cout << ans << "\n"; return 0; }