結果

問題 No.1843 Tree ANDistance
ユーザー bekzhan29bekzhan29
提出日時 2022-02-21 21:03:40
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 466 ms / 2,000 ms
コード長 1,384 bytes
コンパイル時間 2,184 ms
コンパイル使用メモリ 198,956 KB
実行使用メモリ 86,348 KB
最終ジャッジ日時 2023-09-12 12:33:15
合計ジャッジ時間 14,003 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 455 ms
86,308 KB
testcase_01 AC 456 ms
86,080 KB
testcase_02 AC 457 ms
86,348 KB
testcase_03 AC 456 ms
86,348 KB
testcase_04 AC 465 ms
86,088 KB
testcase_05 AC 407 ms
86,156 KB
testcase_06 AC 404 ms
86,028 KB
testcase_07 AC 452 ms
85,752 KB
testcase_08 AC 453 ms
86,004 KB
testcase_09 AC 464 ms
85,660 KB
testcase_10 AC 452 ms
85,324 KB
testcase_11 AC 466 ms
85,964 KB
testcase_12 AC 367 ms
85,260 KB
testcase_13 AC 380 ms
85,440 KB
testcase_14 AC 20 ms
75,432 KB
testcase_15 AC 21 ms
75,496 KB
testcase_16 AC 17 ms
75,212 KB
testcase_17 AC 17 ms
75,388 KB
testcase_18 AC 16 ms
75,112 KB
testcase_19 AC 21 ms
75,508 KB
testcase_20 AC 17 ms
75,300 KB
testcase_21 AC 16 ms
75,076 KB
testcase_22 AC 15 ms
75,164 KB
testcase_23 AC 16 ms
75,236 KB
testcase_24 AC 16 ms
75,092 KB
testcase_25 AC 15 ms
75,104 KB
testcase_26 AC 16 ms
75,096 KB
testcase_27 AC 16 ms
75,092 KB
testcase_28 AC 104 ms
83,580 KB
testcase_29 AC 75 ms
80,796 KB
testcase_30 AC 69 ms
80,884 KB
testcase_31 AC 111 ms
84,360 KB
testcase_32 AC 104 ms
83,908 KB
testcase_33 AC 41 ms
78,024 KB
testcase_34 AC 83 ms
82,000 KB
testcase_35 AC 15 ms
75,104 KB
testcase_36 AC 15 ms
75,048 KB
testcase_37 AC 15 ms
75,108 KB
権限があれば一括ダウンロードができます

ソースコード

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