結果

問題 No.1843 Tree ANDistance
ユーザー chineristACchineristAC
提出日時 2022-02-18 22:20:17
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 708 ms / 2,000 ms
コード長 1,115 bytes
コンパイル時間 1,380 ms
コンパイル使用メモリ 86,408 KB
実行使用メモリ 198,812 KB
最終ジャッジ日時 2023-09-11 19:26:37
合計ジャッジ時間 17,786 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 654 ms
169,552 KB
testcase_01 AC 660 ms
169,876 KB
testcase_02 AC 676 ms
191,536 KB
testcase_03 AC 681 ms
198,812 KB
testcase_04 AC 672 ms
161,176 KB
testcase_05 AC 474 ms
135,704 KB
testcase_06 AC 539 ms
136,592 KB
testcase_07 AC 632 ms
159,096 KB
testcase_08 AC 650 ms
170,028 KB
testcase_09 AC 660 ms
197,304 KB
testcase_10 AC 635 ms
182,312 KB
testcase_11 AC 708 ms
159,656 KB
testcase_12 AC 461 ms
132,316 KB
testcase_13 AC 530 ms
131,020 KB
testcase_14 AC 168 ms
82,412 KB
testcase_15 AC 177 ms
82,816 KB
testcase_16 AC 162 ms
81,824 KB
testcase_17 AC 156 ms
81,752 KB
testcase_18 AC 141 ms
81,484 KB
testcase_19 AC 157 ms
82,588 KB
testcase_20 AC 160 ms
82,068 KB
testcase_21 AC 112 ms
74,344 KB
testcase_22 AC 111 ms
74,268 KB
testcase_23 AC 110 ms
74,188 KB
testcase_24 AC 111 ms
74,404 KB
testcase_25 AC 114 ms
74,256 KB
testcase_26 AC 114 ms
74,332 KB
testcase_27 AC 116 ms
74,400 KB
testcase_28 AC 526 ms
143,384 KB
testcase_29 AC 403 ms
129,280 KB
testcase_30 AC 422 ms
140,568 KB
testcase_31 AC 565 ms
173,840 KB
testcase_32 AC 545 ms
147,428 KB
testcase_33 AC 217 ms
95,652 KB
testcase_34 AC 370 ms
115,512 KB
testcase_35 AC 111 ms
74,436 KB
testcase_36 AC 113 ms
74,132 KB
testcase_37 AC 116 ms
74,356 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys,random,bisect
from collections import deque,defaultdict
from heapq import heapify,heappop,heappush
from itertools import permutations
from math import log,gcd

input = lambda :sys.stdin.readline().rstrip()
mi = lambda :map(int,input().split())
li = lambda :list(mi())

N = int(input())
edge = [[] for v in range(N)]
for _ in range(N-1):
    a,b,c = mi()
    edge[a-1].append((b-1,c))
    edge[b-1].append((a-1,c))

parent = [(-1,-1) for v in range(N)]
topo = []
deq = deque([0])
while deq:
    v = deq.popleft()
    topo.append(v)
    for nv,c in edge[v]:
        if nv==parent[v][0]:
            continue
        parent[nv] = (v,c)
        deq.append(nv)

dp = [[(0,1) for k in range(30)] for v in range(N)]
for v in topo[::-1]:
    for nv,c in edge[v]:
        if nv==parent[v][0]:
            continue

        for k in range(30):
            x,y = dp[v][k]
            z,w = dp[nv][k]
            if c>>k & 1:
                dp[v][k] = (x+z+y*w,y+w)
            else:
                dp[v][k] = (x+z,y)

res = 0
for k in range(30):
    res += dp[0][k][0] * pow(2,k,10**9+7)

print(res % (10**9 + 7))
0