結果

問題 No.1843 Tree ANDistance
ユーザー ぷらぷら
提出日時 2022-02-18 21:27:35
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 174 ms / 2,000 ms
コード長 1,521 bytes
コンパイル時間 2,181 ms
コンパイル使用メモリ 205,396 KB
実行使用メモリ 5,116 KB
最終ジャッジ日時 2023-09-11 18:32:15
合計ジャッジ時間 7,173 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 170 ms
5,072 KB
testcase_01 AC 170 ms
5,084 KB
testcase_02 AC 165 ms
5,068 KB
testcase_03 AC 164 ms
4,920 KB
testcase_04 AC 170 ms
5,100 KB
testcase_05 AC 156 ms
5,084 KB
testcase_06 AC 174 ms
4,968 KB
testcase_07 AC 161 ms
5,032 KB
testcase_08 AC 165 ms
5,036 KB
testcase_09 AC 155 ms
5,000 KB
testcase_10 AC 152 ms
4,980 KB
testcase_11 AC 169 ms
5,116 KB
testcase_12 AC 142 ms
4,908 KB
testcase_13 AC 161 ms
4,832 KB
testcase_14 AC 6 ms
4,376 KB
testcase_15 AC 7 ms
4,380 KB
testcase_16 AC 3 ms
4,376 KB
testcase_17 AC 4 ms
4,376 KB
testcase_18 AC 3 ms
4,380 KB
testcase_19 AC 8 ms
4,380 KB
testcase_20 AC 4 ms
4,380 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 AC 1 ms
4,376 KB
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 1 ms
4,376 KB
testcase_28 AC 83 ms
4,580 KB
testcase_29 AC 56 ms
4,376 KB
testcase_30 AC 55 ms
4,376 KB
testcase_31 AC 90 ms
4,948 KB
testcase_32 AC 86 ms
4,756 KB
testcase_33 AC 28 ms
4,376 KB
testcase_34 AC 68 ms
4,632 KB
testcase_35 AC 2 ms
4,380 KB
testcase_36 AC 2 ms
4,380 KB
testcase_37 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

constexpr int mod = 1000000007;

struct UnionFind {
    vector<int> par;
    vector<int> size;
    UnionFind(int n) {
        par.resize(n);
        size.resize(n,1);
        for(int i = 0; i < n; i++) {
            par[i] = i;
        }
    }
    int find(int x) {
        if(par[x] == x) {
            return x;
        }
        return par[x] = find(par[x]);
    }
    bool same(int x, int y) {
        return find(x) == find(y);
    }
    int consize(int x) {
        return size[find(x)];
    }
    bool unite(int x, int y) {
        x = find(x);
        y = find(y);
        if(x == y) {
            return false;
        }
        if(size[x] > size[y]) {
            swap(x,y);
        }
        par[x] = y;
        size[y] += size[x];
        return true;
    }
};

int main() {
    int N;
    cin >> N;
    vector<int>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]--;
    }
    long long ans = 0;
    for(int i = 0; i < 30; i++) {
        UnionFind uf(N);
        for(int j = 0; j < N-1; j++) {
            if(1 & (C[j] >> i)) {
                uf.unite(A[j],B[j]);
            }
        }
        for(int j = 0; j < N; j++) {
            if(uf.find(j) == j) {
                long long c = uf.consize(j);
                c = c*(c-1)/2;
                c %= mod;
                ans += (1ll << i)*c%mod;
                ans %= mod;
            }
        }
    }
    cout << ans << endl;
}
0