結果

問題 No.1207 グラフX
ユーザー titiatitia
提出日時 2020-08-30 14:59:57
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 859 ms / 2,000 ms
コード長 1,719 bytes
コンパイル時間 350 ms
コンパイル使用メモリ 87,044 KB
実行使用メモリ 207,664 KB
最終ジャッジ日時 2023-08-09 12:53:48
合計ジャッジ時間 34,183 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 807 ms
204,856 KB
testcase_01 AC 818 ms
203,100 KB
testcase_02 AC 799 ms
201,968 KB
testcase_03 AC 810 ms
202,508 KB
testcase_04 AC 790 ms
202,856 KB
testcase_05 AC 852 ms
189,040 KB
testcase_06 AC 859 ms
189,728 KB
testcase_07 AC 850 ms
186,520 KB
testcase_08 AC 672 ms
153,628 KB
testcase_09 AC 743 ms
174,696 KB
testcase_10 AC 839 ms
186,060 KB
testcase_11 AC 848 ms
190,048 KB
testcase_12 AC 688 ms
161,804 KB
testcase_13 AC 396 ms
110,396 KB
testcase_14 AC 836 ms
197,612 KB
testcase_15 AC 746 ms
178,804 KB
testcase_16 AC 413 ms
112,912 KB
testcase_17 AC 587 ms
148,980 KB
testcase_18 AC 517 ms
145,400 KB
testcase_19 AC 532 ms
130,760 KB
testcase_20 AC 851 ms
207,664 KB
testcase_21 AC 138 ms
82,016 KB
testcase_22 AC 578 ms
149,100 KB
testcase_23 AC 638 ms
163,332 KB
testcase_24 AC 448 ms
136,080 KB
testcase_25 AC 805 ms
203,592 KB
testcase_26 AC 669 ms
169,892 KB
testcase_27 AC 769 ms
187,108 KB
testcase_28 AC 762 ms
177,364 KB
testcase_29 AC 760 ms
187,232 KB
testcase_30 AC 427 ms
121,428 KB
testcase_31 AC 355 ms
106,848 KB
testcase_32 AC 382 ms
116,612 KB
testcase_33 AC 412 ms
115,620 KB
testcase_34 AC 690 ms
167,880 KB
testcase_35 AC 178 ms
82,488 KB
testcase_36 AC 703 ms
174,868 KB
testcase_37 AC 607 ms
152,912 KB
testcase_38 AC 272 ms
94,644 KB
testcase_39 AC 432 ms
126,084 KB
testcase_40 AC 285 ms
95,896 KB
testcase_41 AC 506 ms
126,352 KB
testcase_42 AC 70 ms
71,360 KB
testcase_43 AC 71 ms
71,232 KB
testcase_44 AC 72 ms
71,436 KB
testcase_45 AC 792 ms
205,824 KB
testcase_46 AC 803 ms
205,792 KB
testcase_47 AC 785 ms
205,892 KB
testcase_48 AC 816 ms
205,872 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