結果

問題 No.1843 Tree ANDistance
ユーザー 👑 SPD_9X2SPD_9X2
提出日時 2022-02-18 21:32:32
言語 PyPy3
(7.3.13)
結果
AC  
実行時間 769 ms / 2,000 ms
コード長 999 bytes
コンパイル時間 290 ms
コンパイル使用メモリ 87,036 KB
実行使用メモリ 137,432 KB
最終ジャッジ日時 2023-09-11 18:36:53
合計ジャッジ時間 16,766 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 755 ms
137,432 KB
testcase_01 AC 734 ms
137,336 KB
testcase_02 AC 721 ms
137,296 KB
testcase_03 AC 713 ms
137,384 KB
testcase_04 AC 769 ms
137,396 KB
testcase_05 AC 634 ms
137,244 KB
testcase_06 AC 680 ms
137,128 KB
testcase_07 AC 701 ms
134,452 KB
testcase_08 AC 718 ms
135,808 KB
testcase_09 AC 683 ms
134,084 KB
testcase_10 AC 669 ms
133,372 KB
testcase_11 AC 726 ms
136,896 KB
testcase_12 AC 491 ms
132,124 KB
testcase_13 AC 664 ms
132,636 KB
testcase_14 AC 161 ms
78,304 KB
testcase_15 AC 164 ms
78,796 KB
testcase_16 AC 137 ms
78,340 KB
testcase_17 AC 136 ms
78,300 KB
testcase_18 AC 113 ms
77,064 KB
testcase_19 AC 125 ms
78,312 KB
testcase_20 AC 131 ms
78,240 KB
testcase_21 AC 78 ms
71,280 KB
testcase_22 AC 75 ms
71,280 KB
testcase_23 AC 74 ms
71,192 KB
testcase_24 AC 76 ms
71,476 KB
testcase_25 AC 76 ms
71,188 KB
testcase_26 AC 74 ms
71,204 KB
testcase_27 AC 75 ms
70,988 KB
testcase_28 AC 347 ms
124,500 KB
testcase_29 AC 248 ms
110,108 KB
testcase_30 AC 213 ms
109,240 KB
testcase_31 AC 363 ms
129,104 KB
testcase_32 AC 323 ms
125,256 KB
testcase_33 AC 141 ms
92,964 KB
testcase_34 AC 267 ms
115,176 KB
testcase_35 AC 74 ms
71,496 KB
testcase_36 AC 75 ms
71,044 KB
testcase_37 AC 74 ms
70,952 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

"""

A*pow(10,C) // B % 10

"""

import sys
from sys import stdin

def uf_find(n,p):

    ufl = []

    while p[n] != n:
        ufl.append(n)
        n = p[n]

    for i in ufl:
        p[i] = n

    return n


def uf_union(a,b,p,rank):

    ap = uf_find(a,p)
    bp = uf_find(b,p)

    if ap == bp:
        return True
    else:

        if rank[ap] > rank[bp]:
            p[bp] = ap
            rank[ap] += rank[bp]
        else:
            p[ap] = bp
            rank[bp] += rank[ap] 

N = int(stdin.readline())
mod = 10**9+7

ABC = []

for i in range(N-1):

    A,B,C = map(int,stdin.readline().split())
    A -= 1
    B -= 1

    ABC.append( (A,B,C) )

ans = 0
for bit in range(30):

    nb = 2**bit

    p = [i for i in range(N)]
    rank = [1] * N

    for A,B,C in ABC:
        if C & nb > 0:
            uf_union(A,B,p,rank)

    now = 0
    for v in range(N):
        if uf_find(v,p) == v:
            now += rank[v] * (rank[v]-1) // 2

    ans += now * nb
    ans %= mod

print (ans)
0