結果

問題 No.1207 グラフX
ユーザー 37zigen37zigen
提出日時 2020-08-30 22:06:09
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,439 ms / 2,000 ms
コード長 1,114 bytes
コンパイル時間 166 ms
コンパイル使用メモリ 81,868 KB
実行使用メモリ 299,688 KB
最終ジャッジ日時 2024-04-27 13:29:53
合計ジャッジ時間 30,587 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 621 ms
116,528 KB
testcase_01 AC 621 ms
116,876 KB
testcase_02 AC 617 ms
116,856 KB
testcase_03 AC 612 ms
116,824 KB
testcase_04 AC 627 ms
116,548 KB
testcase_05 AC 1,389 ms
299,324 KB
testcase_06 AC 1,391 ms
297,952 KB
testcase_07 AC 1,374 ms
299,688 KB
testcase_08 AC 556 ms
100,856 KB
testcase_09 AC 584 ms
108,368 KB
testcase_10 AC 1,439 ms
257,320 KB
testcase_11 AC 1,367 ms
296,308 KB
testcase_12 AC 536 ms
103,204 KB
testcase_13 AC 312 ms
83,612 KB
testcase_14 AC 640 ms
117,324 KB
testcase_15 AC 586 ms
106,692 KB
testcase_16 AC 329 ms
85,104 KB
testcase_17 AC 481 ms
99,340 KB
testcase_18 AC 398 ms
98,776 KB
testcase_19 AC 429 ms
90,276 KB
testcase_20 AC 660 ms
117,800 KB
testcase_21 AC 90 ms
77,260 KB
testcase_22 AC 472 ms
100,336 KB
testcase_23 AC 511 ms
103,632 KB
testcase_24 AC 361 ms
96,872 KB
testcase_25 AC 647 ms
116,568 KB
testcase_26 AC 536 ms
106,944 KB
testcase_27 AC 626 ms
112,604 KB
testcase_28 AC 603 ms
108,376 KB
testcase_29 AC 579 ms
112,240 KB
testcase_30 AC 361 ms
89,900 KB
testcase_31 AC 285 ms
82,004 KB
testcase_32 AC 308 ms
91,496 KB
testcase_33 AC 351 ms
90,804 KB
testcase_34 AC 569 ms
107,300 KB
testcase_35 AC 138 ms
77,168 KB
testcase_36 AC 566 ms
107,444 KB
testcase_37 AC 506 ms
100,928 KB
testcase_38 AC 225 ms
82,244 KB
testcase_39 AC 343 ms
92,392 KB
testcase_40 AC 179 ms
78,128 KB
testcase_41 AC 423 ms
91,064 KB
testcase_42 AC 36 ms
52,268 KB
testcase_43 AC 36 ms
52,132 KB
testcase_44 AC 36 ms
52,204 KB
testcase_45 AC 610 ms
116,148 KB
testcase_46 AC 611 ms
116,300 KB
testcase_47 AC 588 ms
116,168 KB
testcase_48 AC 600 ms
116,172 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

MOD=10**9+7

class UnionFind():
    def __init__(self,n):
        self.upper=[-1]*n

    def root(self,x):
        if self.upper[x]<0:
            return x
        else:
            self.upper[x]=self.root(self.upper[x])
            return self.upper[x]

    def equiv(self,x,y):
        return self.root(x)==self.root(y)

    def unite(self,x,y):
        x=self.root(x)
        y=self.root(y)
        if x==y:
            return
        if self.upper[x]>self.upper[y]:
            x,y=y,x
        self.upper[x]+=self.upper[y]
        self.upper[y]=x

N,M,X=map(int,input().split())
g=[[] for _ in range(N+1)]
uf=UnionFind(N+1)
for i in range(M):
    x,y,z=map(int,input().split())
    if not uf.equiv(x,y):
        g[x].append((y,pow(X,z,MOD)))
        g[y].append((x,pow(X,z,MOD)))
        uf.unite(x,y)

sz=[1]*(N+1)
ans=0

def dfs(v,p):
    global ans
    for dst,cost in g[v]:
        if dst==p:
            continue
        dfs(dst,v)
        ans+=sz[dst]*(N-sz[dst])%MOD*cost%MOD
        ans%=MOD
        sz[v]+=sz[dst]

dfs(1,-1)
print(ans)
0