結果

問題 No.1207 グラフX
ユーザー marroncastlemarroncastle
提出日時 2020-08-30 18:46:32
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,569 ms / 2,000 ms
コード長 1,161 bytes
コンパイル時間 163 ms
コンパイル使用メモリ 82,472 KB
実行使用メモリ 350,564 KB
最終ジャッジ日時 2024-04-27 13:03:48
合計ジャッジ時間 33,354 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 673 ms
168,924 KB
testcase_01 AC 671 ms
169,116 KB
testcase_02 AC 677 ms
168,600 KB
testcase_03 AC 675 ms
169,436 KB
testcase_04 AC 672 ms
169,272 KB
testcase_05 AC 1,453 ms
347,748 KB
testcase_06 AC 1,496 ms
347,372 KB
testcase_07 AC 1,507 ms
347,932 KB
testcase_08 AC 646 ms
138,384 KB
testcase_09 AC 660 ms
149,848 KB
testcase_10 AC 1,569 ms
311,028 KB
testcase_11 AC 1,499 ms
350,564 KB
testcase_12 AC 641 ms
141,764 KB
testcase_13 AC 395 ms
105,492 KB
testcase_14 AC 711 ms
166,168 KB
testcase_15 AC 677 ms
149,092 KB
testcase_16 AC 402 ms
106,572 KB
testcase_17 AC 557 ms
130,864 KB
testcase_18 AC 436 ms
129,468 KB
testcase_19 AC 517 ms
121,140 KB
testcase_20 AC 709 ms
166,348 KB
testcase_21 AC 107 ms
80,544 KB
testcase_22 AC 543 ms
131,452 KB
testcase_23 AC 559 ms
139,128 KB
testcase_24 AC 393 ms
121,556 KB
testcase_25 AC 705 ms
166,140 KB
testcase_26 AC 607 ms
143,040 KB
testcase_27 AC 695 ms
157,000 KB
testcase_28 AC 683 ms
152,152 KB
testcase_29 AC 641 ms
156,764 KB
testcase_30 AC 433 ms
111,616 KB
testcase_31 AC 358 ms
100,236 KB
testcase_32 AC 362 ms
111,264 KB
testcase_33 AC 392 ms
110,500 KB
testcase_34 AC 667 ms
145,812 KB
testcase_35 AC 157 ms
81,752 KB
testcase_36 AC 656 ms
148,812 KB
testcase_37 AC 604 ms
135,168 KB
testcase_38 AC 260 ms
92,500 KB
testcase_39 AC 395 ms
112,672 KB
testcase_40 AC 245 ms
93,640 KB
testcase_41 AC 499 ms
119,128 KB
testcase_42 AC 39 ms
53,084 KB
testcase_43 AC 39 ms
53,576 KB
testcase_44 AC 39 ms
53,212 KB
testcase_45 AC 640 ms
167,512 KB
testcase_46 AC 646 ms
167,544 KB
testcase_47 AC 641 ms
167,664 KB
testcase_48 AC 646 ms
167,600 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)

import sys
sys.setrecursionlimit(10**6)
input = sys.stdin.readline
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
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