結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 734 ms
166,232 KB
testcase_01 AC 743 ms
166,404 KB
testcase_02 AC 747 ms
165,552 KB
testcase_03 AC 744 ms
165,448 KB
testcase_04 AC 747 ms
166,340 KB
testcase_05 AC 1,531 ms
347,088 KB
testcase_06 AC 1,522 ms
347,464 KB
testcase_07 AC 1,537 ms
347,652 KB
testcase_08 AC 722 ms
139,344 KB
testcase_09 AC 729 ms
150,736 KB
testcase_10 AC 1,612 ms
300,388 KB
testcase_11 AC 1,520 ms
343,716 KB
testcase_12 AC 690 ms
141,920 KB
testcase_13 AC 446 ms
105,420 KB
testcase_14 AC 757 ms
163,692 KB
testcase_15 AC 721 ms
149,052 KB
testcase_16 AC 475 ms
106,656 KB
testcase_17 AC 588 ms
130,404 KB
testcase_18 AC 473 ms
128,960 KB
testcase_19 AC 578 ms
122,008 KB
testcase_20 AC 750 ms
165,304 KB
testcase_21 AC 136 ms
81,264 KB
testcase_22 AC 587 ms
133,788 KB
testcase_23 AC 614 ms
139,424 KB
testcase_24 AC 436 ms
122,956 KB
testcase_25 AC 770 ms
164,552 KB
testcase_26 AC 663 ms
143,820 KB
testcase_27 AC 738 ms
158,180 KB
testcase_28 AC 715 ms
152,376 KB
testcase_29 AC 697 ms
156,740 KB
testcase_30 AC 461 ms
112,396 KB
testcase_31 AC 396 ms
100,492 KB
testcase_32 AC 383 ms
111,552 KB
testcase_33 AC 427 ms
111,424 KB
testcase_34 AC 703 ms
147,076 KB
testcase_35 AC 173 ms
82,340 KB
testcase_36 AC 688 ms
148,900 KB
testcase_37 AC 639 ms
135,312 KB
testcase_38 AC 283 ms
91,984 KB
testcase_39 AC 440 ms
113,984 KB
testcase_40 AC 291 ms
93,468 KB
testcase_41 AC 551 ms
119,344 KB
testcase_42 AC 40 ms
53,200 KB
testcase_43 AC 39 ms
53,144 KB
testcase_44 AC 39 ms
53,964 KB
testcase_45 AC 706 ms
169,468 KB
testcase_46 AC 705 ms
169,788 KB
testcase_47 AC 715 ms
169,588 KB
testcase_48 AC 712 ms
169,828 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,p):
  global ans
  res = 0
  for u,z,num in edge[v]:
    if u!=p:
      v_num = dfs(u,v)
      ans += v_num*(N-v_num)*pow(X,z,mod)
      ans %= mod
      res += v_num
  return res+1

ans = 0
mod = 10**9+7
dfs(0,-1)

print(ans)
0