結果
問題 | No.1207 グラフX |
ユーザー | naniwazu |
提出日時 | 2020-08-30 18:26:56 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 776 ms / 2,000 ms |
コード長 | 1,930 bytes |
コンパイル時間 | 313 ms |
コンパイル使用メモリ | 82,240 KB |
実行使用メモリ | 149,968 KB |
最終ジャッジ日時 | 2024-11-15 15:09:02 |
合計ジャッジ時間 | 23,405 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 503 ms
149,968 KB |
testcase_01 | AC | 481 ms
147,772 KB |
testcase_02 | AC | 474 ms
148,356 KB |
testcase_03 | AC | 489 ms
147,812 KB |
testcase_04 | AC | 505 ms
149,760 KB |
testcase_05 | AC | 776 ms
145,696 KB |
testcase_06 | AC | 744 ms
142,120 KB |
testcase_07 | AC | 749 ms
147,388 KB |
testcase_08 | AC | 446 ms
118,648 KB |
testcase_09 | AC | 467 ms
131,996 KB |
testcase_10 | AC | 758 ms
147,372 KB |
testcase_11 | AC | 750 ms
145,136 KB |
testcase_12 | AC | 437 ms
124,112 KB |
testcase_13 | AC | 266 ms
88,304 KB |
testcase_14 | AC | 497 ms
145,544 KB |
testcase_15 | AC | 475 ms
130,692 KB |
testcase_16 | AC | 268 ms
91,084 KB |
testcase_17 | AC | 388 ms
115,400 KB |
testcase_18 | AC | 322 ms
117,248 KB |
testcase_19 | AC | 350 ms
101,124 KB |
testcase_20 | AC | 491 ms
145,152 KB |
testcase_21 | AC | 86 ms
76,980 KB |
testcase_22 | AC | 370 ms
116,788 KB |
testcase_23 | AC | 388 ms
125,520 KB |
testcase_24 | AC | 288 ms
109,600 KB |
testcase_25 | AC | 537 ms
146,584 KB |
testcase_26 | AC | 428 ms
125,456 KB |
testcase_27 | AC | 476 ms
137,588 KB |
testcase_28 | AC | 468 ms
131,184 KB |
testcase_29 | AC | 463 ms
138,032 KB |
testcase_30 | AC | 282 ms
99,896 KB |
testcase_31 | AC | 233 ms
83,464 KB |
testcase_32 | AC | 246 ms
101,192 KB |
testcase_33 | AC | 276 ms
101,376 KB |
testcase_34 | AC | 449 ms
125,444 KB |
testcase_35 | AC | 123 ms
77,844 KB |
testcase_36 | AC | 442 ms
130,112 KB |
testcase_37 | AC | 409 ms
118,880 KB |
testcase_38 | AC | 196 ms
86,484 KB |
testcase_39 | AC | 272 ms
101,608 KB |
testcase_40 | AC | 161 ms
78,424 KB |
testcase_41 | AC | 343 ms
102,236 KB |
testcase_42 | AC | 39 ms
55,000 KB |
testcase_43 | AC | 38 ms
55,628 KB |
testcase_44 | AC | 39 ms
54,744 KB |
testcase_45 | AC | 469 ms
147,708 KB |
testcase_46 | AC | 469 ms
148,240 KB |
testcase_47 | AC | 471 ms
148,204 KB |
testcase_48 | AC | 533 ms
148,252 KB |
ソースコード
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): 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()) import sys from collections import deque input = sys.stdin.buffer.readline mod = 10**9+7 N,M,X = map(int,input().split()) Tree = [[] for _ in range(N)] edges = [] uf = UnionFind(N) for i in range(M): x,y,z = map(int,input().split()) if not uf.same(x-1,y-1): uf.union(x-1,y-1) Tree[x-1].append(y-1) Tree[y-1].append(x-1) edges.append((x,y,z)) size = [1]*N parent = [-1]*N d = deque() d.append(0) nodes = [] while d: x = d.popleft() for y in Tree[x]: if parent[x] != y: d.append(y) parent[y] = x nodes.append(y) for x in nodes[::-1]: p = parent[x] size[p] += size[x] ans = 0 for x,y,z in edges: s = min(size[x-1],size[y-1]) ans += s*(N-s)*pow(X,z,mod) ans %= mod print(ans)