結果

問題 No.1843 Tree ANDistance
ユーザー mkawa2mkawa2
提出日時 2023-02-16 10:48:11
言語 PyPy3
(7.3.15)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,492 bytes
コンパイル時間 988 ms
コンパイル使用メモリ 87,000 KB
実行使用メモリ 144,564 KB
最終ジャッジ日時 2023-09-25 16:01:22
合計ジャッジ時間 38,986 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 TLE -
testcase_02 AC 1,963 ms
139,840 KB
testcase_03 TLE -
testcase_04 TLE -
testcase_05 AC 998 ms
144,564 KB
testcase_06 AC 1,642 ms
144,400 KB
testcase_07 TLE -
testcase_08 TLE -
testcase_09 AC 1,951 ms
138,340 KB
testcase_10 AC 1,770 ms
137,732 KB
testcase_11 AC 1,892 ms
140,580 KB
testcase_12 AC 915 ms
137,780 KB
testcase_13 AC 1,460 ms
136,028 KB
testcase_14 AC 134 ms
78,844 KB
testcase_15 AC 138 ms
79,200 KB
testcase_16 AC 116 ms
78,068 KB
testcase_17 AC 116 ms
78,092 KB
testcase_18 AC 91 ms
76,752 KB
testcase_19 AC 130 ms
79,108 KB
testcase_20 AC 120 ms
78,264 KB
testcase_21 AC 74 ms
71,408 KB
testcase_22 AC 76 ms
71,344 KB
testcase_23 AC 74 ms
71,388 KB
testcase_24 AC 74 ms
71,300 KB
testcase_25 AC 74 ms
71,440 KB
testcase_26 AC 75 ms
71,436 KB
testcase_27 AC 73 ms
71,432 KB
testcase_28 AC 1,213 ms
122,124 KB
testcase_29 AC 812 ms
110,684 KB
testcase_30 AC 818 ms
111,096 KB
testcase_31 AC 1,424 ms
130,204 KB
testcase_32 AC 1,171 ms
122,616 KB
testcase_33 AC 247 ms
96,784 KB
testcase_34 AC 659 ms
119,412 KB
testcase_35 AC 73 ms
71,208 KB
testcase_36 AC 74 ms
71,256 KB
testcase_37 AC 73 ms
71,424 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

# sys.setrecursionlimit(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 << 63)-1
# inf = (1 << 31)-1
md = 10**9+7
# md = 998244353

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

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

uu = dfs()

ans = 0
for i in range(30)[::-1]:
    cnt = [1]*n
    s = 0
    for u in uu[:0:-1]:
        p = parent[u]
        if cc[p, u] >> i & 1:
            s += cnt[p]*cnt[u]%md
            s %= md
            cnt[p] += cnt[u]
    ans += pow(2, i, md)*s%md
    ans %= md

print(ans)
0