結果
問題 | No.1207 グラフX |
ユーザー | 双六 |
提出日時 | 2020-09-01 00:07:21 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,193 ms / 2,000 ms |
コード長 | 2,232 bytes |
コンパイル時間 | 189 ms |
コンパイル使用メモリ | 82,304 KB |
実行使用メモリ | 265,324 KB |
最終ジャッジ日時 | 2024-11-17 02:58:47 |
合計ジャッジ時間 | 33,742 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 730 ms
235,332 KB |
testcase_01 | AC | 721 ms
238,012 KB |
testcase_02 | AC | 738 ms
236,376 KB |
testcase_03 | AC | 733 ms
237,768 KB |
testcase_04 | AC | 729 ms
238,004 KB |
testcase_05 | AC | 1,192 ms
263,708 KB |
testcase_06 | AC | 1,193 ms
264,432 KB |
testcase_07 | AC | 1,163 ms
259,872 KB |
testcase_08 | AC | 695 ms
171,900 KB |
testcase_09 | AC | 714 ms
208,100 KB |
testcase_10 | AC | 1,145 ms
260,876 KB |
testcase_11 | AC | 1,170 ms
265,324 KB |
testcase_12 | AC | 709 ms
192,908 KB |
testcase_13 | AC | 394 ms
99,584 KB |
testcase_14 | AC | 764 ms
242,608 KB |
testcase_15 | AC | 714 ms
211,188 KB |
testcase_16 | AC | 422 ms
112,256 KB |
testcase_17 | AC | 578 ms
160,052 KB |
testcase_18 | AC | 442 ms
151,372 KB |
testcase_19 | AC | 584 ms
139,460 KB |
testcase_20 | AC | 771 ms
258,288 KB |
testcase_21 | AC | 118 ms
77,824 KB |
testcase_22 | AC | 596 ms
172,760 KB |
testcase_23 | AC | 620 ms
188,692 KB |
testcase_24 | AC | 420 ms
145,660 KB |
testcase_25 | AC | 744 ms
244,532 KB |
testcase_26 | AC | 655 ms
189,856 KB |
testcase_27 | AC | 765 ms
225,684 KB |
testcase_28 | AC | 766 ms
209,336 KB |
testcase_29 | AC | 673 ms
215,704 KB |
testcase_30 | AC | 440 ms
126,060 KB |
testcase_31 | AC | 324 ms
92,160 KB |
testcase_32 | AC | 379 ms
127,104 KB |
testcase_33 | AC | 418 ms
126,372 KB |
testcase_34 | AC | 702 ms
195,420 KB |
testcase_35 | AC | 147 ms
78,208 KB |
testcase_36 | AC | 692 ms
194,172 KB |
testcase_37 | AC | 633 ms
179,664 KB |
testcase_38 | AC | 281 ms
96,000 KB |
testcase_39 | AC | 413 ms
128,160 KB |
testcase_40 | AC | 221 ms
79,744 KB |
testcase_41 | AC | 578 ms
143,816 KB |
testcase_42 | AC | 45 ms
54,400 KB |
testcase_43 | AC | 43 ms
54,144 KB |
testcase_44 | AC | 45 ms
54,016 KB |
testcase_45 | AC | 706 ms
236,408 KB |
testcase_46 | AC | 725 ms
236,844 KB |
testcase_47 | AC | 727 ms
236,592 KB |
testcase_48 | AC | 715 ms
236,596 KB |
ソースコード
import sys; input = sys.stdin.buffer.readline sys.setrecursionlimit(10**7) from collections import defaultdict from collections import deque con = 10 ** 9 + 7; INF = float("inf") def getlist(): return list(map(int, input().split())) class UnionFind: def __init__(self, n): self.par = [i for i in range(n + 1)] self.rank = [0] * (n + 1) def find(self, x): if self.par[x] == x: return x else: self.par[x] = self.find(self.par[x]) return self.par[x] def same_check(self, x, y): return self.find(x) == self.find(y) def union(self, x, y): x = self.find(x); y = self.find(y) if self.rank[x] < self.rank[y]: self.par[x] = y else: self.par[y] = x if self.rank[x] == self.rank[y]: self.rank[x] += 1 class Graph(object): def __init__(self): self.graph = defaultdict(list) def __len__(self): return len(self.graph) def add_edge(self, a, b): self.graph[a].append(b) class BFS(object): def __init__(self, graph, s, N): self.g = graph.graph self.Q = deque(); self.Q.append(s) self.dist = [INF] * N; self.dist[s] = 0 self.prev = [None] * N; self.prev[s] = -1 while self.Q: v = self.Q.popleft() for i in self.g[v]: if self.dist[i] == INF: self.dist[i] = self.dist[v] + 1 self.prev[i] = v self.Q.append(i) def topologicalSort(outE, inE, N): W = [1] * N Q = deque() for i in range(N): if inE[i] == 0: Q.append(i) while Q: u = Q.popleft() for v in outE[u]: inE[v] -= 1 W[v] += W[u] if inE[v] == 0: Q.append(v) return W #処理内容 def main(): N, M, X = getlist() UF = UnionFind(N) D = defaultdict(int) G = Graph() for i in range(M): x, y, z = getlist() x -= 1; y -= 1 if not UF.same_check(x, y): UF.union(x, y) G.add_edge(x, y) G.add_edge(y, x) D[(10 ** 6) * x + y] = z D[(10 ** 6) * y + x] = z BF = BFS(G, 0, N) outE = defaultdict(list) inE = defaultdict(int) for i in range(1, N): a, b = i, BF.prev[i] outE[a].append(b) inE[b] += 1 # print(outE) # print(inE) W = topologicalSort(outE, inE, N) # print(W) ans = 0 for i in range(1, N): ans += (N - W[i]) * W[i] * pow(X, D[(10 ** 6) * i + BF.prev[i]], con) ans %= con print(ans) if __name__ == '__main__': main()