結果

問題 No.1843 Tree ANDistance
ユーザー mkawa2mkawa2
提出日時 2023-09-26 23:32:36
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 444 ms / 2,000 ms
コード長 1,531 bytes
コンパイル時間 1,647 ms
コンパイル使用メモリ 86,840 KB
実行使用メモリ 157,808 KB
最終ジャッジ日時 2023-09-26 23:32:52
合計ジャッジ時間 15,479 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 443 ms
144,096 KB
testcase_01 AC 437 ms
144,016 KB
testcase_02 AC 435 ms
143,988 KB
testcase_03 AC 440 ms
143,704 KB
testcase_04 AC 430 ms
143,836 KB
testcase_05 AC 378 ms
157,808 KB
testcase_06 AC 418 ms
147,508 KB
testcase_07 AC 409 ms
145,536 KB
testcase_08 AC 435 ms
140,988 KB
testcase_09 AC 423 ms
145,028 KB
testcase_10 AC 421 ms
144,444 KB
testcase_11 AC 444 ms
141,904 KB
testcase_12 AC 351 ms
146,228 KB
testcase_13 AC 376 ms
148,976 KB
testcase_14 AC 118 ms
78,552 KB
testcase_15 AC 118 ms
78,424 KB
testcase_16 AC 103 ms
77,316 KB
testcase_17 AC 103 ms
77,544 KB
testcase_18 AC 91 ms
76,620 KB
testcase_19 AC 115 ms
78,464 KB
testcase_20 AC 110 ms
77,756 KB
testcase_21 AC 76 ms
71,228 KB
testcase_22 AC 76 ms
71,304 KB
testcase_23 AC 75 ms
71,008 KB
testcase_24 AC 75 ms
71,452 KB
testcase_25 AC 75 ms
71,264 KB
testcase_26 AC 76 ms
71,416 KB
testcase_27 AC 76 ms
71,308 KB
testcase_28 AC 341 ms
132,900 KB
testcase_29 AC 263 ms
112,392 KB
testcase_30 AC 238 ms
113,260 KB
testcase_31 AC 347 ms
138,008 KB
testcase_32 AC 314 ms
133,680 KB
testcase_33 AC 151 ms
96,916 KB
testcase_34 AC 276 ms
124,932 KB
testcase_35 AC 75 ms
71,400 KB
testcase_36 AC 75 ms
71,504 KB
testcase_37 AC 77 ms
71,292 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

# sys.setrecursionlimit(200005)
# sys.set_int_max_str_digits(200005)
int1 = lambda x: int(x)-1
pDB = lambda *x: print(*x, end="\n", file=sys.stderr)
p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr)
def II(): return int(sys.stdin.readline())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]
def SI(): return sys.stdin.readline().rstrip()

dij = [(0, 1), (-1, 0), (0, -1), (1, 0)]
# dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
# inf = -1-(-1 << 31)
inf = -1-(-1 << 63)
md = 10**9+7
# md = 998244353

def dfs(root=0):
    uu, stack = [], [root]
    while stack:
        u = stack.pop()
        uu.append(u)
        for v, c in to[u]:
            if v == parent[u]: continue
            cc[v] = c
            parent[v] = u
            depth[v] = depth[u]+1
            stack.append(v)
    return uu

n = II()
to = [[] for _ in range(n)]
for _ in range(n-1):
    u, v, c = LI1()
    to[u].append((v, c+1))
    to[v].append((u, c+1))

parent, depth = [-1]*n, [0]*n
cc = [0]*n
uu = dfs()

ans = 0
for k in range(30):
    dp = [1]*n
    cur = 0
    for u in uu[:0:-1]:
        if cc[u] >> k & 1:
            dp[parent[u]] += dp[u]
        else:
            cur += dp[u]*(dp[u]-1)//2
    cur += dp[0]*(dp[0]-1)//2
    ans += cur*pow(2, k, md)%md
    ans %= md

print(ans)
0