結果

問題 No.1843 Tree ANDistance
ユーザー ripityripity
提出日時 2022-02-18 22:47:53
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,591 bytes
コンパイル時間 2,952 ms
コンパイル使用メモリ 215,784 KB
実行使用メモリ 13,628 KB
最終ジャッジ日時 2023-09-11 19:49:55
合計ジャッジ時間 46,923 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 TLE -
testcase_02 TLE -
testcase_03 TLE -
testcase_04 TLE -
testcase_05 AC 1,821 ms
13,280 KB
testcase_06 TLE -
testcase_07 TLE -
testcase_08 TLE -
testcase_09 TLE -
testcase_10 TLE -
testcase_11 TLE -
testcase_12 AC 1,626 ms
12,280 KB
testcase_13 AC 1,893 ms
12,312 KB
testcase_14 AC 49 ms
4,380 KB
testcase_15 AC 53 ms
4,380 KB
testcase_16 AC 23 ms
4,380 KB
testcase_17 AC 21 ms
4,380 KB
testcase_18 AC 9 ms
4,380 KB
testcase_19 AC 50 ms
4,380 KB
testcase_20 AC 25 ms
4,376 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 2 ms
4,384 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 2 ms
4,384 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 1,054 ms
11,180 KB
testcase_29 AC 650 ms
8,528 KB
testcase_30 AC 604 ms
9,084 KB
testcase_31 AC 1,206 ms
11,972 KB
testcase_32 AC 1,000 ms
11,636 KB
testcase_33 AC 223 ms
6,324 KB
testcase_34 AC 726 ms
9,408 KB
testcase_35 AC 2 ms
4,380 KB
testcase_36 AC 2 ms
4,380 KB
testcase_37 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>
using namespace std;

using ll = long long;

ll dfs(int N, vector<int> &a, vector<int> &b,vector<int> &c, int &d, ll &P) {
    
    ll res = 0;
    set<int> visited;
    deque<int> que;
    vector<vector<int>> edge(N,vector<int>());
    for( int i = 0; i < N-1; i++ ) {
        if( (c[i]>>d)&1 ) {
            edge[a[i]].push_back(b[i]);
            edge[b[i]].push_back(a[i]);
        }
    }
    
    for( int i = 0; i < N; i++ ) {
        if( visited.count(i) ) continue;
        que.push_back(i);
        ll cnt = 0;
        while( !que.empty() ) {
            int now = que.front();
            que.pop_front();
            if( visited.count(now) ) continue;
            visited.insert(now);
            cnt++;
            for( auto& next : edge[now] ) {
                if( !visited.count(next) ) que.push_back(next);
            }
        }
        res += cnt*(cnt-1)/2%P;
        res %= P;
    }
    
    return res;
    
}

void solve() {
    
    int N;
    cin >> N;
    vector<int> a(N-1);
    vector<int> b(N-1);
    vector<int> c(N-1);
    ll ans = 0;
    ll pt = 1;
    ll P = 1000000007;
    
    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++, pt*=2 ) {
        ans += dfs(N,a,b,c,i,P)*pt%P;
        ans %= P;
    }
    
    cout << ans << endl;
    
}

int main() {
    
    int t = 1;
    while( t > 0 ) {
        solve();
        t--;
    }
    
}
0