結果

問題 No.1207 グラフX
ユーザー 37zigen37zigen
提出日時 2020-08-30 22:01:36
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,437 ms / 2,000 ms
コード長 1,089 bytes
コンパイル時間 162 ms
コンパイル使用メモリ 82,404 KB
実行使用メモリ 295,620 KB
最終ジャッジ日時 2024-04-27 13:27:38
合計ジャッジ時間 32,351 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 656 ms
120,104 KB
testcase_01 AC 648 ms
119,896 KB
testcase_02 AC 650 ms
120,104 KB
testcase_03 AC 665 ms
120,592 KB
testcase_04 AC 662 ms
119,848 KB
testcase_05 AC 1,406 ms
295,476 KB
testcase_06 AC 1,437 ms
293,852 KB
testcase_07 AC 1,393 ms
295,620 KB
testcase_08 AC 596 ms
103,112 KB
testcase_09 AC 644 ms
110,880 KB
testcase_10 AC 1,435 ms
261,812 KB
testcase_11 AC 1,379 ms
294,400 KB
testcase_12 AC 585 ms
105,196 KB
testcase_13 AC 349 ms
84,288 KB
testcase_14 AC 670 ms
120,008 KB
testcase_15 AC 654 ms
109,444 KB
testcase_16 AC 369 ms
85,836 KB
testcase_17 AC 498 ms
100,652 KB
testcase_18 AC 433 ms
100,588 KB
testcase_19 AC 465 ms
92,420 KB
testcase_20 AC 684 ms
118,876 KB
testcase_21 AC 103 ms
76,820 KB
testcase_22 AC 529 ms
101,904 KB
testcase_23 AC 554 ms
105,784 KB
testcase_24 AC 380 ms
97,748 KB
testcase_25 AC 686 ms
119,576 KB
testcase_26 AC 572 ms
108,204 KB
testcase_27 AC 658 ms
114,092 KB
testcase_28 AC 634 ms
110,392 KB
testcase_29 AC 616 ms
114,548 KB
testcase_30 AC 371 ms
90,984 KB
testcase_31 AC 316 ms
82,552 KB
testcase_32 AC 340 ms
92,660 KB
testcase_33 AC 358 ms
92,064 KB
testcase_34 AC 595 ms
108,996 KB
testcase_35 AC 147 ms
78,536 KB
testcase_36 AC 595 ms
109,160 KB
testcase_37 AC 549 ms
102,624 KB
testcase_38 AC 239 ms
82,952 KB
testcase_39 AC 365 ms
93,572 KB
testcase_40 AC 219 ms
78,636 KB
testcase_41 AC 455 ms
91,916 KB
testcase_42 AC 35 ms
52,200 KB
testcase_43 AC 34 ms
52,648 KB
testcase_44 AC 35 ms
53,204 KB
testcase_45 AC 650 ms
119,604 KB
testcase_46 AC 633 ms
119,108 KB
testcase_47 AC 632 ms
119,116 KB
testcase_48 AC 649 ms
119,628 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(10**6)

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