#include using namespace std; typedef long long ll; typedef priority_queue, greater > PQ; //#define P pair #define FOR(I,A,B) for(ll I = ll(A); I < ll(B); ++I) // int範囲に注意 llより早い #define FORR(I,A,B) for(int I = int((B)-1); I >= int(A); --I) #define TO(x,t,f) ((x)?(t):(f)) #define SORT(x) (sort(x.begin(),x.end())) // 0 2 2 3 4 5 8 9 #define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin()) //xi>=v x is sorted #define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin()) //xi>v x is sorted #define NUM(x,v) (POSU(x,v)-POSL(x,v)) //x is sorted #define REV(x) (reverse(x.begin(),x.end())) //reverse ll gcd_(ll a,ll b){if(a%b==0)return b;return gcd_(b,a%b);} ll lcm_(ll a,ll b){ll c=gcd_(a,b);return ((a/c)*b);} #define NEXTP(x) next_permutation(x.begin(),x.end()) const ll INF=ll(1e16)+ll(7); const ll MOD=1000000007LL; #define out(a) cout< Centroid(const vector< vector > &G){ queue leaf; const int root = 0; const int N = G.size(); vector cnt(N,0),pa(N,-1); FOR(i,0,N){ cnt[i] = G[i].size(); if(G[i].size() == 1 && i != root){ leaf.push(i); } } vector size_sub_tree(N,1); while(leaf.size()){ int u = leaf.front(); leaf.pop(); for(int v:G[u]){ if(cnt[v] >= 2){ size_sub_tree[v] += size_sub_tree[u]; cnt[v]--; if(cnt[v] == 1 && v!=root){ leaf.push(v); pa[u] = v; } } } } vector res; FOR(i,0,N){ bool ok = (N-size_sub_tree[i]) <= N/2; for(auto u:G[i]){ if(u == pa[i]) continue; ok &= size_sub_tree[u] <= N/2; } if(ok) res.push_back(i); } return res; } int main(){ int N; cin >> N; vector> G(N),Gc(N); FOR(i,0,N-1){ int u,v,c; cin >> u >> v >> c; G[u-1].push_back(v-1); G[v-1].push_back(u-1); Gc[u-1].push_back(c); Gc[v-1].push_back(c); } queue< vector> > QG; queue< vector> > QGc; QG.push(G); G.clear(); QGc.push(Gc); Gc.clear(); ll ans = 0; while(QG.size()){ int center = Centroid(QG.front())[0]; auto g = QG.front(); QG.pop(); auto gc = QGc.front(); QGc.pop(); vector cnt(64,0); // ibit目が1のやつの個数 for(int i=0;i number; int k = 1; // g[center][i]を根とする部分木で計算 vector and_(g.size(),-1); and_[ g[center][i] ] = gc[center][i]; and_[ center ] = 0; queue v; v.push(g[center][i]); vector> makeG,makeGc; vector use; while(v.size()){ int from = v.front(); v.pop(); for(int j=0;j<(int)g[from].size();j++){ int to = g[from][j]; if(and_[to] >= 0)continue; and_[to] = and_[from] & gc[from][j]; v.push(to); if(number[from] == 0){ number[from] = k++; use.push_back(from); } if(number[ to ] == 0){ number[ to ] = k++; use.push_back(to); } makeG.resize(k); makeGc.resize(k); makeG[ number[from]-1 ].push_back( number[ to ]-1 ); makeG[ number[ to ]-1 ].push_back( number[from]-1 ); makeGc[ number[from]-1 ].push_back( gc[center][j] ); makeGc[ number[ to ]-1 ].push_back( gc[center][j] ); } } if(k>1)QG.push(makeG); if(k>1)QGc.push(makeGc); for(auto u:use){ for(int j=0;j<33;j++){ if(and_[u] & (1LL<