結果

問題 No.1843 Tree ANDistance
コンテスト
ユーザー Hentci
提出日時 2022-02-24 14:35:45
言語 C++17
(gcc 15.2.0 + boost 1.90.0)
コンパイル:
g++-15 -O2 -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 215 ms / 2,000 ms
コード長 1,076 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,557 ms
コンパイル使用メモリ 222,152 KB
実行使用メモリ 12,412 KB
最終ジャッジ日時 2026-06-25 08:34:30
合計ジャッジ時間 6,723 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge2_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 38
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <atcoder/dsu.hpp>
#include <bits/stdc++.h>
using namespace std;
using namespace atcoder;
#define ll long long
#define all(x) x.begin(),x.end()
#define ar array
#define sz(x) ((int)x.size())
#define lazycalc S, op, e, F, mapping, composition, id
#define segcalc S, op, e
template <class T,class S> inline bool chmin(T &a,const S &b) {return (a> b? a= b, 1: 0);}
template <class T,class S> inline bool chmax(T &a,const S &b) {return (a< b? a= b, 1: 0);}
const int mod = 1e9 + 7;
int main(){
    ios_base::sync_with_stdio(false),cin.tie(0);
    int n;
    cin >> n;
    vector <ar<ll, 3>> G(n - 1);
    for(auto &[a, b, c]: G){
        cin >> a >> b >> c;
        --a, --b;
    }
    
    ll ans = 0;
    for(int i = 0;i < 32;i++){
        dsu d(n);
        for(auto [a, b, c]: G){
            if(c >> i & 1)
                d.merge(a, b);
        }
        for(auto ele: d.groups()){
            ll SZ = ele.size() % mod;
            SZ = SZ * (SZ - 1) / 2 % mod;
            ans = (ans + SZ * (1 << i)) % mod;
        }
    }

    cout << ans << "\n";

    return 0;
}
0