結果

問題 No.1843 Tree ANDistance
ユーザー brthyyjpbrthyyjp
提出日時 2022-02-19 15:28:00
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 510 ms / 2,000 ms
コード長 1,372 bytes
コンパイル時間 400 ms
コンパイル使用メモリ 87,072 KB
実行使用メモリ 142,600 KB
最終ジャッジ日時 2023-09-11 20:41:12
合計ジャッジ時間 12,901 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 493 ms
133,124 KB
testcase_01 AC 510 ms
132,712 KB
testcase_02 AC 452 ms
140,152 KB
testcase_03 AC 438 ms
140,052 KB
testcase_04 AC 487 ms
122,364 KB
testcase_05 AC 357 ms
136,328 KB
testcase_06 AC 414 ms
142,600 KB
testcase_07 AC 465 ms
140,224 KB
testcase_08 AC 484 ms
125,912 KB
testcase_09 AC 429 ms
136,980 KB
testcase_10 AC 421 ms
131,924 KB
testcase_11 AC 503 ms
128,032 KB
testcase_12 AC 350 ms
137,632 KB
testcase_13 AC 380 ms
138,144 KB
testcase_14 AC 160 ms
79,316 KB
testcase_15 AC 166 ms
79,092 KB
testcase_16 AC 129 ms
77,736 KB
testcase_17 AC 127 ms
77,868 KB
testcase_18 AC 112 ms
77,272 KB
testcase_19 AC 114 ms
78,336 KB
testcase_20 AC 122 ms
78,040 KB
testcase_21 AC 73 ms
71,292 KB
testcase_22 AC 73 ms
71,404 KB
testcase_23 AC 73 ms
71,288 KB
testcase_24 AC 74 ms
71,440 KB
testcase_25 AC 72 ms
71,288 KB
testcase_26 AC 71 ms
71,096 KB
testcase_27 AC 72 ms
71,100 KB
testcase_28 AC 334 ms
128,792 KB
testcase_29 AC 272 ms
111,748 KB
testcase_30 AC 277 ms
111,732 KB
testcase_31 AC 315 ms
133,260 KB
testcase_32 AC 331 ms
128,968 KB
testcase_33 AC 129 ms
94,032 KB
testcase_34 AC 265 ms
118,140 KB
testcase_35 AC 74 ms
71,404 KB
testcase_36 AC 71 ms
71,268 KB
testcase_37 AC 72 ms
71,224 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind:
    def __init__(self, n):
        self.par = [-1]*n
        self.rank = [0]*n

    def Find(self, x):
        if self.par[x] < 0:
            return x
        else:
            self.par[x] = self.Find(self.par[x])
            return self.par[x]

    def Unite(self, x, y):
        x = self.Find(x)
        y = self.Find(y)

        if x != y:
            if self.rank[x] < self.rank[y]:
                self.par[y] += self.par[x]
                self.par[x] = y
            else:
                self.par[x] += self.par[y]
                self.par[y] = x
                if self.rank[x] == self.rank[y]:
                    self.rank[x] += 1

    def Same(self, x, y):
        return self.Find(x) == self.Find(y)

    def Size(self, x):
        return -self.par[self.Find(x)]

mod = 10**9+7
P = [0]*33
P[0] = 1
for i in range(1, 33):
    P[i] = P[i-1]*2
    P[i] %= mod

import sys
import io, os
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline

n = int(input())
E = []
for i in range(n-1):
    a, b, c = map(int, input().split())
    a, b = a-1, b-1
    E.append((a, b, c))

ans = 0
for i in range(32):
    uf = UnionFind(n)
    for a, b, c in E:
        if (c>>i)&1:
            uf.Unite(a, b)
    for j in range(n):
        if uf.par[j] < 0:
            c = uf.Size(j)
            ans += P[i]*(c*(c-1)//2)
            ans %= mod
print(ans)
0