結果
| 問題 |
No.1843 Tree ANDistance
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2022-02-28 18:49:00 |
| 言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 983 bytes |
| コンパイル時間 | 314 ms |
| コンパイル使用メモリ | 12,544 KB |
| 実行使用メモリ | 88,664 KB |
| 最終ジャッジ日時 | 2024-07-06 22:00:03 |
| 合計ジャッジ時間 | 7,174 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | TLE * 1 -- * 37 |
ソースコード
import collections
MOD = 1000000007
n = int(input())
edges = [map(int, input().split()) for _ in range(n - 1)]
graph = [[] for _ in range(n)]
for [a, b, c] in edges:
graph[a - 1].append([b - 1, c])
graph[b - 1].append([a - 1, c])
depth = [-1] * n
depth[0] = 0
stack = collections.deque()
stack.append(0)
while stack:
top = stack.pop()
for [to, _] in graph[top]:
if depth[to] == -1:
depth[to] = depth[top] + 1
stack.append(to)
fromLeaf = sorted([i for i in range(n)], key = lambda i: depth[i], reverse = True)
result = 0
for d in range(30):
onePath = 0
endPath = [0] * n
for node in fromLeaf:
count = 0
for [child, c] in graph[node]:
if depth[child] <= depth[node] or ((c >> d) & 1) == 0:
continue
onePath += (count + 1) * (endPath[child] + 1)
count += endPath[child] + 1
endPath[node] = count
result += onePath % MOD << d
print(result % MOD)