結果

問題 No.1843 Tree ANDistance
ユーザー srjywrdnprktsrjywrdnprkt
提出日時 2023-05-21 23:30:50
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 537 ms / 2,000 ms
コード長 1,208 bytes
コンパイル時間 1,418 ms
コンパイル使用メモリ 116,472 KB
実行使用メモリ 10,172 KB
最終ジャッジ日時 2023-08-23 15:22:17
合計ジャッジ時間 12,784 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 512 ms
10,164 KB
testcase_01 AC 525 ms
10,040 KB
testcase_02 AC 537 ms
10,164 KB
testcase_03 AC 518 ms
10,172 KB
testcase_04 AC 497 ms
10,012 KB
testcase_05 AC 349 ms
9,708 KB
testcase_06 AC 360 ms
9,944 KB
testcase_07 AC 498 ms
9,648 KB
testcase_08 AC 512 ms
9,848 KB
testcase_09 AC 500 ms
9,644 KB
testcase_10 AC 491 ms
9,584 KB
testcase_11 AC 525 ms
9,900 KB
testcase_12 AC 311 ms
9,516 KB
testcase_13 AC 331 ms
9,380 KB
testcase_14 AC 14 ms
4,376 KB
testcase_15 AC 15 ms
4,376 KB
testcase_16 AC 7 ms
4,380 KB
testcase_17 AC 7 ms
4,380 KB
testcase_18 AC 4 ms
4,376 KB
testcase_19 AC 13 ms
4,376 KB
testcase_20 AC 7 ms
4,380 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 1 ms
4,380 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 123 ms
8,604 KB
testcase_29 AC 80 ms
6,684 KB
testcase_30 AC 73 ms
8,528 KB
testcase_31 AC 134 ms
9,056 KB
testcase_32 AC 128 ms
8,580 KB
testcase_33 AC 34 ms
5,268 KB
testcase_34 AC 86 ms
7,672 KB
testcase_35 AC 1 ms
4,376 KB
testcase_36 AC 2 ms
4,380 KB
testcase_37 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cmath>
#include <map>
#include <set>
#include <iomanip>
#include <queue>
#include <algorithm>
#include <numeric>
#include <deque>
#include <complex>
#include <cassert>

using namespace std;
using ll = long long;

const ll modc = 1e9+7;

vector<bool> visit;
ll v;

void dfs(ll from, vector<vector<ll>> &E){
    ll cnt = 1;
    visit[from] = 1;
    v++;

    for (auto to : E[from]){
        if (visit[to]) continue;
        dfs(to, E);
    }
}

int main(){

    ll N, ans=0;
    cin >> N;
    visit.resize(N);
    vector<ll> A(N-1), B(N-1), C(N-1);
    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++){
        vector<vector<ll>> E(N);
        for (int j=0; j<N; j++) visit[j] = 0;
        for (int j=0; j<N-1; j++){
            if (C[j] & (1<<i)){
                E[A[j]].push_back(B[j]);
                E[B[j]].push_back(A[j]);
            }
        }
        for (int j=0; j<N; j++){
            v = 0;
            if (!visit[j]) dfs(j, E);
            ans += ((v * (v-1) / 2) * (1LL << i)) % modc;
            ans %= modc;
        }
    }
    cout << ans << endl;

    return 0;
}
0