結果

問題 No.1207 グラフX
ユーザー marroncastlemarroncastle
提出日時 2020-08-30 18:41:12
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,658 ms / 2,000 ms
コード長 1,185 bytes
コンパイル時間 183 ms
コンパイル使用メモリ 82,464 KB
実行使用メモリ 349,316 KB
最終ジャッジ日時 2024-04-27 13:02:00
合計ジャッジ時間 36,787 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 751 ms
168,060 KB
testcase_01 AC 749 ms
167,932 KB
testcase_02 AC 754 ms
166,524 KB
testcase_03 AC 742 ms
167,084 KB
testcase_04 AC 747 ms
167,988 KB
testcase_05 AC 1,632 ms
348,344 KB
testcase_06 AC 1,629 ms
349,316 KB
testcase_07 AC 1,627 ms
349,148 KB
testcase_08 AC 742 ms
140,112 KB
testcase_09 AC 742 ms
151,652 KB
testcase_10 AC 1,658 ms
302,088 KB
testcase_11 AC 1,637 ms
345,104 KB
testcase_12 AC 711 ms
143,204 KB
testcase_13 AC 462 ms
106,232 KB
testcase_14 AC 798 ms
165,612 KB
testcase_15 AC 763 ms
150,100 KB
testcase_16 AC 459 ms
106,520 KB
testcase_17 AC 627 ms
131,060 KB
testcase_18 AC 509 ms
130,176 KB
testcase_19 AC 615 ms
122,028 KB
testcase_20 AC 782 ms
166,724 KB
testcase_21 AC 138 ms
81,016 KB
testcase_22 AC 599 ms
134,660 KB
testcase_23 AC 620 ms
139,836 KB
testcase_24 AC 443 ms
123,728 KB
testcase_25 AC 780 ms
165,440 KB
testcase_26 AC 655 ms
144,304 KB
testcase_27 AC 753 ms
159,620 KB
testcase_28 AC 752 ms
153,216 KB
testcase_29 AC 709 ms
157,880 KB
testcase_30 AC 470 ms
112,600 KB
testcase_31 AC 412 ms
100,552 KB
testcase_32 AC 400 ms
112,348 KB
testcase_33 AC 443 ms
112,060 KB
testcase_34 AC 722 ms
147,684 KB
testcase_35 AC 179 ms
82,308 KB
testcase_36 AC 711 ms
150,188 KB
testcase_37 AC 656 ms
135,760 KB
testcase_38 AC 284 ms
92,608 KB
testcase_39 AC 437 ms
114,620 KB
testcase_40 AC 308 ms
93,276 KB
testcase_41 AC 580 ms
119,932 KB
testcase_42 AC 40 ms
53,964 KB
testcase_43 AC 39 ms
52,996 KB
testcase_44 AC 40 ms
53,816 KB
testcase_45 AC 728 ms
169,304 KB
testcase_46 AC 719 ms
169,572 KB
testcase_47 AC 720 ms
169,368 KB
testcase_48 AC 721 ms
169,568 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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 same(self, x, y):
    return self.find(x) == self.find(y)

N, M, X = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(M)]
uf = UnionFind(N)
edge = [[] for _ in range(N)]
num = 0
for x,y,z in A:
  x -= 1
  y -= 1
  if not uf.same(x,y):
    uf.union(x,y)
    edge[x].append((y,z,num))
    edge[y].append((x,z,num))
    num += 1
    if num==N-1:
      break

import sys
sys.setrecursionlimit(10**6)

def dfs(v):
  global ans
  res = 0
  for u,z,num in edge[v]:
    if used[num]==False:
      used[num] = True
      v_num = dfs(u)
      ans += v_num*(N-v_num)*pow(X,z,mod)
      ans %= mod
      res += v_num
  return res+1

used = [False]*(N-1)
ans = 0
mod = 10**9+7
dfs(0)

print(ans)
0