結果
問題 | No.1843 Tree ANDistance |
ユーザー | moharan627 |
提出日時 | 2022-02-18 22:43:55 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,948 ms / 2,000 ms |
コード長 | 2,906 bytes |
コンパイル時間 | 156 ms |
コンパイル使用メモリ | 82,304 KB |
実行使用メモリ | 128,908 KB |
最終ジャッジ日時 | 2024-06-29 09:30:56 |
合計ジャッジ時間 | 32,208 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1,688 ms
120,868 KB |
testcase_01 | AC | 1,721 ms
125,192 KB |
testcase_02 | AC | 1,711 ms
126,976 KB |
testcase_03 | AC | 1,776 ms
126,752 KB |
testcase_04 | AC | 1,948 ms
123,008 KB |
testcase_05 | AC | 1,315 ms
128,908 KB |
testcase_06 | AC | 1,337 ms
128,160 KB |
testcase_07 | AC | 1,603 ms
121,188 KB |
testcase_08 | AC | 1,685 ms
123,900 KB |
testcase_09 | AC | 1,532 ms
117,872 KB |
testcase_10 | AC | 1,598 ms
119,644 KB |
testcase_11 | AC | 1,688 ms
126,272 KB |
testcase_12 | AC | 1,175 ms
122,352 KB |
testcase_13 | AC | 1,371 ms
121,588 KB |
testcase_14 | AC | 131 ms
77,696 KB |
testcase_15 | AC | 141 ms
77,248 KB |
testcase_16 | AC | 109 ms
76,672 KB |
testcase_17 | AC | 116 ms
76,900 KB |
testcase_18 | AC | 110 ms
77,032 KB |
testcase_19 | AC | 132 ms
77,532 KB |
testcase_20 | AC | 109 ms
77,440 KB |
testcase_21 | AC | 48 ms
54,016 KB |
testcase_22 | AC | 47 ms
54,912 KB |
testcase_23 | AC | 46 ms
54,912 KB |
testcase_24 | AC | 47 ms
54,528 KB |
testcase_25 | AC | 46 ms
54,144 KB |
testcase_26 | AC | 46 ms
54,656 KB |
testcase_27 | AC | 47 ms
54,784 KB |
testcase_28 | AC | 1,103 ms
112,056 KB |
testcase_29 | AC | 485 ms
97,684 KB |
testcase_30 | AC | 678 ms
98,516 KB |
testcase_31 | AC | 1,091 ms
116,704 KB |
testcase_32 | AC | 1,113 ms
110,532 KB |
testcase_33 | AC | 269 ms
90,420 KB |
testcase_34 | AC | 592 ms
103,376 KB |
testcase_35 | AC | 47 ms
54,272 KB |
testcase_36 | AC | 46 ms
54,528 KB |
testcase_37 | AC | 50 ms
59,648 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()) Num = pow(2,bit,MOD) for n in range(N): Ans += (uf.size(n)-1-Dec[uf.find(n)]) * Num Dec[uf.find(n)] += 1 Ans %= MOD #print(bit,Ans) print(Ans % MOD) return solve()