結果

問題 No.1843 Tree ANDistance
ユーザー SSRSSSRS
提出日時 2022-02-18 21:24:35
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 423 ms / 2,000 ms
コード長 1,015 bytes
コンパイル時間 2,081 ms
コンパイル使用メモリ 208,084 KB
実行使用メモリ 9,604 KB
最終ジャッジ日時 2023-09-11 18:29:56
合計ジャッジ時間 10,369 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 423 ms
8,968 KB
testcase_01 AC 387 ms
9,132 KB
testcase_02 AC 400 ms
8,592 KB
testcase_03 AC 369 ms
8,440 KB
testcase_04 AC 372 ms
9,604 KB
testcase_05 AC 261 ms
9,488 KB
testcase_06 AC 273 ms
9,588 KB
testcase_07 AC 388 ms
8,896 KB
testcase_08 AC 386 ms
8,892 KB
testcase_09 AC 374 ms
8,420 KB
testcase_10 AC 355 ms
7,984 KB
testcase_11 AC 403 ms
9,560 KB
testcase_12 AC 238 ms
8,832 KB
testcase_13 AC 244 ms
8,960 KB
testcase_14 AC 10 ms
4,376 KB
testcase_15 AC 11 ms
4,380 KB
testcase_16 AC 5 ms
4,376 KB
testcase_17 AC 4 ms
4,376 KB
testcase_18 AC 3 ms
4,376 KB
testcase_19 AC 11 ms
4,380 KB
testcase_20 AC 6 ms
4,376 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 229 ms
7,752 KB
testcase_29 AC 145 ms
6,188 KB
testcase_30 AC 135 ms
5,908 KB
testcase_31 AC 231 ms
7,736 KB
testcase_32 AC 247 ms
8,408 KB
testcase_33 AC 64 ms
4,728 KB
testcase_34 AC 159 ms
7,088 KB
testcase_35 AC 2 ms
4,380 KB
testcase_36 AC 2 ms
4,376 KB
testcase_37 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
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(make_pair(C, B));
    E[B].push_back(make_pair(C, A));
  }
  long long ans = 0;
  for (int i = 0; i < 30; i++){
    vector<bool> used(N, false);
    for (int j = 0; j < N; j++){
      if (!used[j]){
        used[j] = true;
        int cnt = 0;
        queue<int> Q;
        Q.push(j);
        while (!Q.empty()){
          int v = Q.front();
          Q.pop();
          cnt++;
          for (auto P : E[v]){
            if ((P.first >> i & 1) == 1){
              int w = P.second;
              if (!used[w]){
                used[w] = true;
                Q.push(w);
              }
            }
          }
        }
        ans += (long long) cnt * (cnt - 1) / 2 % MOD * (1 << i) % MOD;
      }
    }
  }
  ans %= MOD;
  cout << ans << endl;
}
0