結果
問題 | No.1207 グラフX |
ユーザー | 👑 Kazun |
提出日時 | 2020-08-30 17:08:37 |
言語 | PyPy3 (7.3.13) |
結果 |
AC
|
実行時間 | 1,313 ms / 2,000 ms |
コード長 | 3,172 bytes |
コンパイル時間 | 1,474 ms |
コンパイル使用メモリ | 85,100 KB |
実行使用メモリ | 219,944 KB |
最終ジャッジ日時 | 2023-08-09 16:39:25 |
合計ジャッジ時間 | 41,584 ms |
ジャッジサーバーID (参考情報) |
judge11 / judge14 |
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 886 ms
206,024 KB |
testcase_01 | AC | 863 ms
206,284 KB |
testcase_02 | AC | 855 ms
205,540 KB |
testcase_03 | AC | 874 ms
205,900 KB |
testcase_04 | AC | 860 ms
205,616 KB |
testcase_05 | AC | 1,252 ms
217,640 KB |
testcase_06 | AC | 1,259 ms
218,692 KB |
testcase_07 | AC | 1,313 ms
219,944 KB |
testcase_08 | AC | 822 ms
145,180 KB |
testcase_09 | AC | 846 ms
163,936 KB |
testcase_10 | AC | 1,269 ms
211,924 KB |
testcase_11 | AC | 1,273 ms
216,032 KB |
testcase_12 | AC | 818 ms
150,240 KB |
testcase_13 | AC | 504 ms
90,576 KB |
testcase_14 | AC | 904 ms
187,636 KB |
testcase_15 | AC | 856 ms
173,360 KB |
testcase_16 | AC | 558 ms
97,332 KB |
testcase_17 | AC | 707 ms
136,028 KB |
testcase_18 | AC | 590 ms
146,012 KB |
testcase_19 | AC | 693 ms
110,164 KB |
testcase_20 | AC | 911 ms
202,348 KB |
testcase_21 | AC | 195 ms
78,848 KB |
testcase_22 | AC | 715 ms
147,416 KB |
testcase_23 | AC | 735 ms
151,908 KB |
testcase_24 | AC | 542 ms
130,844 KB |
testcase_25 | AC | 907 ms
202,676 KB |
testcase_26 | AC | 783 ms
164,024 KB |
testcase_27 | AC | 874 ms
188,476 KB |
testcase_28 | AC | 855 ms
171,760 KB |
testcase_29 | AC | 831 ms
188,976 KB |
testcase_30 | AC | 566 ms
114,340 KB |
testcase_31 | AC | 453 ms
86,692 KB |
testcase_32 | AC | 498 ms
119,068 KB |
testcase_33 | AC | 524 ms
112,324 KB |
testcase_34 | AC | 818 ms
157,332 KB |
testcase_35 | AC | 246 ms
80,288 KB |
testcase_36 | AC | 817 ms
177,072 KB |
testcase_37 | AC | 806 ms
144,300 KB |
testcase_38 | AC | 361 ms
91,456 KB |
testcase_39 | AC | 518 ms
117,180 KB |
testcase_40 | AC | 324 ms
80,640 KB |
testcase_41 | AC | 644 ms
111,832 KB |
testcase_42 | AC | 96 ms
71,268 KB |
testcase_43 | AC | 98 ms
71,276 KB |
testcase_44 | AC | 96 ms
71,320 KB |
testcase_45 | AC | 782 ms
198,684 KB |
testcase_46 | AC | 779 ms
199,096 KB |
testcase_47 | AC | 782 ms
199,176 KB |
testcase_48 | AC | 777 ms
198,828 KB |
ソースコード
class Union_Find(): def __init__(self,N): """0,1,...,n-1を要素として初期化する. n:要素数 """ self.n=N self.parents=[-1]*N self.rank=[0]*N def find(self, x): """要素xの属している族を調べる. 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,yを同一視する. x,y:要素 """ x = self.find(x) y = self.find(y) if x == y: return if self.rank[x]>self.rank[y]: self.parents[x] += self.parents[y] self.parents[y] = x else: self.parents[y] += self.parents[x] self.parents[x] = y if self.rank[x]==self.rank[y]: self.rank[y]+=1 def size(self, x): """要素xの属している要素の数. x:要素 """ return -self.parents[self.find(x)] def same(self, x, y): """要素x,yは同一視されているか? x,y:要素 """ return self.find(x) == self.find(y) def members(self, x): """要素xが属している族の要素. ※族の要素の個数が欲しいときはsizeを使うこと!! 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): """全ての族の出力 """ return {r: self.members(r) for r in self.roots()} def __str__(self): return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots()) #================================================ from collections import deque import sys N,M,X=map(int,input().split()) sys.setrecursionlimit(2*10**5) Mod=10**9+7 E=[[] for _ in range(N+1)] F=[] U=Union_Find(N+1) for i in range(M): x,y,z=tuple(map(int,input().split())) if not U.same(x,y): U.union(x,y) E[x].append(y) E[y].append(x) F.append((x,y,z)) #---------------------------- Parent=[-1]*(N+1) Parent[1]=0 Depth=[-1]*(N+1) Depth[1]=0 Depthset=[[] for _ in range(N)] Depthset[0]=[1] Direct_Children=[set() for _ in range(N+1)] Q=deque([1]) while Q: u=Q.popleft() for v in E[u]: if Parent[v]==-1: Q.append(v) Parent[v]=u Direct_Children[u].add(v) Depth[v]=Depth[u]+1 Depthset[Depth[v]].append(v) Descendants_Count=[0]*(N+1) for D in Depthset[::-1]: for x in D: Descendants_Count[x]+=1 Descendants_Count[Parent[x]]+=Descendants_Count[x] Ans=0 for (x,y,z) in F: if Depth[x]<Depth[y]: a,b=x,y else: a,b=y,x Ans+=Descendants_Count[b]*(N-Descendants_Count[b])*pow(X,z,Mod) Ans%=Mod print(Ans)