#include using namespace std; using ll = long long; #define rep(i,n) for(int i=0;i inline void print(const T & a) { cout << a << "\n"; } template inline void print(const T & a, const Ts&... ts) { cout << a << " "; print(ts...); } template inline void print(const vector&v) { for (int i = 0; i < v.size(); ++i)cout << v[i] << (i == v.size() - 1 ? "\n" : " "); } pair operator+(const pair& a, const pair& b) { return { a.first + b.first,a.second + b.second }; } pair operator-(const pair& a, const pair& b) { return { a.first - b.first,a.second - b.second }; } pair operator+=(pair& a, const pair& b) { a.first += b.first; a.second += b.second; return a; } template class BinaryIndexedTree { int n; vector dat; public: BinaryIndexedTree(int n) : n(n), dat(n + 1) {}; void add(int i, T a) { for (++i; i <= n; i += i & -i)dat[i] += a; } T sum(int r) {// sum of [0,r) T res = {}; for (; r; r -= r & -r)res += dat[r]; return res; } T sum(int l, int r) {// sum of [l,r) if (l < 0 || n < r || l > r)return {}; return sum(r) - sum(l); } }; int main() { int n; cin >> 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); } int now = 0; vector siz(n, 1), x(n), in(n), out(n); auto dfs0 = [&](auto&& f, int cur, int par)->void { in[cur] = now; ++now; for (auto [to, weight] : g[cur]) { if (to == par)continue; x[to] = x[cur] ^ weight; f(f, to, cur); siz[cur] += siz[to]; } out[cur] = now; }; dfs0(dfs0, 0, -1); auto sorted_x = x; sort(sorted_x.begin(), sorted_x.end()); sorted_x.erase(unique(sorted_x.begin(), sorted_x.end()), sorted_x.end()); rep(i, n)x[i] = lower_bound(sorted_x.begin(), sorted_x.end(), x[i]) - sorted_x.begin();//compress vector> idxs(sorted_x.size()); auto dfs1 = [&](auto&& f, int cur, int par)->void { idxs[x[cur]].push_back(in[cur]); for (auto [to, weight] : g[cur]) { if (to == par)continue; f(f, to, cur); } }; dfs1(dfs1, 0, -1); vector>> y_cnt; y_cnt.reserve(idxs.size()); rep(i, idxs.size()) { sort(idxs[i].begin(), idxs[i].end()); y_cnt.emplace_back(idxs[i].size()); } ll ans = ll(n) * (n - 1); auto dfs2 = [&](auto&& f, int cur, int par)->void { int l = lower_bound(idxs[x[cur]].begin(), idxs[x[cur]].end(), in[cur]) - idxs[x[cur]].begin(); int r = lower_bound(idxs[x[cur]].begin(), idxs[x[cur]].end(), out[cur]) - idxs[x[cur]].begin(); for (auto [to, weight] : g[cur]) { if (to == par)continue; f(f, to, cur); } { auto [y, cnt] = y_cnt[x[cur]].sum(l, r); ans -= y;//case-1 y_cnt[x[cur]].add(l, pair(siz[cur] - y, 1 - cnt)); } if (cur == 0) { rep(i, idxs.size())if (i != x[cur]) { auto [y, cnt] = y_cnt[i].sum(0, idxs[i].size()); ans -= ll(y) * cnt;//case-3 } } else { l = lower_bound(idxs[x[par]].begin(), idxs[x[par]].end(), in[cur]) - idxs[x[par]].begin(); r = lower_bound(idxs[x[par]].begin(), idxs[x[par]].end(), out[cur]) - idxs[x[par]].begin(); auto [y, cnt] = y_cnt[x[par]].sum(l, r); ans -= ll(cnt) * (n - siz[cur]);//case-2 ans -= ll(y) * cnt;//case-3 ans += siz[cur];//case-3 } }; dfs2(dfs2, 0, -1); cout << ans << "\n"; return 0; }