結果

問題 No.1207 グラフX
ユーザー titiatitia
提出日時 2020-08-30 14:59:57
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 692 ms / 2,000 ms
コード長 1,719 bytes
コンパイル時間 128 ms
コンパイル使用メモリ 82,788 KB
実行使用メモリ 202,696 KB
最終ジャッジ日時 2024-04-27 07:55:51
合計ジャッジ時間 26,975 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 660 ms
199,592 KB
testcase_01 AC 654 ms
198,792 KB
testcase_02 AC 640 ms
197,388 KB
testcase_03 AC 685 ms
202,696 KB
testcase_04 AC 645 ms
199,380 KB
testcase_05 AC 678 ms
187,116 KB
testcase_06 AC 683 ms
186,768 KB
testcase_07 AC 692 ms
187,452 KB
testcase_08 AC 517 ms
157,700 KB
testcase_09 AC 601 ms
180,532 KB
testcase_10 AC 691 ms
187,224 KB
testcase_11 AC 691 ms
187,304 KB
testcase_12 AC 554 ms
164,028 KB
testcase_13 AC 283 ms
103,480 KB
testcase_14 AC 665 ms
198,372 KB
testcase_15 AC 581 ms
174,832 KB
testcase_16 AC 308 ms
106,484 KB
testcase_17 AC 456 ms
143,236 KB
testcase_18 AC 439 ms
146,356 KB
testcase_19 AC 409 ms
123,220 KB
testcase_20 AC 690 ms
199,912 KB
testcase_21 AC 101 ms
80,520 KB
testcase_22 AC 452 ms
144,144 KB
testcase_23 AC 495 ms
159,328 KB
testcase_24 AC 394 ms
137,512 KB
testcase_25 AC 669 ms
199,912 KB
testcase_26 AC 546 ms
165,608 KB
testcase_27 AC 621 ms
186,912 KB
testcase_28 AC 590 ms
173,224 KB
testcase_29 AC 610 ms
182,288 KB
testcase_30 AC 343 ms
119,524 KB
testcase_31 AC 267 ms
98,468 KB
testcase_32 AC 321 ms
118,712 KB
testcase_33 AC 325 ms
118,532 KB
testcase_34 AC 593 ms
168,884 KB
testcase_35 AC 132 ms
82,376 KB
testcase_36 AC 580 ms
172,852 KB
testcase_37 AC 488 ms
150,756 KB
testcase_38 AC 219 ms
94,424 KB
testcase_39 AC 332 ms
122,100 KB
testcase_40 AC 228 ms
93,892 KB
testcase_41 AC 426 ms
128,732 KB
testcase_42 AC 36 ms
53,420 KB
testcase_43 AC 35 ms
53,684 KB
testcase_44 AC 38 ms
53,756 KB
testcase_45 AC 676 ms
198,320 KB
testcase_46 AC 651 ms
198,288 KB
testcase_47 AC 637 ms
198,280 KB
testcase_48 AC 643 ms
198,416 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
from operator import itemgetter

mod = 10**9+7

N,M,X=map(int,input().split())

E=[tuple(map(int,input().split())) for i in range(M)]

E.sort(key=itemgetter(2))

# UnionFind

Group = [i for i in range(N+1)] # グループ分け
Nodes = [1]*(N+1) # 各グループのノードの数

def find(x):
    while Group[x] != x:
        x=Group[x]
    return x

def Union(x,y):
    if find(x) != find(y):
        if Nodes[find(x)] < Nodes[find(y)]:
            
            Nodes[find(y)] += Nodes[find(x)]
            Nodes[find(x)] = 0
            Group[find(x)] = find(y)
            
        else:
            Nodes[find(x)] += Nodes[find(y)]
            Nodes[find(y)] = 0
            Group[find(y)] = find(x)

E2=[]

for x,y,z in E:
    if find(x)!=find(y):
        E2.append((x,y,z))
        Union(x,y)

EDGE=[[] for i in range(N+1)]

D=[0]*(N+1)

for x,y,z in E2:
    EDGE[x].append((y,z))
    EDGE[y].append((x,z))
    D[x]+=1
    D[y]+=1

for i in range(1,N+1):
    if D[i]==1:
        ROOT=i
        break

QUE=[ROOT] 
Parent=[-1]*(N+1)
Score=[-1]*(N+1)
Parent[ROOT]=N+1 # ROOTの親を定めておく.
TOP_SORT=[] # トポロジカルソート

while QUE: # トポロジカルソートと同時に親を見つける
    x=QUE.pop()
    TOP_SORT.append(x)
    for to,sc in EDGE[x]:
        if Parent[to]==-1:
            Score[to]=sc
            Parent[to]=x
            QUE.append(to)

Children=[1]*(N+1)

for x in TOP_SORT[1:][::-1]: #(自分を含む)子ノードの数を調べる
    Children[Parent[x]]+=Children[x]

ANS=0
for i in range(1,N+1):
    if i==ROOT:
        continue
    c=Children[i]
    ANS=ANS+c*(N-c)*pow(X,Score[i],mod)
    ANS%=mod

print(ANS)

    
0