結果

問題 No.1843 Tree ANDistance
ユーザー taisii2taisii2
提出日時 2022-02-19 10:53:45
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 548 ms / 2,000 ms
コード長 1,202 bytes
コンパイル時間 1,718 ms
コンパイル使用メモリ 176,920 KB
実行使用メモリ 9,636 KB
最終ジャッジ日時 2023-09-11 20:32:16
合計ジャッジ時間 11,698 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 517 ms
9,052 KB
testcase_01 AC 478 ms
9,048 KB
testcase_02 AC 452 ms
8,516 KB
testcase_03 AC 461 ms
8,596 KB
testcase_04 AC 494 ms
9,636 KB
testcase_05 AC 310 ms
9,496 KB
testcase_06 AC 338 ms
9,588 KB
testcase_07 AC 523 ms
8,720 KB
testcase_08 AC 548 ms
8,808 KB
testcase_09 AC 479 ms
8,400 KB
testcase_10 AC 391 ms
8,108 KB
testcase_11 AC 441 ms
9,632 KB
testcase_12 AC 278 ms
8,848 KB
testcase_13 AC 289 ms
8,972 KB
testcase_14 AC 12 ms
4,380 KB
testcase_15 AC 13 ms
4,376 KB
testcase_16 AC 6 ms
4,376 KB
testcase_17 AC 5 ms
4,380 KB
testcase_18 AC 4 ms
4,376 KB
testcase_19 AC 12 ms
4,376 KB
testcase_20 AC 6 ms
4,376 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 299 ms
7,772 KB
testcase_29 AC 198 ms
6,196 KB
testcase_30 AC 194 ms
5,936 KB
testcase_31 AC 322 ms
7,884 KB
testcase_32 AC 328 ms
8,252 KB
testcase_33 AC 83 ms
4,724 KB
testcase_34 AC 221 ms
7,088 KB
testcase_35 AC 2 ms
4,376 KB
testcase_36 AC 2 ms
4,376 KB
testcase_37 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1E9 + 7;
long long POW(long long a, long long b) {
  long long ans = 1;
  while (b > 0) {
    if (b & 1) {
      ans *= a;
      ans %= MOD;
    }
    a *= a;
    a %= MOD;
    b /= 2;
  }
  return ans;
}

int main() {
  int N;
  cin >> N;
  vector<vector<pair<int, int>>> E(N);
  for (int i = 0; i < N - 1; i++) {
    int A, B, C;
    cin >> A >> B >> C;
    A--;
    B--;
    E[A].push_back({C, B});
    E[B].push_back({C, A});
  }
  long long ans = 0;
  for (int i = 0; i < 32; i++) {
    vector<bool> used(N, false);
    for (int j = 0; j < N; j++) {
      if (!used[j]) {
        long long cnt = 1;
        used[j] = true;
        queue<int> Q;
        Q.push(j);
        while (!Q.empty()) {
          int v = Q.front();
          Q.pop();
          for (auto p : E[v]) {
            int w = p.second;
            int c = p.first;
            if (!used[w]) {
              if (c >> i & 1) {
                cnt++;
                used[w] = true;
                Q.push(w);
              }
            }
          }
        }
        ans += cnt * (cnt - 1) / 2 * POW(2, i) % MOD;
      }
    }
  }
  cout << ans % MOD << endl;
}
0