結果
問題 | No.1843 Tree ANDistance |
ユーザー | moharan627 |
提出日時 | 2022-02-18 22:41:30 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,912 ms / 2,000 ms |
コード長 | 2,888 bytes |
コンパイル時間 | 156 ms |
コンパイル使用メモリ | 81,752 KB |
実行使用メモリ | 129,304 KB |
最終ジャッジ日時 | 2024-06-29 09:27:35 |
合計ジャッジ時間 | 32,458 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1,666 ms
121,248 KB |
testcase_01 | AC | 1,712 ms
123,300 KB |
testcase_02 | AC | 1,709 ms
127,488 KB |
testcase_03 | AC | 1,707 ms
126,776 KB |
testcase_04 | AC | 1,912 ms
123,516 KB |
testcase_05 | AC | 1,332 ms
129,304 KB |
testcase_06 | AC | 1,339 ms
127,688 KB |
testcase_07 | AC | 1,622 ms
121,184 KB |
testcase_08 | AC | 1,709 ms
123,668 KB |
testcase_09 | AC | 1,544 ms
117,424 KB |
testcase_10 | AC | 1,635 ms
119,896 KB |
testcase_11 | AC | 1,621 ms
126,664 KB |
testcase_12 | AC | 1,151 ms
123,196 KB |
testcase_13 | AC | 1,317 ms
121,280 KB |
testcase_14 | AC | 120 ms
77,568 KB |
testcase_15 | AC | 132 ms
77,440 KB |
testcase_16 | AC | 102 ms
76,800 KB |
testcase_17 | AC | 108 ms
77,276 KB |
testcase_18 | AC | 103 ms
76,508 KB |
testcase_19 | AC | 125 ms
77,784 KB |
testcase_20 | AC | 101 ms
77,564 KB |
testcase_21 | AC | 44 ms
54,380 KB |
testcase_22 | AC | 45 ms
54,912 KB |
testcase_23 | AC | 47 ms
54,400 KB |
testcase_24 | AC | 45 ms
54,912 KB |
testcase_25 | AC | 44 ms
54,400 KB |
testcase_26 | AC | 45 ms
54,656 KB |
testcase_27 | AC | 45 ms
54,912 KB |
testcase_28 | AC | 1,138 ms
112,556 KB |
testcase_29 | AC | 517 ms
98,268 KB |
testcase_30 | AC | 719 ms
98,264 KB |
testcase_31 | AC | 1,154 ms
117,160 KB |
testcase_32 | AC | 1,181 ms
110,580 KB |
testcase_33 | AC | 287 ms
90,300 KB |
testcase_34 | AC | 697 ms
103,124 KB |
testcase_35 | AC | 44 ms
54,400 KB |
testcase_36 | AC | 44 ms
54,400 KB |
testcase_37 | AC | 48 ms
59,904 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) Dec = [0]*N #print(uf.all_group_members()) for n in range(N): Ans += ((uf.size(n)-1-Dec[uf.find(n)]) * (2**bit))%MOD Dec[uf.find(n)] += 1 Ans %= MOD #print(bit,Ans) print(Ans % MOD) return solve()