結果
問題 | No.1843 Tree ANDistance |
ユーザー | ch1channn |
提出日時 | 2023-03-03 03:06:56 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 426 ms / 2,000 ms |
コード長 | 2,033 bytes |
コンパイル時間 | 124 ms |
コンパイル使用メモリ | 82,188 KB |
実行使用メモリ | 211,512 KB |
最終ジャッジ日時 | 2024-09-17 19:17:16 |
合計ジャッジ時間 | 11,877 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 396 ms
119,608 KB |
testcase_01 | AC | 409 ms
118,768 KB |
testcase_02 | AC | 375 ms
120,988 KB |
testcase_03 | AC | 376 ms
120,680 KB |
testcase_04 | AC | 426 ms
119,260 KB |
testcase_05 | AC | 355 ms
120,544 KB |
testcase_06 | AC | 387 ms
119,520 KB |
testcase_07 | AC | 395 ms
117,120 KB |
testcase_08 | AC | 402 ms
117,788 KB |
testcase_09 | AC | 374 ms
119,984 KB |
testcase_10 | AC | 353 ms
119,320 KB |
testcase_11 | AC | 414 ms
119,636 KB |
testcase_12 | AC | 313 ms
118,680 KB |
testcase_13 | AC | 364 ms
120,616 KB |
testcase_14 | AC | 120 ms
77,828 KB |
testcase_15 | AC | 120 ms
77,596 KB |
testcase_16 | AC | 96 ms
76,868 KB |
testcase_17 | AC | 92 ms
77,044 KB |
testcase_18 | AC | 77 ms
75,996 KB |
testcase_19 | AC | 93 ms
77,072 KB |
testcase_20 | AC | 91 ms
76,808 KB |
testcase_21 | AC | 36 ms
54,284 KB |
testcase_22 | AC | 34 ms
53,036 KB |
testcase_23 | AC | 35 ms
52,648 KB |
testcase_24 | AC | 34 ms
53,152 KB |
testcase_25 | AC | 34 ms
54,472 KB |
testcase_26 | AC | 34 ms
53,444 KB |
testcase_27 | AC | 35 ms
53,140 KB |
testcase_28 | AC | 322 ms
125,448 KB |
testcase_29 | AC | 266 ms
113,396 KB |
testcase_30 | AC | 260 ms
113,140 KB |
testcase_31 | AC | 360 ms
211,512 KB |
testcase_32 | AC | 337 ms
195,280 KB |
testcase_33 | AC | 126 ms
93,092 KB |
testcase_34 | AC | 249 ms
117,972 KB |
testcase_35 | AC | 35 ms
53,440 KB |
testcase_36 | AC | 35 ms
53,696 KB |
testcase_37 | AC | 37 ms
53,712 KB |
ソースコード
import sys if sys.platform =='ios': import clipboard a=clipboard.get() a = a.split('\n') text = '\n'.join(a) with open('input_file.txt','w') as f: f.write(text) sys.stdin = open('input_file.txt') sys.setrecursionlimit(4100000) stdin = sys.stdin ni = lambda: int(ns()) na = lambda: list(map(int, stdin.readline().split())) ns = lambda: stdin.readline().strip() nm = lambda: map(int,input().split()) MOD = 10 ** 9 + 7 class UnionFind: def __init__(self, n): self.n = n self.parents = [-1] * n self.group = 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 self.group -= 1 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 self.group def all_group_members(self): dic = {r:[] for r in self.roots()} for i in range(self.n): dic[self.find(i)].append(i) return dic def __str__(self): return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots()) n = int(input()) abc = [list(map(int, input().split())) for _ in range(n - 1)] ans = 0 bi = 1 for i in range(30): UF = UnionFind(n) for a, b, c in abc: if c >> i & 1: UF.union(a - 1, b - 1) for r in UF.roots(): r = UF.size(r) ans += r * (r - 1) // 2 * bi ans %= MOD bi *= 2 print(ans)