結果

問題 No.1207 グラフX
ユーザー ああいいああいい
提出日時 2022-01-27 17:46:49
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 787 ms / 2,000 ms
コード長 1,172 bytes
コンパイル時間 378 ms
コンパイル使用メモリ 86,816 KB
実行使用メモリ 171,564 KB
最終ジャッジ日時 2023-08-26 18:50:57
合計ジャッジ時間 32,236 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 662 ms
163,880 KB
testcase_01 AC 659 ms
164,172 KB
testcase_02 AC 631 ms
165,368 KB
testcase_03 AC 653 ms
165,744 KB
testcase_04 AC 651 ms
164,928 KB
testcase_05 AC 779 ms
146,612 KB
testcase_06 AC 771 ms
149,956 KB
testcase_07 AC 761 ms
148,440 KB
testcase_08 AC 635 ms
131,472 KB
testcase_09 AC 680 ms
151,004 KB
testcase_10 AC 787 ms
154,816 KB
testcase_11 AC 762 ms
147,980 KB
testcase_12 AC 692 ms
138,516 KB
testcase_13 AC 477 ms
92,188 KB
testcase_14 AC 729 ms
168,908 KB
testcase_15 AC 697 ms
152,784 KB
testcase_16 AC 510 ms
97,460 KB
testcase_17 AC 584 ms
128,184 KB
testcase_18 AC 478 ms
126,492 KB
testcase_19 AC 586 ms
106,628 KB
testcase_20 AC 716 ms
171,564 KB
testcase_21 AC 176 ms
79,640 KB
testcase_22 AC 612 ms
130,056 KB
testcase_23 AC 593 ms
139,368 KB
testcase_24 AC 429 ms
123,508 KB
testcase_25 AC 701 ms
161,056 KB
testcase_26 AC 621 ms
139,836 KB
testcase_27 AC 714 ms
158,892 KB
testcase_28 AC 707 ms
148,560 KB
testcase_29 AC 692 ms
151,904 KB
testcase_30 AC 424 ms
104,848 KB
testcase_31 AC 413 ms
87,704 KB
testcase_32 AC 389 ms
109,124 KB
testcase_33 AC 450 ms
108,516 KB
testcase_34 AC 662 ms
144,200 KB
testcase_35 AC 231 ms
80,900 KB
testcase_36 AC 703 ms
154,908 KB
testcase_37 AC 661 ms
136,732 KB
testcase_38 AC 285 ms
89,896 KB
testcase_39 AC 441 ms
110,528 KB
testcase_40 AC 315 ms
80,648 KB
testcase_41 AC 559 ms
108,564 KB
testcase_42 AC 70 ms
71,204 KB
testcase_43 AC 69 ms
71,180 KB
testcase_44 AC 69 ms
71,212 KB
testcase_45 AC 597 ms
164,620 KB
testcase_46 AC 602 ms
163,920 KB
testcase_47 AC 589 ms
163,848 KB
testcase_48 AC 599 ms
163,688 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(10 ** 8)

N,M,X = map(int,input().split())
G = [[] for _ in range(N+1)]
P = 10 ** 9 + 7
edge = []
parent = list(range(N+1))
def find(i):
    if parent[i] == i:return i
    parent[i] = find(parent[i])
    return parent[i]
def unite(i,j):
    I = find(i)
    J = find(j)
    if I == J:return False
    parent[I] = J
    parent[i] = J
    return True
def isSame(i,j):
    return find(i) == find(j)

for _ in range(M):
    x,y,z = map(int,input().split())
    if isSame(x,y):
        continue
    else:
        unite(x,y)
        G[x].append(y)
        G[y].append(x)
        edge.append((x,y,z))
child = [1] * (N + 1)
"""
def dfs(now = 1,p = 0):
    for v in G[now]:
        if v == p:continue
        dfs(v,now)
        child[now] += child[v]
dfs()
"""
stack = [(1,0)]
order = []
while stack:
    now,p = stack.pop()
    order.append((now,p))
    for v in G[now]:
        if v == p:continue
        stack.append((v,now))
for v,p in order[::-1]:
    if p == 0:continue
    child[p] += child[v]
    
ans = 0
for x,y,z in edge:
    if child[x] > child[y]:
        x,y = y,x
    ans = (ans + child[x] * (N-child[x]) * pow(X,z,P)) % P
print(ans)
0