結果
問題 | No.1843 Tree ANDistance |
ユーザー | eve__fuyuki |
提出日時 | 2024-04-11 00:20:07 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,276 ms / 2,000 ms |
コード長 | 2,094 bytes |
コンパイル時間 | 168 ms |
コンパイル使用メモリ | 82,000 KB |
実行使用メモリ | 281,996 KB |
最終ジャッジ日時 | 2024-10-02 21:07:16 |
合計ジャッジ時間 | 24,594 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1,276 ms
280,024 KB |
testcase_01 | AC | 1,243 ms
274,964 KB |
testcase_02 | AC | 1,156 ms
276,908 KB |
testcase_03 | AC | 1,135 ms
281,996 KB |
testcase_04 | AC | 1,246 ms
268,112 KB |
testcase_05 | AC | 953 ms
277,808 KB |
testcase_06 | AC | 1,061 ms
269,124 KB |
testcase_07 | AC | 1,153 ms
270,152 KB |
testcase_08 | AC | 1,180 ms
268,924 KB |
testcase_09 | AC | 1,104 ms
275,864 KB |
testcase_10 | AC | 1,019 ms
265,052 KB |
testcase_11 | AC | 1,264 ms
279,948 KB |
testcase_12 | AC | 856 ms
276,436 KB |
testcase_13 | AC | 1,054 ms
274,056 KB |
testcase_14 | AC | 211 ms
80,684 KB |
testcase_15 | AC | 211 ms
80,824 KB |
testcase_16 | AC | 175 ms
79,872 KB |
testcase_17 | AC | 169 ms
79,872 KB |
testcase_18 | AC | 146 ms
79,360 KB |
testcase_19 | AC | 169 ms
80,372 KB |
testcase_20 | AC | 169 ms
80,128 KB |
testcase_21 | AC | 76 ms
66,944 KB |
testcase_22 | AC | 68 ms
67,320 KB |
testcase_23 | AC | 67 ms
67,328 KB |
testcase_24 | AC | 69 ms
67,376 KB |
testcase_25 | AC | 70 ms
67,200 KB |
testcase_26 | AC | 69 ms
67,712 KB |
testcase_27 | AC | 70 ms
67,712 KB |
testcase_28 | AC | 686 ms
213,212 KB |
testcase_29 | AC | 520 ms
155,196 KB |
testcase_30 | AC | 503 ms
154,868 KB |
testcase_31 | AC | 737 ms
231,236 KB |
testcase_32 | AC | 718 ms
219,392 KB |
testcase_33 | AC | 265 ms
107,264 KB |
testcase_34 | AC | 578 ms
176,620 KB |
testcase_35 | AC | 73 ms
67,072 KB |
testcase_36 | AC | 74 ms
66,944 KB |
testcase_37 | AC | 76 ms
67,328 KB |
ソースコード
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)