結果
問題 |
No.1843 Tree ANDistance
|
ユーザー |
![]() |
提出日時 | 2022-02-18 22:52:03 |
言語 | C++17(gcc12) (gcc 12.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 1,154 ms / 2,000 ms |
コード長 | 1,580 bytes |
コンパイル時間 | 11,428 ms |
コンパイル使用メモリ | 271,336 KB |
最終ジャッジ日時 | 2025-01-28 00:31:57 |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 38 |
ソースコード
#pragma GCC target ("avx2") #pragma GCC optimization ("O3") #pragma GCC optimization ("unroll-loops") #include <bits/stdc++.h> using namespace std; using ll = long long; ll dfs(int N, vector<int> &a, vector<int> &b,vector<int> &c, int &d, ll &P) { ll res = 0; deque<int> que; vector<bool> visited(N); vector<vector<int>> edge(N,vector<int>()); for( int i = 0; i < N-1; i++ ) { if( (c[i]>>d)&1 ) { edge[a[i]].push_back(b[i]); edge[b[i]].push_back(a[i]); } } for( int i = 0; i < N; i++ ) { if( visited[i] ) continue; que.push_back(i); ll cnt = 0; while( !que.empty() ) { int now = que.front(); que.pop_front(); if( visited[now] ) continue; visited[now] = true; cnt++; for( auto& next : edge[now] ) { if( !visited[next] ) que.push_back(next); } } res += cnt*(cnt-1)/2%P; res %= P; } return res; } void solve() { int N; cin >> N; vector<int> a(N-1); vector<int> b(N-1); vector<int> c(N-1); ll ans = 0; ll pt = 1; ll P = 1000000007; for( int i = 0; i < N-1; i++ ) { cin >> a[i]>> b[i] >> c[i]; a[i]--; b[i]--; } for( int i = 0; i < 30; i++, pt*=2 ) { ans += dfs(N,a,b,c,i,P)*pt%P; ans %= P; } cout << ans << endl; } int main() { int t = 1; while( t > 0 ) { solve(); t--; } }