結果

問題 No.1843 Tree ANDistance
ユーザー bekzhan29
提出日時 2022-02-21 21:03:40
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 568 ms / 2,000 ms
コード長 1,384 bytes
コンパイル時間 2,135 ms
コンパイル使用メモリ 191,828 KB
最終ジャッジ日時 2025-01-28 01:17:20
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 38
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <algorithm>
#include <vector>
#include <map>
#include <unordered_map>
#include <set>
#include <queue>
#include <stack>
#include <chrono>
#include <random>
using namespace std;
#define pb push_back
#define mp make_pair
#define INF ll(1e18)
#define mod 998244353
#define mod2 1000000007
#define eps 1e-9
#define abs(x) ((x)>=0?(x):-(x))
#define y1 solai
#define fi first
#define se second
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pll;
typedef pair<double,double> pdd;
const ll N=200100;
ll n,a,b,c,p[30][N],r[30][N],ans;
ll fin(ll a, ll t)
{
	if(p[t][a]==a)
		return a;
	return p[t][a]=fin(p[t][a],t);
}
void uni(ll a, ll b, ll t)
{
	a=fin(a,t),b=fin(b,t);
	if(a==b)
		return;
	if(r[t][a]>r[t][b])
		r[t][a]+=r[t][b],p[t][b]=a;
	else
		r[t][b]+=r[t][a],p[t][a]=b;
}
int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	cin>>n;
	for(ll i=1;i<=n;i++)
		for(ll j=0;j<30;j++)
			p[j][i]=i,r[j][i]=1;
	for(ll i=1;i<n;i++)
	{
		cin>>a>>b>>c;
		for(ll j=0;j<30;j++)
			if((c>>j)&1)
				uni(a,b,j);
	}
	for(ll i=1;i<=n;i++)
		for(ll j=0;j<30;j++)
			if(fin(i,j)==i)
				ans+=r[j][i]*(r[j][i]-1)/2%mod2*(1<<j),ans%=mod2;
	cout<<ans;
}
/*
10
4 10 720486811
3 6 815722764
3 4 983953993
8 10 211236348
3 5 349109700
5 9 338069808
6 7 510511646
1 5 367286770
2 6 831398479

*/
0