結果

問題 No.1207 グラフX
ユーザー marroncastlemarroncastle
提出日時 2020-08-30 15:42:59
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,921 ms / 2,000 ms
コード長 1,736 bytes
コンパイル時間 186 ms
コンパイル使用メモリ 82,584 KB
実行使用メモリ 390,832 KB
最終ジャッジ日時 2024-04-27 09:10:53
合計ジャッジ時間 33,287 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 651 ms
147,540 KB
testcase_01 AC 643 ms
147,528 KB
testcase_02 AC 625 ms
146,892 KB
testcase_03 AC 631 ms
147,748 KB
testcase_04 AC 630 ms
147,800 KB
testcase_05 AC 1,353 ms
318,076 KB
testcase_06 AC 1,348 ms
318,228 KB
testcase_07 AC 1,360 ms
317,388 KB
testcase_08 AC 639 ms
124,280 KB
testcase_09 AC 622 ms
130,616 KB
testcase_10 AC 1,921 ms
390,832 KB
testcase_11 AC 1,379 ms
314,680 KB
testcase_12 AC 647 ms
125,416 KB
testcase_13 AC 473 ms
101,472 KB
testcase_14 AC 668 ms
140,988 KB
testcase_15 AC 642 ms
130,804 KB
testcase_16 AC 421 ms
100,848 KB
testcase_17 AC 521 ms
117,544 KB
testcase_18 AC 421 ms
116,252 KB
testcase_19 AC 539 ms
112,456 KB
testcase_20 AC 666 ms
145,652 KB
testcase_21 AC 140 ms
81,032 KB
testcase_22 AC 535 ms
117,508 KB
testcase_23 AC 541 ms
122,056 KB
testcase_24 AC 375 ms
111,280 KB
testcase_25 AC 653 ms
141,260 KB
testcase_26 AC 561 ms
124,032 KB
testcase_27 AC 647 ms
136,200 KB
testcase_28 AC 648 ms
132,248 KB
testcase_29 AC 618 ms
135,800 KB
testcase_30 AC 439 ms
103,372 KB
testcase_31 AC 393 ms
97,540 KB
testcase_32 AC 353 ms
100,796 KB
testcase_33 AC 396 ms
101,716 KB
testcase_34 AC 620 ms
127,940 KB
testcase_35 AC 173 ms
81,940 KB
testcase_36 AC 599 ms
131,604 KB
testcase_37 AC 554 ms
120,776 KB
testcase_38 AC 257 ms
88,220 KB
testcase_39 AC 371 ms
104,708 KB
testcase_40 AC 300 ms
93,100 KB
testcase_41 AC 511 ms
109,092 KB
testcase_42 AC 39 ms
52,660 KB
testcase_43 AC 39 ms
52,840 KB
testcase_44 AC 39 ms
53,488 KB
testcase_45 AC 623 ms
145,500 KB
testcase_46 AC 623 ms
145,640 KB
testcase_47 AC 631 ms
145,384 KB
testcase_48 AC 639 ms
145,140 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)

  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
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,num))
    edge[y].append((x,num))
    num += 1
    if num==N-1:
      break

import sys
sys.setrecursionlimit(5*10**5)

def dfs(v):
  ans = 0
  for u,num in edge[v]:
    if used[num]==False:
      used[num] = True
      v_num = dfs(u)
      e_cnt[num] = v_num*(N-v_num)
      ans += v_num
  return ans+1

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

mod = 10**9+7
ans = 0
for i in range(N-1):
  ans += e_cnt[i]*pow(X,e_length[i],mod)
  ans %= mod
print(ans)



0