結果

問題 No.1843 Tree ANDistance
ユーザー 👑 rin204rin204
提出日時 2022-05-04 18:33:31
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 535 ms / 2,000 ms
コード長 1,625 bytes
コンパイル時間 273 ms
コンパイル使用メモリ 86,792 KB
実行使用メモリ 176,504 KB
最終ジャッジ日時 2023-09-16 22:46:19
合計ジャッジ時間 13,539 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 530 ms
118,272 KB
testcase_01 AC 526 ms
119,084 KB
testcase_02 AC 480 ms
121,804 KB
testcase_03 AC 476 ms
121,112 KB
testcase_04 AC 516 ms
119,532 KB
testcase_05 AC 440 ms
121,652 KB
testcase_06 AC 470 ms
122,964 KB
testcase_07 AC 495 ms
120,640 KB
testcase_08 AC 512 ms
120,148 KB
testcase_09 AC 475 ms
122,128 KB
testcase_10 AC 461 ms
122,000 KB
testcase_11 AC 535 ms
118,472 KB
testcase_12 AC 394 ms
120,240 KB
testcase_13 AC 473 ms
119,740 KB
testcase_14 AC 173 ms
79,044 KB
testcase_15 AC 176 ms
78,900 KB
testcase_16 AC 140 ms
77,924 KB
testcase_17 AC 141 ms
77,984 KB
testcase_18 AC 119 ms
77,972 KB
testcase_19 AC 140 ms
78,744 KB
testcase_20 AC 136 ms
78,032 KB
testcase_21 AC 75 ms
71,212 KB
testcase_22 AC 76 ms
71,076 KB
testcase_23 AC 76 ms
71,160 KB
testcase_24 AC 76 ms
71,220 KB
testcase_25 AC 77 ms
71,412 KB
testcase_26 AC 76 ms
71,160 KB
testcase_27 AC 77 ms
71,316 KB
testcase_28 AC 423 ms
128,364 KB
testcase_29 AC 340 ms
114,676 KB
testcase_30 AC 345 ms
116,348 KB
testcase_31 AC 426 ms
176,504 KB
testcase_32 AC 426 ms
162,112 KB
testcase_33 AC 176 ms
94,092 KB
testcase_34 AC 328 ms
119,632 KB
testcase_35 AC 76 ms
71,096 KB
testcase_36 AC 74 ms
71,216 KB
testcase_37 AC 74 ms
71,396 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

MOD = 10 ** 9 + 7

class UnionFind:
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n
        self.group = n

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

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return
        self.group -= 1
        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return self.group

    def all_group_members(self):
        dic = {r:[] for r in self.roots()}
        for i in range(self.n):
            dic[self.find(i)].append(i)
        return dic

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())


n = int(input())
abc = [list(map(int, input().split())) for _ in range(n - 1)]
ans = 0
bi = 1
for i in range(30):
    UF = UnionFind(n)
    for a, b, c in abc:
        if c >> i & 1:
            UF.union(a - 1, b - 1)
    for r in UF.roots():
        r = UF.size(r)
        ans += r * (r - 1) // 2 * bi
        ans %= MOD
    bi *= 2
print(ans)
0