結果

問題 No.1843 Tree ANDistance
ユーザー ああいいああいい
提出日時 2022-02-18 22:03:27
言語 PyPy3
(7.3.15)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,030 bytes
コンパイル時間 319 ms
コンパイル使用メモリ 87,112 KB
実行使用メモリ 141,272 KB
最終ジャッジ日時 2023-09-11 19:12:36
合計ジャッジ時間 32,393 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 AC 1,938 ms
103,940 KB
testcase_02 AC 1,956 ms
103,824 KB
testcase_03 AC 1,945 ms
103,492 KB
testcase_04 AC 1,611 ms
102,536 KB
testcase_05 AC 1,106 ms
141,272 KB
testcase_06 AC 1,628 ms
134,740 KB
testcase_07 AC 1,886 ms
102,364 KB
testcase_08 AC 1,700 ms
103,796 KB
testcase_09 AC 1,855 ms
101,820 KB
testcase_10 AC 1,812 ms
101,792 KB
testcase_11 AC 1,896 ms
102,488 KB
testcase_12 AC 991 ms
139,028 KB
testcase_13 AC 1,528 ms
119,288 KB
testcase_14 AC 173 ms
79,136 KB
testcase_15 AC 175 ms
79,284 KB
testcase_16 AC 132 ms
78,532 KB
testcase_17 AC 132 ms
78,348 KB
testcase_18 AC 127 ms
77,892 KB
testcase_19 AC 144 ms
78,836 KB
testcase_20 AC 130 ms
78,796 KB
testcase_21 AC 74 ms
71,364 KB
testcase_22 AC 76 ms
71,396 KB
testcase_23 AC 79 ms
71,208 KB
testcase_24 AC 76 ms
71,360 KB
testcase_25 AC 75 ms
71,276 KB
testcase_26 AC 76 ms
71,372 KB
testcase_27 AC 75 ms
71,300 KB
testcase_28 AC 407 ms
93,544 KB
testcase_29 AC 328 ms
89,004 KB
testcase_30 AC 183 ms
86,168 KB
testcase_31 AC 471 ms
95,100 KB
testcase_32 AC 423 ms
94,092 KB
testcase_33 AC 127 ms
82,400 KB
testcase_34 AC 317 ms
97,980 KB
testcase_35 AC 75 ms
71,248 KB
testcase_36 AC 75 ms
71,360 KB
testcase_37 AC 78 ms
71,204 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
r = sys.stdin
sys.setrecursionlimit(10 ** 8)
N = int(r.readline())
_max = 0
G = [[] for _ in range(N+1)]
for _ in range(N-1):
    a,b,c = map(int,r.readline().split())
    if c > _max:
        _max = c
    G[a].append((b,c))
    G[b].append((a,c))
k = 0
while 1 << k < _max:
    k += 1
ans = 0
P = 10 ** 9 + 7
def find(i):
    if parent[i] < 0:return i
    parent[i] = find(parent[i])
    return parent[i]
def unite(i,j):
    I = find(i)
    J = find(j)
    if I == J:return False
    if parent[I] < parent[J]:
        I,J = J,I
    parent[J] += parent[I]
    parent[I] = J
    return True
for i in range(k+1):
    mask = 1 << i
    parent = [-1] * (N+1)
    stack = [(1,-1)]
    while stack:
        now,p = stack.pop()
        for v,c in G[now]:
            if v == p:continue
            if c & mask:
                unite(now,v)
            stack.append((v,now))
    for i in range(1,N+1):
        if parent[i] < 0:
            num = -parent[i]
            ans = (ans + num * (num-1) //2 % P * mask) % P
print(ans)
0