結果

問題 No.1843 Tree ANDistance
ユーザー eve__fuyukieve__fuyuki
提出日時 2024-04-11 00:20:07
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,096 ms / 2,000 ms
コード長 2,094 bytes
コンパイル時間 256 ms
コンパイル使用メモリ 82,168 KB
実行使用メモリ 282,268 KB
最終ジャッジ日時 2024-04-11 00:20:35
合計ジャッジ時間 20,911 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 947 ms
279,824 KB
testcase_01 AC 1,039 ms
275,344 KB
testcase_02 AC 910 ms
277,424 KB
testcase_03 AC 917 ms
282,268 KB
testcase_04 AC 1,096 ms
267,984 KB
testcase_05 AC 783 ms
277,380 KB
testcase_06 AC 905 ms
269,252 KB
testcase_07 AC 916 ms
270,188 KB
testcase_08 AC 937 ms
269,320 KB
testcase_09 AC 914 ms
276,244 KB
testcase_10 AC 813 ms
264,880 KB
testcase_11 AC 1,009 ms
279,700 KB
testcase_12 AC 820 ms
276,564 KB
testcase_13 AC 843 ms
274,360 KB
testcase_14 AC 159 ms
81,064 KB
testcase_15 AC 208 ms
80,828 KB
testcase_16 AC 136 ms
79,868 KB
testcase_17 AC 130 ms
80,200 KB
testcase_18 AC 107 ms
79,584 KB
testcase_19 AC 125 ms
80,796 KB
testcase_20 AC 126 ms
80,224 KB
testcase_21 AC 53 ms
68,940 KB
testcase_22 AC 57 ms
68,556 KB
testcase_23 AC 55 ms
68,324 KB
testcase_24 AC 54 ms
69,080 KB
testcase_25 AC 52 ms
67,828 KB
testcase_26 AC 54 ms
67,648 KB
testcase_27 AC 58 ms
69,060 KB
testcase_28 AC 656 ms
213,024 KB
testcase_29 AC 419 ms
155,452 KB
testcase_30 AC 419 ms
154,844 KB
testcase_31 AC 680 ms
231,096 KB
testcase_32 AC 571 ms
219,560 KB
testcase_33 AC 218 ms
107,724 KB
testcase_34 AC 551 ms
176,720 KB
testcase_35 AC 54 ms
68,568 KB
testcase_36 AC 52 ms
67,644 KB
testcase_37 AC 55 ms
68,500 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import typing


class DSU:
    '''
    Implement (union by size) + (path halving)

    Reference:
    Zvi Galil and Giuseppe F. Italiano,
    Data structures and algorithms for disjoint set union problems
    '''

    def __init__(self, n: int = 0) -> None:
        self._n = n
        self.parent_or_size = [-1] * n

    def merge(self, a: int, b: int) -> int:
        assert 0 <= a < self._n
        assert 0 <= b < self._n

        x = self.leader(a)
        y = self.leader(b)

        if x == y:
            return x

        if -self.parent_or_size[x] < -self.parent_or_size[y]:
            x, y = y, x

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

        return x

    def same(self, a: int, b: int) -> bool:
        assert 0 <= a < self._n
        assert 0 <= b < self._n

        return self.leader(a) == self.leader(b)

    def leader(self, a: int) -> int:
        assert 0 <= a < self._n

        parent = self.parent_or_size[a]
        while parent >= 0:
            if self.parent_or_size[parent] < 0:
                return parent
            self.parent_or_size[a], a, parent = (
                self.parent_or_size[parent],
                self.parent_or_size[parent],
                self.parent_or_size[self.parent_or_size[parent]]
            )

        return a

    def size(self, a: int) -> int:
        assert 0 <= a < self._n

        return -self.parent_or_size[self.leader(a)]

    def groups(self) -> typing.List[typing.List[int]]:
        leader_buf = [self.leader(i) for i in range(self._n)]

        result: typing.List[typing.List[int]] = [[] for _ in range(self._n)]
        for i in range(self._n):
            result[leader_buf[i]].append(i)

        return list(filter(lambda r: r, result))

MOD = 10 ** 9 + 7
n = int(input())
g = [tuple(map(int, input().split())) for _ in range(n - 1)]
ans = 0
for i in range(0, 31):
	dsu = DSU(n)
	for a, b, c in g:
		if (c >> i) & 1:
			dsu.merge(a - 1, b - 1)
	for gr in dsu.groups():
		ans += (len(gr) * (len(gr) - 1) // 2 % MOD) * (1 << i) % MOD
		ans %= MOD
print(ans)
0