結果

問題 No.1843 Tree ANDistance
ユーザー Hentci
提出日時 2022-02-24 14:35:45
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 420 ms / 2,000 ms
コード長 1,076 bytes
コンパイル時間 2,606 ms
コンパイル使用メモリ 206,692 KB
最終ジャッジ日時 2025-01-28 01:34:05
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 38
権限があれば一括ダウンロードができます

ソースコード

diff #

#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