結果

問題 No.1207 グラフX
ユーザー marroncastlemarroncastle
提出日時 2020-08-30 18:52:42
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,527 ms / 2,000 ms
コード長 1,778 bytes
コンパイル時間 197 ms
コンパイル使用メモリ 82,856 KB
実行使用メモリ 346,508 KB
最終ジャッジ日時 2024-04-27 13:04:23
合計ジャッジ時間 28,705 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 573 ms
130,064 KB
testcase_01 AC 559 ms
130,296 KB
testcase_02 AC 569 ms
129,800 KB
testcase_03 AC 570 ms
130,308 KB
testcase_04 AC 565 ms
130,508 KB
testcase_05 AC 1,238 ms
313,916 KB
testcase_06 AC 1,248 ms
314,396 KB
testcase_07 AC 1,233 ms
313,628 KB
testcase_08 AC 555 ms
117,524 KB
testcase_09 AC 543 ms
120,880 KB
testcase_10 AC 1,527 ms
346,508 KB
testcase_11 AC 1,301 ms
312,592 KB
testcase_12 AC 520 ms
116,816 KB
testcase_13 AC 362 ms
99,252 KB
testcase_14 AC 578 ms
132,672 KB
testcase_15 AC 540 ms
122,964 KB
testcase_16 AC 358 ms
98,692 KB
testcase_17 AC 452 ms
112,352 KB
testcase_18 AC 376 ms
107,664 KB
testcase_19 AC 463 ms
108,072 KB
testcase_20 AC 588 ms
131,096 KB
testcase_21 AC 113 ms
80,516 KB
testcase_22 AC 451 ms
111,956 KB
testcase_23 AC 464 ms
115,692 KB
testcase_24 AC 335 ms
106,672 KB
testcase_25 AC 590 ms
131,316 KB
testcase_26 AC 495 ms
122,532 KB
testcase_27 AC 563 ms
124,848 KB
testcase_28 AC 552 ms
123,188 KB
testcase_29 AC 530 ms
127,180 KB
testcase_30 AC 366 ms
99,640 KB
testcase_31 AC 331 ms
96,764 KB
testcase_32 AC 297 ms
98,568 KB
testcase_33 AC 332 ms
98,920 KB
testcase_34 AC 531 ms
121,464 KB
testcase_35 AC 146 ms
81,640 KB
testcase_36 AC 525 ms
122,708 KB
testcase_37 AC 478 ms
114,560 KB
testcase_38 AC 230 ms
87,208 KB
testcase_39 AC 329 ms
100,496 KB
testcase_40 AC 238 ms
93,184 KB
testcase_41 AC 433 ms
106,404 KB
testcase_42 AC 39 ms
52,948 KB
testcase_43 AC 40 ms
54,792 KB
testcase_44 AC 39 ms
53,504 KB
testcase_45 AC 554 ms
138,732 KB
testcase_46 AC 549 ms
138,448 KB
testcase_47 AC 555 ms
138,512 KB
testcase_48 AC 545 ms
138,896 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())


import sys
sys.setrecursionlimit(10**6)
input = sys.stdin.readline

def dfs(v,p):
  ans = 0
  for unum in edge[v]:
    u,num = divmod(unum,mod2)
    if u!=p:
      v_num = dfs(u,v)
      e_cnt[num] = v_num*(N-v_num)
      ans += v_num
  return ans+1

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
e_cnt = [0]*(N-1)
used = [False]*(N-1)
dfs(0,-1)

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