結果
問題 | No.1843 Tree ANDistance |
ユーザー | moharan627 |
提出日時 | 2022-02-18 22:26:26 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,667 ms / 2,000 ms |
コード長 | 3,011 bytes |
コンパイル時間 | 125 ms |
コンパイル使用メモリ | 82,440 KB |
実行使用メモリ | 168,856 KB |
最終ジャッジ日時 | 2024-06-29 09:18:01 |
合計ジャッジ時間 | 29,436 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1,460 ms
152,800 KB |
testcase_01 | AC | 1,418 ms
163,364 KB |
testcase_02 | AC | 1,514 ms
164,516 KB |
testcase_03 | AC | 1,437 ms
162,392 KB |
testcase_04 | AC | 1,667 ms
160,808 KB |
testcase_05 | AC | 1,256 ms
160,136 KB |
testcase_06 | AC | 1,240 ms
168,856 KB |
testcase_07 | AC | 1,352 ms
158,380 KB |
testcase_08 | AC | 1,478 ms
156,912 KB |
testcase_09 | AC | 1,373 ms
150,404 KB |
testcase_10 | AC | 1,437 ms
149,776 KB |
testcase_11 | AC | 1,461 ms
167,348 KB |
testcase_12 | AC | 1,111 ms
155,104 KB |
testcase_13 | AC | 1,240 ms
155,300 KB |
testcase_14 | AC | 104 ms
77,648 KB |
testcase_15 | AC | 120 ms
77,828 KB |
testcase_16 | AC | 92 ms
77,540 KB |
testcase_17 | AC | 98 ms
76,800 KB |
testcase_18 | AC | 98 ms
77,420 KB |
testcase_19 | AC | 117 ms
77,476 KB |
testcase_20 | AC | 94 ms
77,184 KB |
testcase_21 | AC | 41 ms
54,652 KB |
testcase_22 | AC | 43 ms
55,980 KB |
testcase_23 | AC | 41 ms
56,108 KB |
testcase_24 | AC | 37 ms
55,184 KB |
testcase_25 | AC | 38 ms
54,960 KB |
testcase_26 | AC | 39 ms
55,944 KB |
testcase_27 | AC | 38 ms
55,524 KB |
testcase_28 | AC | 1,136 ms
162,564 KB |
testcase_29 | AC | 573 ms
142,996 KB |
testcase_30 | AC | 756 ms
137,648 KB |
testcase_31 | AC | 1,194 ms
167,168 KB |
testcase_32 | AC | 1,200 ms
162,152 KB |
testcase_33 | AC | 301 ms
103,920 KB |
testcase_34 | AC | 709 ms
147,556 KB |
testcase_35 | AC | 40 ms
55,076 KB |
testcase_36 | AC | 41 ms
55,548 KB |
testcase_37 | AC | 41 ms
55,248 KB |
ソースコード
import sys #sys.setrecursionlimit(10 ** 6) INF = float('inf') #10**20,2**63に変えるのもあり MOD = 10**9 + 7 MOD2 = 998244353 from collections import defaultdict class UnionFind(): def __init__(self, n): self.n = n self.parents = [-1] * 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 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 len(self.roots()) def all_group_members(self): group_members = defaultdict(list) for member in range(self.n): group_members[self.find(member)].append(member) return group_members def __str__(self): return '\n'.join(f'{r}: {m}' for r, m in self.all_group_members().items()) def solve(): def II(): return int(sys.stdin.readline()) def LI(): return list(map(int, sys.stdin.readline().split())) def LC(): return list(input()) def IC(): return [int(c) for c in input()] def MI(): return map(int, sys.stdin.readline().split()) N = II() Graph = [[] for _ in range(N)] for _ in range(N-1): A,B,C =MI() A-=1 B-=1 Graph[A].append((B,C)) Graph[B].append((A,C)) from collections import deque Ans = 0 for bit in range(30): Q = deque() Seen = [False for i in range(N)] uf = UnionFind(N) for u in range(N): if (Seen[u] == True): # 始点sが探索済みなら continue else: Q.append(u) # 始点をキューに入れる while Q: s = Q.pop() Seen[s] = True # 0,→sで初めてたどり着いたときに行う処理はここで for t,c in Graph[s]: if Seen[t] == False and c & (1 << bit): uf.union(s,t) Q.append(t) Flag = defaultdict(lambda:True) Num = 2 ** bit for n in range(N): if Flag[uf.find(n)] == True: Flag[uf.find(n)] = False Size = uf.size(n) Size -=1 tmp = Size*(Size+1)//2 tmp %= MOD Ans += tmp* Num Ans %= MOD print(Ans % MOD) return solve()