結果

問題 No.1843 Tree ANDistance
ユーザー shobonvip
提出日時 2022-02-19 00:19:06
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 546 ms / 2,000 ms
コード長 781 bytes
コンパイル時間 215 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 102,144 KB
最終ジャッジ日時 2024-06-29 10:04:40
合計ジャッジ時間 11,402 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 38
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind:
	def __init__(self, n):
		self.n = n
		self.parents = [-1] * n
	
	def find(self, x):
		if self.parents[x] < 0:
			return x
		else:
			self.parents[x] = self.find(self.parents[x])
			return self.parents[x]
	
	def union(self, x, y):
		x = self.find(x)
		y = self.find(y)
		if x == y:
			return
		if self.parents[x] > self.parents[y]:
			x, y = y, x
		self.parents[x] += self.parents[y]
		self.parents[y] = x

n = int(input())
ans = 0
union = [UnionFind(n) for _ in range(32)]
mod = 10**9 + 7

for i in range(n-1):
	a, b, c = map(int,input().split())
	for j in range(32):
		if (c >> j) & 1 == 1:
			union[j].union(a-1, b-1)

for i in range(n):
	for j in range(32):
		t = -union[j].parents[i]
		if t > 0:
			ans = (ans + t * (t - 1) // 2 * (1 << j)) % mod

print(ans)
0