結果

問題 No.1843 Tree ANDistance
ユーザー ytftytft
提出日時 2022-02-21 00:10:22
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 258 ms / 2,000 ms
コード長 1,781 bytes
コンパイル時間 1,573 ms
コンパイル使用メモリ 170,944 KB
実行使用メモリ 5,208 KB
最終ジャッジ日時 2023-09-11 21:12:50
合計ジャッジ時間 7,657 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 248 ms
4,920 KB
testcase_01 AC 249 ms
5,004 KB
testcase_02 AC 236 ms
5,088 KB
testcase_03 AC 234 ms
4,928 KB
testcase_04 AC 250 ms
4,916 KB
testcase_05 AC 237 ms
4,968 KB
testcase_06 AC 258 ms
4,996 KB
testcase_07 AC 234 ms
5,004 KB
testcase_08 AC 240 ms
4,888 KB
testcase_09 AC 221 ms
4,856 KB
testcase_10 AC 217 ms
4,976 KB
testcase_11 AC 246 ms
5,208 KB
testcase_12 AC 227 ms
4,888 KB
testcase_13 AC 243 ms
4,856 KB
testcase_14 AC 9 ms
4,376 KB
testcase_15 AC 10 ms
4,380 KB
testcase_16 AC 5 ms
4,376 KB
testcase_17 AC 4 ms
4,380 KB
testcase_18 AC 3 ms
4,380 KB
testcase_19 AC 11 ms
4,380 KB
testcase_20 AC 6 ms
4,380 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 2 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,380 KB
testcase_28 AC 92 ms
4,628 KB
testcase_29 AC 61 ms
4,380 KB
testcase_30 AC 62 ms
4,380 KB
testcase_31 AC 102 ms
4,824 KB
testcase_32 AC 96 ms
4,656 KB
testcase_33 AC 27 ms
4,380 KB
testcase_34 AC 76 ms
4,380 KB
testcase_35 AC 2 ms
4,376 KB
testcase_36 AC 1 ms
4,380 KB
testcase_37 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
struct UnionFind{
    vector<int> parent;
    vector<int> numberOfElements;//根ノードのみ有効
    UnionFind(int n){
        parent.resize(n);
        numberOfElements.resize(n);
        for(int i=0;i<n;++i){
            parent[i]=-1;
            numberOfElements[i]=1;
        }
    }
    int find(int a){
        vector<int> path(0);
        int pointer=a;
        while(parent[pointer]!=-1){
            path.push_back(pointer);
            pointer=parent[pointer];
        }
        for(int i:path){
            parent[i]=pointer;
        }
        return pointer;
    }
    void unite(int a,int b){
        int p=find(a);
        int q=find(b);
        if(p==q){
            return;
        }
        if(numberOfElements[p]<numberOfElements[q]){
            parent[p]=q;
            numberOfElements[q]+=numberOfElements[p];
        }else{
            parent[q]=p;
            numberOfElements[p]+=numberOfElements[q];
        }
    }
};

int main(){
    int N,mod=pow(10,9)+7;
    cin>>N;
    int edges[N-1][3];
    long long cur=1,ans=0;
    for(int i=0;i<N-1;++i){
        for(int j=0;j<3;++j){
            cin>>edges[i][j];
            edges[i][j]-=(j<2);
        }
    }
    for(int i=0;i<32;++i){
        long long temp=0;
        UnionFind U(N);
        for(int j=0;j<N-1;++j){
            if(edges[j][2]%2){
                U.unite(edges[j][0],edges[j][1]);
            }
        }
        for(int j=0;j<N-1;++j){
            if(U.find(j)==j){
            	long long n=U.numberOfElements[j];
                temp=(temp+n*(n-1)/2)%mod;
            }
        }
        for(int j=0;j<N-1;++j){
            edges[j][2]>>=1;
        }
        ans=(ans+temp*cur)%mod;
        cur=(cur*2)%mod;
    }
    cout<<ans<<endl;
}
0