結果
問題 | No.1843 Tree ANDistance |
ユーザー |
|
提出日時 | 2022-02-18 22:06:29 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 1,317 ms / 2,000 ms |
コード長 | 976 bytes |
コンパイル時間 | 4,516 ms |
コンパイル使用メモリ | 259,800 KB |
最終ジャッジ日時 | 2025-01-28 00:06:54 |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 38 |
ソースコード
#include <bits/stdc++.h>#include <atcoder/all>using namespace std;using namespace atcoder;using ll = long long;#define all(x) x.begin(), x.end()#define rep(i, n) for (int i = 0; i < n; i++)#define mint modint1000000007int main(void){int n;cin >> n;mint ans, bi2;bi2 = 1;vector<vector<int>> abc(n, vector<int>(3));rep(i, n - 1)rep(j, 3){cin >> abc[i][j];}rep(bit, 32){dsu U(n + 1);rep(i, n - 1){int a = abc[i][0];int b = abc[i][1];int c = abc[i][2];if(1 & (c >> bit)) U.merge(a - 1, b - 1);}map<int, int> s;rep(i, n){int x = U.leader(i);if (s.find(x) != s.end()) continue;else{s[x] = 1;mint c = U.size(i);ans += bi2 * c * (c - 1) / 2;}}bi2 *= 2;}cout << ans.val() << endl;return 0;}