結果

問題 No.1843 Tree ANDistance
ユーザー yudedako
提出日時 2022-02-28 18:11:57
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 977 bytes
コンパイル時間 233 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 165,760 KB
最終ジャッジ日時 2024-07-06 21:24:59
合計ジャッジ時間 43,953 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 27 TLE * 11
権限があれば一括ダウンロードができます

ソースコード

diff #

import queue

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
queue = queue.Queue()
queue.put(0)
while not queue.empty():
    top = queue.get()
    for [to, _] in graph[top]:
        if depth[to] == -1:
            depth[to] = depth[top] + 1
            queue.put(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)
0