結果

問題 No.1843 Tree ANDistance
ユーザー shobonvipshobonvip
提出日時 2022-02-19 00:19:06
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 581 ms / 2,000 ms
コード長 781 bytes
コンパイル時間 296 ms
コンパイル使用メモリ 87,016 KB
実行使用メモリ 104,096 KB
最終ジャッジ日時 2023-09-11 20:20:03
合計ジャッジ時間 12,929 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 544 ms
103,832 KB
testcase_01 AC 557 ms
103,568 KB
testcase_02 AC 520 ms
102,952 KB
testcase_03 AC 516 ms
102,996 KB
testcase_04 AC 581 ms
104,096 KB
testcase_05 AC 425 ms
102,980 KB
testcase_06 AC 443 ms
103,232 KB
testcase_07 AC 543 ms
102,156 KB
testcase_08 AC 524 ms
102,632 KB
testcase_09 AC 487 ms
102,136 KB
testcase_10 AC 477 ms
101,304 KB
testcase_11 AC 549 ms
103,440 KB
testcase_12 AC 377 ms
100,520 KB
testcase_13 AC 390 ms
101,560 KB
testcase_14 AC 142 ms
78,604 KB
testcase_15 AC 142 ms
78,636 KB
testcase_16 AC 123 ms
77,988 KB
testcase_17 AC 120 ms
78,568 KB
testcase_18 AC 120 ms
77,980 KB
testcase_19 AC 116 ms
78,596 KB
testcase_20 AC 118 ms
78,720 KB
testcase_21 AC 70 ms
71,456 KB
testcase_22 AC 73 ms
71,268 KB
testcase_23 AC 72 ms
71,272 KB
testcase_24 AC 72 ms
70,956 KB
testcase_25 AC 72 ms
71,148 KB
testcase_26 AC 75 ms
71,192 KB
testcase_27 AC 72 ms
71,148 KB
testcase_28 AC 254 ms
97,408 KB
testcase_29 AC 214 ms
91,908 KB
testcase_30 AC 223 ms
92,116 KB
testcase_31 AC 253 ms
99,248 KB
testcase_32 AC 251 ms
97,628 KB
testcase_33 AC 133 ms
84,176 KB
testcase_34 AC 200 ms
93,836 KB
testcase_35 AC 74 ms
71,428 KB
testcase_36 AC 72 ms
71,464 KB
testcase_37 AC 73 ms
71,280 KB
権限があれば一括ダウンロードができます

ソースコード

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