結果
問題 | No.1207 グラフX |
ユーザー | 👑 Kazun |
提出日時 | 2020-08-30 16:19:26 |
言語 | PyPy3 (7.3.15) |
結果 |
RE
|
実行時間 | - |
コード長 | 3,102 bytes |
コンパイル時間 | 341 ms |
コンパイル使用メモリ | 82,060 KB |
実行使用メモリ | 158,604 KB |
最終ジャッジ日時 | 2024-04-27 09:56:42 |
合計ジャッジ時間 | 28,600 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 629 ms
158,112 KB |
testcase_01 | AC | 613 ms
158,604 KB |
testcase_02 | AC | 621 ms
156,664 KB |
testcase_03 | AC | 613 ms
158,164 KB |
testcase_04 | AC | 618 ms
158,556 KB |
testcase_05 | RE | - |
testcase_06 | RE | - |
testcase_07 | RE | - |
testcase_08 | AC | 594 ms
124,232 KB |
testcase_09 | AC | 612 ms
136,380 KB |
testcase_10 | RE | - |
testcase_11 | RE | - |
testcase_12 | AC | 593 ms
127,780 KB |
testcase_13 | AC | 386 ms
91,040 KB |
testcase_14 | AC | 642 ms
156,628 KB |
testcase_15 | AC | 634 ms
135,196 KB |
testcase_16 | AC | 444 ms
95,252 KB |
testcase_17 | AC | 502 ms
120,068 KB |
testcase_18 | AC | 393 ms
120,876 KB |
testcase_19 | AC | 517 ms
104,492 KB |
testcase_20 | AC | 644 ms
156,252 KB |
testcase_21 | AC | 126 ms
78,076 KB |
testcase_22 | AC | 510 ms
122,028 KB |
testcase_23 | AC | 527 ms
129,420 KB |
testcase_24 | AC | 374 ms
113,076 KB |
testcase_25 | AC | 634 ms
157,444 KB |
testcase_26 | AC | 551 ms
135,532 KB |
testcase_27 | AC | 620 ms
146,256 KB |
testcase_28 | AC | 605 ms
138,172 KB |
testcase_29 | AC | 582 ms
144,480 KB |
testcase_30 | AC | 398 ms
102,732 KB |
testcase_31 | AC | 352 ms
87,600 KB |
testcase_32 | AC | 333 ms
104,016 KB |
testcase_33 | AC | 361 ms
102,448 KB |
testcase_34 | AC | 611 ms
134,384 KB |
testcase_35 | AC | 161 ms
79,464 KB |
testcase_36 | AC | 584 ms
136,932 KB |
testcase_37 | AC | 531 ms
124,600 KB |
testcase_38 | AC | 257 ms
89,316 KB |
testcase_39 | AC | 358 ms
106,756 KB |
testcase_40 | AC | 239 ms
80,872 KB |
testcase_41 | AC | 473 ms
105,120 KB |
testcase_42 | AC | 36 ms
55,740 KB |
testcase_43 | AC | 36 ms
54,584 KB |
testcase_44 | AC | 36 ms
55,380 KB |
testcase_45 | AC | 538 ms
151,120 KB |
testcase_46 | AC | 553 ms
151,220 KB |
testcase_47 | AC | 584 ms
151,252 KB |
testcase_48 | AC | 571 ms
151,152 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 N,M,X=map(int,input().split()) Mod=10**9+7 E=[[] for _ in range(N+1)] F=[0]*(N-1) U=Union_Find(N+1) k=0 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[k]=(x,y,z) k+=1 #---------------------------- Parent=[-1]*(N+1) Parent[1]=0 Depth=[-1]*(N+1) Depth[1]=0 Direct_Children=[[] 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].append(v) Depth[v]=Depth[u]+1 Children=[-1]*(N+1) def Children_Count(x): if Children[x]!=-1: return Children[x] X=0 for a in Direct_Children[x]: X+=Children_Count(a) Children[x]=X+1 return X+1 _=Children_Count(1) Ans=0 for (x,y,z) in F: if Depth[x]<Depth[y]: a,b=x,y else: a,b=y,x Ans+=Children[b]*(N-Children[b])*pow(X,z,Mod) Ans%=Mod print(Ans)