結果
問題 | No.1843 Tree ANDistance |
ユーザー | H3PO4 |
提出日時 | 2022-02-18 21:40:59 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 629 ms / 2,000 ms |
コード長 | 1,708 bytes |
コンパイル時間 | 402 ms |
コンパイル使用メモリ | 86,804 KB |
実行使用メモリ | 254,488 KB |
最終ジャッジ日時 | 2023-09-11 18:46:00 |
合計ジャッジ時間 | 15,013 ms |
ジャッジサーバーID (参考情報) |
judge12 / judge13 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 606 ms
176,472 KB |
testcase_01 | AC | 621 ms
175,372 KB |
testcase_02 | AC | 573 ms
197,004 KB |
testcase_03 | AC | 538 ms
187,384 KB |
testcase_04 | AC | 596 ms
185,204 KB |
testcase_05 | AC | 472 ms
186,116 KB |
testcase_06 | AC | 564 ms
200,088 KB |
testcase_07 | AC | 567 ms
167,228 KB |
testcase_08 | AC | 627 ms
174,260 KB |
testcase_09 | AC | 536 ms
184,384 KB |
testcase_10 | AC | 536 ms
178,284 KB |
testcase_11 | AC | 629 ms
188,112 KB |
testcase_12 | AC | 412 ms
188,800 KB |
testcase_13 | AC | 461 ms
188,540 KB |
testcase_14 | AC | 165 ms
78,948 KB |
testcase_15 | AC | 160 ms
79,304 KB |
testcase_16 | AC | 124 ms
77,868 KB |
testcase_17 | AC | 126 ms
77,940 KB |
testcase_18 | AC | 113 ms
77,468 KB |
testcase_19 | AC | 115 ms
78,624 KB |
testcase_20 | AC | 118 ms
77,844 KB |
testcase_21 | AC | 72 ms
71,184 KB |
testcase_22 | AC | 70 ms
71,244 KB |
testcase_23 | AC | 68 ms
71,248 KB |
testcase_24 | AC | 71 ms
71,116 KB |
testcase_25 | AC | 72 ms
71,336 KB |
testcase_26 | AC | 71 ms
71,280 KB |
testcase_27 | AC | 71 ms
71,160 KB |
testcase_28 | AC | 487 ms
194,600 KB |
testcase_29 | AC | 355 ms
179,060 KB |
testcase_30 | AC | 351 ms
157,020 KB |
testcase_31 | AC | 464 ms
254,488 KB |
testcase_32 | AC | 485 ms
195,528 KB |
testcase_33 | AC | 157 ms
105,060 KB |
testcase_34 | AC | 346 ms
195,968 KB |
testcase_35 | AC | 68 ms
71,208 KB |
testcase_36 | AC | 70 ms
71,120 KB |
testcase_37 | AC | 70 ms
71,400 KB |
ソースコード
import sys input = sys.stdin.buffer.readline class UnionFind: __slots__ = ["n", "parent", "height", "size"] def __init__(self, n): self.n = n self.parent = [i for i in range(n)] self.height = [1] * n self.size = [1] * n def find(self, x): if self.parent[x] == x: return x else: self.parent[x] = self.find(self.parent[x]) return self.parent[x] def unite(self, x, y): x = self.find(x) y = self.find(y) if x != y: if self.height[x] < self.height[y]: self.parent[x] = y self.size[y] += self.size[x] else: self.parent[y] = x self.size[x] += self.size[y] if self.height[x] == self.height[y]: self.height[x] += 1 def issame(self, x, y): return self.find(x) == self.find(y) def group_size(self, x): return self.size[self.find(x)] def group_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.parent) if i == x] def group_count(self): return len(self.roots()) N = int(input()) edges = [] for _ in range(N - 1): a, b, c = map(int, input().split()) a -= 1 b -= 1 edges.append((a, b, c)) MOD = 10 ** 9 + 7 ans = 0 for i in range(32): uf = UnionFind(N) for a, b, c in edges: if (c >> i) & 1: uf.unite(a, b) for root in uf.roots(): size = uf.group_size(root) ans += (1 << i) * size * (size - 1) // 2 ans %= MOD print(ans)