結果

問題 No.1843 Tree ANDistance
ユーザー titiatitia
提出日時 2022-02-18 22:08:27
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,123 ms / 2,000 ms
コード長 963 bytes
コンパイル時間 320 ms
コンパイル使用メモリ 87,052 KB
実行使用メモリ 140,972 KB
最終ジャッジ日時 2023-09-11 19:16:33
合計ジャッジ時間 20,589 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 964 ms
140,888 KB
testcase_01 AC 944 ms
140,972 KB
testcase_02 AC 886 ms
140,828 KB
testcase_03 AC 892 ms
140,964 KB
testcase_04 AC 932 ms
140,616 KB
testcase_05 AC 1,060 ms
139,380 KB
testcase_06 AC 1,004 ms
140,312 KB
testcase_07 AC 910 ms
136,908 KB
testcase_08 AC 896 ms
138,436 KB
testcase_09 AC 805 ms
137,108 KB
testcase_10 AC 800 ms
136,356 KB
testcase_11 AC 930 ms
140,144 KB
testcase_12 AC 1,123 ms
134,248 KB
testcase_13 AC 955 ms
134,872 KB
testcase_14 AC 188 ms
79,792 KB
testcase_15 AC 198 ms
79,308 KB
testcase_16 AC 155 ms
78,304 KB
testcase_17 AC 152 ms
78,948 KB
testcase_18 AC 135 ms
77,936 KB
testcase_19 AC 177 ms
79,240 KB
testcase_20 AC 158 ms
78,492 KB
testcase_21 AC 80 ms
71,456 KB
testcase_22 AC 77 ms
71,328 KB
testcase_23 AC 76 ms
71,284 KB
testcase_24 AC 78 ms
71,144 KB
testcase_25 AC 76 ms
71,440 KB
testcase_26 AC 76 ms
71,060 KB
testcase_27 AC 75 ms
71,372 KB
testcase_28 AC 412 ms
126,620 KB
testcase_29 AC 303 ms
112,332 KB
testcase_30 AC 263 ms
110,748 KB
testcase_31 AC 360 ms
130,884 KB
testcase_32 AC 394 ms
127,412 KB
testcase_33 AC 155 ms
94,020 KB
testcase_34 AC 313 ms
116,708 KB
testcase_35 AC 75 ms
71,416 KB
testcase_36 AC 75 ms
71,436 KB
testcase_37 AC 78 ms
75,280 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

mod=10**9+7
n=int(input())

# UnionFind

def find(x):
    while Group[x] != x:
        x=Group[x]
    return x

def Union(x,y):
    if find(x) != find(y):
        if Nodes[find(x)] < Nodes[find(y)]:
            
            Nodes[find(y)] += Nodes[find(x)]
            Nodes[find(x)] = 0
            Group[find(x)] = find(y)
            
        else:
            Nodes[find(x)] += Nodes[find(y)]
            Nodes[find(y)] = 0
            Group[find(y)] = find(x)

E=[tuple(map(int,input().split())) for i in range(n-1)]

LANS=0

for i in range(32):
    Group = [i for i in range(n+1)] # グループ分け
    Nodes = [1]*(n+1) # 各グループのノードの数

    for x,y,c in E:
        x-=1
        y-=1

        if c & (1<<i) != 0:
            Union(x,y)

    ANS=0

    for j in range(n):
        if find(j)==j:
            x=Nodes[j]

            ANS+=x*(x-1)//2

    LANS+=ANS*(1<<i)

print(LANS%mod)

        
0