結果

問題 No.1843 Tree ANDistance
ユーザー yudedakoyudedako
提出日時 2022-02-28 18:49:00
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 983 bytes
コンパイル時間 302 ms
コンパイル使用メモリ 10,900 KB
実行使用メモリ 87,308 KB
最終ジャッジ日時 2023-09-21 03:20:24
合計ジャッジ時間 7,252 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

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)
0