結果

問題 No.1843 Tree ANDistance
ユーザー tktk_snsntktk_snsn
提出日時 2022-02-18 21:52:25
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 662 ms / 2,000 ms
コード長 1,294 bytes
コンパイル時間 320 ms
コンパイル使用メモリ 87,176 KB
実行使用メモリ 144,400 KB
最終ジャッジ日時 2023-09-11 19:00:39
合計ジャッジ時間 14,850 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 648 ms
144,352 KB
testcase_01 AC 651 ms
144,400 KB
testcase_02 AC 566 ms
139,228 KB
testcase_03 AC 557 ms
139,252 KB
testcase_04 AC 622 ms
144,088 KB
testcase_05 AC 639 ms
127,660 KB
testcase_06 AC 634 ms
143,412 KB
testcase_07 AC 586 ms
141,072 KB
testcase_08 AC 623 ms
142,216 KB
testcase_09 AC 535 ms
135,812 KB
testcase_10 AC 522 ms
133,836 KB
testcase_11 AC 652 ms
143,620 KB
testcase_12 AC 662 ms
137,032 KB
testcase_13 AC 609 ms
138,560 KB
testcase_14 AC 167 ms
78,964 KB
testcase_15 AC 174 ms
79,416 KB
testcase_16 AC 141 ms
78,420 KB
testcase_17 AC 129 ms
78,140 KB
testcase_18 AC 116 ms
77,656 KB
testcase_19 AC 132 ms
78,344 KB
testcase_20 AC 131 ms
78,240 KB
testcase_21 AC 73 ms
71,292 KB
testcase_22 AC 73 ms
71,364 KB
testcase_23 AC 73 ms
71,424 KB
testcase_24 AC 73 ms
71,172 KB
testcase_25 AC 73 ms
71,236 KB
testcase_26 AC 73 ms
71,424 KB
testcase_27 AC 73 ms
71,276 KB
testcase_28 AC 295 ms
129,560 KB
testcase_29 AC 277 ms
112,708 KB
testcase_30 AC 230 ms
112,984 KB
testcase_31 AC 329 ms
132,944 KB
testcase_32 AC 288 ms
130,524 KB
testcase_33 AC 127 ms
93,684 KB
testcase_34 AC 264 ms
118,512 KB
testcase_35 AC 72 ms
71,352 KB
testcase_36 AC 72 ms
71,436 KB
testcase_37 AC 72 ms
71,016 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)
mod = 10 ** 9 + 7


class UF_tree:
    def __init__(self, n):
        self.root = [-1] * (n + 1)
        self.rank = [0] * (n + 1)

    def find(self, x):
        stack = []
        while self.root[x] >= 0:
            stack.append(x)
            x = self.root[x]
        for i in stack:
            self.root[i] = x
        return x

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

    def unite(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return False
        if self.rank[x] < self.rank[y]:
            self.root[y] += self.root[x]
            self.root[x] = y
        else:
            self.root[x] += self.root[y]
            self.root[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
        return True

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


N = int(input())
ABC = tuple(tuple(map(int, input().split())) for _ in range(N-1))


two = 1
ans = 0
for d in range(33):
    edge = []
    uf = UF_tree(N + 1)
    for a, b, c in ABC:
        if (c >> d) & 1:
            ans += uf.size(a) * uf.size(b) * two
            ans %= mod
            uf.unite(a, b)
    two <<= 1

print(ans)
0