結果
問題 | No.1843 Tree ANDistance |
ユーザー | Navier_Boltzmann |
提出日時 | 2023-01-15 19:27:38 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 930 ms / 2,000 ms |
コード長 | 1,743 bytes |
コンパイル時間 | 302 ms |
コンパイル使用メモリ | 82,560 KB |
実行使用メモリ | 223,556 KB |
最終ジャッジ日時 | 2024-06-09 07:25:46 |
合計ジャッジ時間 | 20,932 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 925 ms
196,292 KB |
testcase_01 | AC | 903 ms
190,016 KB |
testcase_02 | AC | 874 ms
197,560 KB |
testcase_03 | AC | 863 ms
190,660 KB |
testcase_04 | AC | 930 ms
193,336 KB |
testcase_05 | AC | 738 ms
206,016 KB |
testcase_06 | AC | 835 ms
223,556 KB |
testcase_07 | AC | 867 ms
188,544 KB |
testcase_08 | AC | 901 ms
187,808 KB |
testcase_09 | AC | 770 ms
165,728 KB |
testcase_10 | AC | 767 ms
172,500 KB |
testcase_11 | AC | 896 ms
188,612 KB |
testcase_12 | AC | 638 ms
185,332 KB |
testcase_13 | AC | 704 ms
194,800 KB |
testcase_14 | AC | 178 ms
78,592 KB |
testcase_15 | AC | 177 ms
79,020 KB |
testcase_16 | AC | 135 ms
77,916 KB |
testcase_17 | AC | 134 ms
77,548 KB |
testcase_18 | AC | 119 ms
77,092 KB |
testcase_19 | AC | 138 ms
78,336 KB |
testcase_20 | AC | 134 ms
77,824 KB |
testcase_21 | AC | 52 ms
55,936 KB |
testcase_22 | AC | 54 ms
55,808 KB |
testcase_23 | AC | 54 ms
56,064 KB |
testcase_24 | AC | 52 ms
55,808 KB |
testcase_25 | AC | 53 ms
56,192 KB |
testcase_26 | AC | 54 ms
56,192 KB |
testcase_27 | AC | 53 ms
55,680 KB |
testcase_28 | AC | 620 ms
196,432 KB |
testcase_29 | AC | 463 ms
148,132 KB |
testcase_30 | AC | 459 ms
149,172 KB |
testcase_31 | AC | 643 ms
219,840 KB |
testcase_32 | AC | 612 ms
198,860 KB |
testcase_33 | AC | 215 ms
103,728 KB |
testcase_34 | AC | 476 ms
165,668 KB |
testcase_35 | AC | 54 ms
55,808 KB |
testcase_36 | AC | 53 ms
55,424 KB |
testcase_37 | AC | 54 ms
56,064 KB |
ソースコード
from collections import * from itertools import * from heapq import * from functools import * import sys,math input = sys.stdin.readline N = int(input()) edge = defaultdict(int) for _ in range(N-1): a,b,c = map(int,input().split()) a -= 1 b -= 1 edge[(a,b)]=c mod = 10**9 + 7 class DSU: def __init__(self, n): self._n = n self.parent_or_size = [-1] * n def merge(self, a, b): assert 0 <= a < self._n assert 0 <= b < self._n x, y = self.leader(a), 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, b): assert 0 <= a < self._n assert 0 <= b < self._n return self.leader(a) == self.leader(b) def leader(self, a): assert 0 <= a < self._n if self.parent_or_size[a] < 0: return a self.parent_or_size[a] = self.leader(self.parent_or_size[a]) return self.parent_or_size[a] def size(self, a): assert 0 <= a < self._n return -self.parent_or_size[self.leader(a)] def groups(self): leader_buf = [self.leader(i) for i in range(self._n)] result = [[] for _ in range(self._n)] for i in range(self._n): result[leader_buf[i]].append(i) return [r for r in result if r != []] ans = 0 for i in range(31): D = DSU(N) for k,v in edge.items(): if (v>>i)&1: D.merge(k[0],k[1]) B = (1<<i) for g in D.groups(): n = len(g) ans += B*(n*(n-1))//2 ans %= mod print(ans)