結果

問題 No.1843 Tree ANDistance
ユーザー 👑 potato167potato167
提出日時 2021-12-09 01:11:44
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 161 ms / 2,000 ms
コード長 1,527 bytes
コンパイル時間 2,220 ms
コンパイル使用メモリ 201,648 KB
実行使用メモリ 5,172 KB
最終ジャッジ日時 2023-09-23 12:26:54
合計ジャッジ時間 9,459 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 158 ms
5,016 KB
testcase_01 AC 156 ms
4,844 KB
testcase_02 AC 149 ms
4,896 KB
testcase_03 AC 151 ms
4,956 KB
testcase_04 AC 153 ms
4,944 KB
testcase_05 AC 136 ms
4,956 KB
testcase_06 AC 161 ms
4,956 KB
testcase_07 AC 147 ms
5,008 KB
testcase_08 AC 152 ms
5,016 KB
testcase_09 AC 142 ms
4,956 KB
testcase_10 AC 139 ms
5,024 KB
testcase_11 AC 155 ms
4,944 KB
testcase_12 AC 120 ms
4,904 KB
testcase_13 AC 150 ms
5,172 KB
testcase_14 AC 6 ms
4,380 KB
testcase_15 AC 6 ms
4,376 KB
testcase_16 AC 4 ms
4,380 KB
testcase_17 AC 4 ms
4,376 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 7 ms
4,376 KB
testcase_20 AC 4 ms
4,376 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 1 ms
4,376 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 65 ms
4,632 KB
testcase_29 AC 44 ms
4,376 KB
testcase_30 AC 44 ms
4,404 KB
testcase_31 AC 70 ms
4,676 KB
testcase_32 AC 67 ms
4,756 KB
testcase_33 AC 21 ms
4,376 KB
testcase_34 AC 53 ms
4,380 KB
testcase_35 AC 1 ms
4,380 KB
testcase_36 AC 1 ms
4,376 KB
testcase_37 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using std::cout;
using std::cin;
using std::endl;
using ll=long long;
const ll mod=1e9+7;
#define rep(i,a) for (int i=0;i<a;i++)


namespace po167{
struct UFtree
{
	std::vector<int> wei;
	int component;
	UFtree(int n):par(n),wei(n),component(n){
		for(int i=0;i<n;i++){
			wei[i]=1,par[i]=i;
		}
	}
	void intialize(){
		for(int i=0;i<(int) par.size();i++){
			wei[i]=1,par[i]=i;
		}
		component=(int)par.size();
	}
	//根っこを返す
	int root(int a){
		if(a==par[a]) return a;
		return par[a]=root(par[a]);
	}
	//trueなら1,falseなら0
	int same(int a,int b){
		if(root(a)==root(b)) return 1;
		else return 0;
	}
	//a,bが違う根っこの元なら結合する,結合したらtrueを返す
	bool unite(int a,int b){
		a=root(a),b=root(b);
		if(a==b) return false;
		if(wei[a]<wei[b]) std::swap(a,b);
		par[b]=a;
		wei[a]+=wei[b];
		component--;
		return true;
	}
	private:
	std::vector<int> par;
};
}
using po167::UFtree;

int main() {
	
	int N;
    cin>>N;
    vector<int> A(N-1),B(N-1),C(N-1);
    rep(i,N-1){
        cin>>A[i]>>B[i]>>C[i];
        A[i]--,B[i]--;
    }
    ll ans=0;
    UFtree T(N);
    rep(i,32){
        ll tmp=0;
        T.intialize();
        rep(j,N-1){
            if(C[j]&(1<<i)) T.unite(A[j],B[j]);
        }
        rep(i,N){
            if(T.root(i)==i){
                tmp+=((ll)(T.wei[i])*(ll)(T.wei[i]-1))/2;
            }
        }
        tmp%=mod;
        tmp*=(1ll<<i);
        ans+=tmp%mod;
    }
    cout<<(ans%mod+mod)%mod<<"\n";
}

0