結果
| 問題 |
No.1843 Tree ANDistance
|
| コンテスト | |
| ユーザー |
tktk_snsn
|
| 提出日時 | 2022-02-18 21:52:25 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 558 ms / 2,000 ms |
| コード長 | 1,294 bytes |
| コンパイル時間 | 159 ms |
| コンパイル使用メモリ | 82,352 KB |
| 実行使用メモリ | 142,516 KB |
| 最終ジャッジ日時 | 2024-06-29 08:46:18 |
| 合計ジャッジ時間 | 11,679 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 38 |
ソースコード
import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)
mod = 10 ** 9 + 7
class UF_tree:
def __init__(self, n):
self.root = [-1] * (n + 1)
self.rank = [0] * (n + 1)
def find(self, x):
stack = []
while self.root[x] >= 0:
stack.append(x)
x = self.root[x]
for i in stack:
self.root[i] = x
return x
def same(self, x, y):
return self.find(x) == self.find(y)
def unite(self, x, y):
x = self.find(x)
y = self.find(y)
if x == y:
return False
if self.rank[x] < self.rank[y]:
self.root[y] += self.root[x]
self.root[x] = y
else:
self.root[x] += self.root[y]
self.root[y] = x
if self.rank[x] == self.rank[y]:
self.rank[x] += 1
return True
def size(self, x):
return -self.root[self.find(x)]
N = int(input())
ABC = tuple(tuple(map(int, input().split())) for _ in range(N-1))
two = 1
ans = 0
for d in range(33):
edge = []
uf = UF_tree(N + 1)
for a, b, c in ABC:
if (c >> d) & 1:
ans += uf.size(a) * uf.size(b) * two
ans %= mod
uf.unite(a, b)
two <<= 1
print(ans)
tktk_snsn