結果

問題 No.1207 グラフX
ユーザー marroncastlemarroncastle
提出日時 2020-08-30 15:52:23
言語 PyPy3
(7.3.13)
結果
MLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,738 bytes
コンパイル時間 324 ms
コンパイル使用メモリ 86,896 KB
実行使用メモリ 847,660 KB
最終ジャッジ日時 2023-08-09 14:28:31
合計ジャッジ時間 16,132 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 634 ms
137,156 KB
testcase_01 AC 629 ms
137,276 KB
testcase_02 AC 625 ms
136,112 KB
testcase_03 AC 639 ms
136,888 KB
testcase_04 AC 652 ms
137,164 KB
testcase_05 AC 1,524 ms
336,928 KB
testcase_06 AC 1,494 ms
336,408 KB
testcase_07 AC 1,502 ms
338,040 KB
testcase_08 AC 648 ms
118,400 KB
testcase_09 AC 629 ms
123,024 KB
testcase_10 MLE -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
権限があれば一括ダウンロードができます

ソースコード

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)

  def roots(self):
    return [i for i, x in enumerate(self.parents) if x < 0]

  def num_roots(self):
    return len([i for i, x in enumerate(self.parents) if x < 0])

  def members(self, x):
    root = self.find(x)
    return [i for i in range(self.n) if self.find(i) == root]

  def num_members(self,x):
    return abs(self.parents[self.find(x)])

  def __str__(self):
    return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

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)]
e_length = [0]*(N-1)
num = 0
mod2 = 10**6
for x,y,z in A:
  x -= 1
  y -= 1
  if not uf.same(x,y):
    uf.union(x,y)
    e_length[num] = z
    edge[x].append(y*mod2+num)
    edge[y].append(x*mod2+num)
    num += 1
    if num==N-1:
      break

import sys
sys.setrecursionlimit(10**6)

dist = [0]
mod = 10**9+7

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

used = [False]*(N-1)
dfs(0)

print(dist[0])
0