結果

問題 No.1843 Tree ANDistance
ユーザー publfl2publfl2
提出日時 2022-02-18 21:43:03
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 135 ms / 2,000 ms
コード長 982 bytes
コンパイル時間 501 ms
コンパイル使用メモリ 51,760 KB
実行使用メモリ 16,676 KB
最終ジャッジ日時 2023-09-11 18:47:12
合計ジャッジ時間 4,560 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 122 ms
15,672 KB
testcase_01 AC 122 ms
15,924 KB
testcase_02 AC 114 ms
15,608 KB
testcase_03 AC 114 ms
15,580 KB
testcase_04 AC 122 ms
15,392 KB
testcase_05 AC 109 ms
16,120 KB
testcase_06 AC 135 ms
16,676 KB
testcase_07 AC 117 ms
15,160 KB
testcase_08 AC 120 ms
15,100 KB
testcase_09 AC 109 ms
15,580 KB
testcase_10 AC 106 ms
14,952 KB
testcase_11 AC 120 ms
15,392 KB
testcase_12 AC 100 ms
14,836 KB
testcase_13 AC 123 ms
14,776 KB
testcase_14 AC 5 ms
4,380 KB
testcase_15 AC 5 ms
4,376 KB
testcase_16 AC 4 ms
4,380 KB
testcase_17 AC 3 ms
4,376 KB
testcase_18 AC 3 ms
4,380 KB
testcase_19 AC 6 ms
4,380 KB
testcase_20 AC 4 ms
4,384 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 AC 1 ms
4,380 KB
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 46 ms
4,476 KB
testcase_29 AC 31 ms
4,376 KB
testcase_30 AC 30 ms
4,376 KB
testcase_31 AC 49 ms
4,536 KB
testcase_32 AC 47 ms
4,500 KB
testcase_33 AC 16 ms
4,376 KB
testcase_34 AC 38 ms
4,380 KB
testcase_35 AC 2 ms
4,380 KB
testcase_36 AC 1 ms
4,376 KB
testcase_37 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <vector>
#define MOD 1000000007

std::vector< std::pair<int,int> > edge[110];
int next[100010],size[100010];
int find(int k)
{
	if(k==next[k]) return k;
	else return next[k] = find(next[k]);
}

int main()
{
	int a;
	scanf("%d",&a);
	for(int i=1;i<a;i++)
	{
		int b,c,d;
		scanf("%d%d%d",&b,&c,&d);
		for(int j=1;j<=30;j++)
		{
			if(d%2==1) edge[j].push_back(std::make_pair(b,c));
			d/=2;
		}
	}
	
	long long int ans = 0;
	for(int j=1;j<=30;j++)
	{
		for(int i=1;i<=a;i++) next[i] = i;
		for(int i=1;i<=a;i++) size[i] = 1;
		
		for(int i=0;i<edge[j].size();i++)
		{
			int s = edge[j][i].first;
			int t = edge[j][i].second;
			if(find(s)!=find(t))
			{
				size[find(t)] += size[find(s)];
				next[find(s)] = find(t);
			}
		}
		
		for(int i=1;i<=a;i++)
		{
			if(i==find(i))
			{
				long long int val = (long long int)size[i]*(size[i]-1)/2;
				val %= MOD, val *= (1<<(j-1)), val %= MOD;
				ans += val, ans %= MOD;
			}
		}
	}
	printf("%lld",ans);
}
0