結果
| 問題 | 
                            No.1843 Tree ANDistance
                             | 
                    
| コンテスト | |
| ユーザー | 
                             | 
                    
| 提出日時 | 2022-02-28 18:10:50 | 
| 言語 | Python3  (3.13.1 + numpy 2.2.1 + scipy 1.14.1)  | 
                    
| 結果 | 
                             
                                TLE
                                 
                             
                            
                         | 
                    
| 実行時間 | - | 
| コード長 | 977 bytes | 
| コンパイル時間 | 249 ms | 
| コンパイル使用メモリ | 12,928 KB | 
| 実行使用メモリ | 92,176 KB | 
| 最終ジャッジ日時 | 2024-07-06 21:23:23 | 
| 合計ジャッジ時間 | 6,991 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge4 / judge3 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | TLE * 1 -- * 37 | 
ソースコード
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)