結果
問題 | No.1843 Tree ANDistance |
ユーザー | moharan627 |
提出日時 | 2022-02-18 22:17:50 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,234 ms / 2,000 ms |
コード長 | 2,882 bytes |
コンパイル時間 | 207 ms |
コンパイル使用メモリ | 82,532 KB |
実行使用メモリ | 129,532 KB |
最終ジャッジ日時 | 2024-06-29 09:09:39 |
合計ジャッジ時間 | 21,262 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1,079 ms
121,116 KB |
testcase_01 | AC | 1,050 ms
123,812 KB |
testcase_02 | AC | 1,021 ms
127,744 KB |
testcase_03 | AC | 1,028 ms
127,196 KB |
testcase_04 | AC | 1,234 ms
123,640 KB |
testcase_05 | AC | 864 ms
129,532 KB |
testcase_06 | AC | 811 ms
127,960 KB |
testcase_07 | AC | 973 ms
121,312 KB |
testcase_08 | AC | 1,072 ms
123,788 KB |
testcase_09 | AC | 973 ms
117,740 KB |
testcase_10 | AC | 1,005 ms
120,028 KB |
testcase_11 | AC | 1,081 ms
126,664 KB |
testcase_12 | AC | 772 ms
123,768 KB |
testcase_13 | AC | 849 ms
121,760 KB |
testcase_14 | AC | 102 ms
77,808 KB |
testcase_15 | AC | 115 ms
78,080 KB |
testcase_16 | AC | 89 ms
77,196 KB |
testcase_17 | AC | 91 ms
77,516 KB |
testcase_18 | AC | 88 ms
76,900 KB |
testcase_19 | AC | 105 ms
78,096 KB |
testcase_20 | AC | 86 ms
77,384 KB |
testcase_21 | AC | 37 ms
55,476 KB |
testcase_22 | AC | 38 ms
55,704 KB |
testcase_23 | AC | 37 ms
55,196 KB |
testcase_24 | AC | 41 ms
55,656 KB |
testcase_25 | AC | 41 ms
55,264 KB |
testcase_26 | AC | 37 ms
55,068 KB |
testcase_27 | AC | 37 ms
55,536 KB |
testcase_28 | AC | 708 ms
112,304 KB |
testcase_29 | AC | 375 ms
98,324 KB |
testcase_30 | AC | 552 ms
98,536 KB |
testcase_31 | AC | 685 ms
117,292 KB |
testcase_32 | AC | 727 ms
110,968 KB |
testcase_33 | AC | 233 ms
89,788 KB |
testcase_34 | AC | 437 ms
103,248 KB |
testcase_35 | AC | 36 ms
54,456 KB |
testcase_36 | AC | 36 ms
54,812 KB |
testcase_37 | AC | 40 ms
60,296 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) Dec[uf.find(n)] += 1 Ans %= MOD #print(bit,Ans) print(Ans % MOD) return solve()