結果

問題 No.1843 Tree ANDistance
ユーザー 👑 H20H20
提出日時 2022-02-18 21:53:59
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,348 ms / 2,000 ms
コード長 1,733 bytes
コンパイル時間 2,123 ms
コンパイル使用メモリ 86,812 KB
実行使用メモリ 314,108 KB
最終ジャッジ日時 2023-09-11 19:03:01
合計ジャッジ時間 29,937 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,308 ms
303,868 KB
testcase_01 AC 1,294 ms
298,328 KB
testcase_02 AC 1,240 ms
299,712 KB
testcase_03 AC 1,218 ms
281,280 KB
testcase_04 AC 1,348 ms
295,032 KB
testcase_05 AC 952 ms
294,912 KB
testcase_06 AC 1,042 ms
298,512 KB
testcase_07 AC 1,206 ms
296,592 KB
testcase_08 AC 1,287 ms
293,444 KB
testcase_09 AC 1,213 ms
275,820 KB
testcase_10 AC 1,158 ms
277,116 KB
testcase_11 AC 1,338 ms
301,000 KB
testcase_12 AC 864 ms
285,816 KB
testcase_13 AC 983 ms
289,260 KB
testcase_14 AC 234 ms
80,524 KB
testcase_15 AC 233 ms
80,328 KB
testcase_16 AC 189 ms
79,328 KB
testcase_17 AC 184 ms
79,348 KB
testcase_18 AC 159 ms
78,476 KB
testcase_19 AC 185 ms
80,012 KB
testcase_20 AC 178 ms
79,256 KB
testcase_21 AC 99 ms
71,480 KB
testcase_22 AC 99 ms
71,688 KB
testcase_23 AC 98 ms
71,740 KB
testcase_24 AC 97 ms
71,612 KB
testcase_25 AC 99 ms
71,952 KB
testcase_26 AC 99 ms
71,864 KB
testcase_27 AC 99 ms
71,600 KB
testcase_28 AC 1,020 ms
303,112 KB
testcase_29 AC 717 ms
250,888 KB
testcase_30 AC 739 ms
270,944 KB
testcase_31 AC 1,012 ms
314,108 KB
testcase_32 AC 981 ms
299,156 KB
testcase_33 AC 360 ms
142,912 KB
testcase_34 AC 802 ms
301,452 KB
testcase_35 AC 100 ms
71,772 KB
testcase_36 AC 100 ms
71,356 KB
testcase_37 AC 100 ms
71,712 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# UnionFind 参考は以下のサイト
# https://note.nkmk.me/python-union-find/
from collections import defaultdict

class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * 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

        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 len(self.roots())

    def all_group_members(self):
        group_members = defaultdict(list)
        for member in range(self.n):
            group_members[self.find(member)].append(member)
        return group_members

    def __str__(self):
        return '\n'.join(f'{r}: {m}' for r, m in self.all_group_members().items())

N = int(input())
ABC = [list(map(int, input().split())) for _ in range(N-1)]
mod = 10**9+7
ans = 0
i = 1
while i<2**32:
    uf = UnionFind(N)
    for a,b,c in ABC:
        if c&i>0:
            uf.union(a-1,b-1)
    GM = uf.all_group_members()
    R = uf.roots()
    for r in R:
        l = uf.size(r)
        ans = (ans+(l-1)*l//2*i)%mod
    i*=2
print(ans)
0