結果
問題 | No.1207 グラフX |
ユーザー | mkawa2 |
提出日時 | 2021-08-26 11:46:37 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 738 ms / 2,000 ms |
コード長 | 2,156 bytes |
コンパイル時間 | 181 ms |
コンパイル使用メモリ | 82,428 KB |
実行使用メモリ | 167,548 KB |
最終ジャッジ日時 | 2024-11-18 08:52:03 |
合計ジャッジ時間 | 28,538 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 625 ms
164,772 KB |
testcase_01 | AC | 621 ms
161,692 KB |
testcase_02 | AC | 603 ms
159,268 KB |
testcase_03 | AC | 600 ms
150,908 KB |
testcase_04 | AC | 610 ms
162,512 KB |
testcase_05 | AC | 729 ms
158,988 KB |
testcase_06 | AC | 738 ms
158,808 KB |
testcase_07 | AC | 721 ms
159,288 KB |
testcase_08 | AC | 503 ms
125,944 KB |
testcase_09 | AC | 528 ms
135,252 KB |
testcase_10 | AC | 734 ms
150,240 KB |
testcase_11 | AC | 728 ms
159,016 KB |
testcase_12 | AC | 499 ms
131,776 KB |
testcase_13 | AC | 309 ms
99,824 KB |
testcase_14 | AC | 601 ms
159,484 KB |
testcase_15 | AC | 544 ms
146,644 KB |
testcase_16 | AC | 315 ms
99,932 KB |
testcase_17 | AC | 439 ms
122,224 KB |
testcase_18 | AC | 365 ms
117,456 KB |
testcase_19 | AC | 409 ms
112,764 KB |
testcase_20 | AC | 606 ms
152,756 KB |
testcase_21 | AC | 101 ms
80,616 KB |
testcase_22 | AC | 434 ms
126,420 KB |
testcase_23 | AC | 457 ms
126,392 KB |
testcase_24 | AC | 337 ms
117,168 KB |
testcase_25 | AC | 606 ms
154,508 KB |
testcase_26 | AC | 484 ms
134,916 KB |
testcase_27 | AC | 562 ms
145,988 KB |
testcase_28 | AC | 556 ms
146,612 KB |
testcase_29 | AC | 551 ms
151,908 KB |
testcase_30 | AC | 321 ms
103,108 KB |
testcase_31 | AC | 275 ms
96,896 KB |
testcase_32 | AC | 285 ms
105,568 KB |
testcase_33 | AC | 297 ms
103,816 KB |
testcase_34 | AC | 512 ms
135,352 KB |
testcase_35 | AC | 127 ms
81,408 KB |
testcase_36 | AC | 506 ms
140,644 KB |
testcase_37 | AC | 473 ms
127,128 KB |
testcase_38 | AC | 199 ms
87,780 KB |
testcase_39 | AC | 294 ms
106,908 KB |
testcase_40 | AC | 217 ms
92,628 KB |
testcase_41 | AC | 397 ms
110,088 KB |
testcase_42 | AC | 34 ms
54,324 KB |
testcase_43 | AC | 35 ms
53,412 KB |
testcase_44 | AC | 34 ms
52,992 KB |
testcase_45 | AC | 553 ms
167,548 KB |
testcase_46 | AC | 572 ms
167,308 KB |
testcase_47 | AC | 575 ms
167,292 KB |
testcase_48 | AC | 576 ms
167,308 KB |
ソースコード
import sys sys.setrecursionlimit(200005) int1 = lambda x: int(x)-1 p2D = lambda x: print(*x, sep="\n") def II(): return int(sys.stdin.readline()) def LI(): return list(map(int, sys.stdin.readline().split())) def LI1(): return list(map(int1, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def LLI1(rows_number): return [LI1() for _ in range(rows_number)] def SI(): return sys.stdin.readline().rstrip() # dij = [(0, 1), (-1, 0), (0, -1), (1, 0)] dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)] inf = 10**9 # md = 998244353 md = 10**9+7 class UnionFind: def __init__(self, n): self.state = [-1]*n self.size_table = [1]*n # cntはグループ数 self.cnt = n def root(self, u): stack = [] while self.state[u] >= 0: stack.append(u) u = self.state[u] for v in stack: self.state[v] = u return u def same(self, u, v): return self.root(u) == self.root(v) def merge(self, u, v): u = self.root(u) v = self.root(v) if u == v: return False du = -self.state[u] dv = -self.state[v] if du < dv: u, v = v, u if du == dv: self.state[u] -= 1 self.state[v] = u self.cnt -= 1 self.size_table[u] += self.size_table[v] return True # グループの要素数 def size(self, u): return self.size_table[self.root(u)] n, m, x = LI() xyz = LLI(m) to = [[] for _ in range(n)] uf = UnionFind(n) xyz.sort(key=lambda x: x[2]) for u, v, z in xyz: u, v = u-1, v-1 if not uf.merge(u, v): continue to[u].append((v, z)) to[v].append((u, z)) size = [1]*n ans = 0 stack = [(0, -1, True)] while stack: u, par, first = stack.pop() if first: stack.append((u, par, False)) for v, z in to[u]: if v == par: continue stack.append((v, u, True)) else: for v, z in to[u]: if v == par: continue ans += size[v]*(n-size[v])*pow(x, z, md)%md size[u] += size[v] ans %= md print(ans)