結果

問題 No.1207 グラフX
ユーザー roarisroaris
提出日時 2020-12-02 06:48:28
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,354 ms / 2,000 ms
コード長 1,970 bytes
コンパイル時間 741 ms
コンパイル使用メモリ 87,084 KB
実行使用メモリ 163,032 KB
最終ジャッジ日時 2023-10-11 04:29:50
合計ジャッジ時間 45,697 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,354 ms
155,588 KB
testcase_01 AC 1,089 ms
157,248 KB
testcase_02 AC 1,089 ms
155,852 KB
testcase_03 AC 1,108 ms
163,032 KB
testcase_04 AC 1,078 ms
156,960 KB
testcase_05 AC 761 ms
145,360 KB
testcase_06 AC 766 ms
144,896 KB
testcase_07 AC 769 ms
145,544 KB
testcase_08 AC 830 ms
124,924 KB
testcase_09 AC 970 ms
143,728 KB
testcase_10 AC 827 ms
145,192 KB
testcase_11 AC 763 ms
145,280 KB
testcase_12 AC 858 ms
130,136 KB
testcase_13 AC 390 ms
89,300 KB
testcase_14 AC 1,096 ms
162,596 KB
testcase_15 AC 918 ms
138,900 KB
testcase_16 AC 429 ms
93,260 KB
testcase_17 AC 722 ms
120,136 KB
testcase_18 AC 675 ms
124,088 KB
testcase_19 AC 608 ms
104,388 KB
testcase_20 AC 1,116 ms
162,068 KB
testcase_21 AC 132 ms
77,760 KB
testcase_22 AC 751 ms
125,432 KB
testcase_23 AC 822 ms
133,884 KB
testcase_24 AC 608 ms
118,288 KB
testcase_25 AC 1,122 ms
161,652 KB
testcase_26 AC 861 ms
136,584 KB
testcase_27 AC 1,021 ms
151,408 KB
testcase_28 AC 951 ms
140,640 KB
testcase_29 AC 999 ms
152,156 KB
testcase_30 AC 518 ms
103,100 KB
testcase_31 AC 336 ms
86,188 KB
testcase_32 AC 486 ms
105,616 KB
testcase_33 AC 489 ms
105,344 KB
testcase_34 AC 904 ms
138,304 KB
testcase_35 AC 167 ms
78,696 KB
testcase_36 AC 909 ms
141,676 KB
testcase_37 AC 788 ms
125,368 KB
testcase_38 AC 301 ms
88,260 KB
testcase_39 AC 514 ms
106,868 KB
testcase_40 AC 242 ms
80,524 KB
testcase_41 AC 572 ms
104,636 KB
testcase_42 AC 85 ms
71,320 KB
testcase_43 AC 73 ms
71,544 KB
testcase_44 AC 73 ms
71,180 KB
testcase_45 AC 1,024 ms
154,908 KB
testcase_46 AC 1,019 ms
154,752 KB
testcase_47 AC 1,020 ms
154,760 KB
testcase_48 AC 1,013 ms
154,948 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

class Unionfind:
    def __init__(self, n):
        self.par = [-1]*n
        self.rank = [1]*n
    
    def root(self, x):
        r = x
        
        while not self.par[r]<0:
            r = self.par[r]
        
        t = x
        
        while t!=r:
            tmp = t
            t = self.par[t]
            self.par[tmp] = r
        
        return r
    
    def unite(self, x, y):
        rx = self.root(x)
        ry = self.root(y)
        
        if rx==ry:
            return
        
        if self.rank[rx]<=self.rank[ry]:
            self.par[ry] += self.par[rx]
            self.par[rx] = ry
            
            if self.rank[rx]==self.rank[ry]:
                self.rank[ry] += 1
        else:
            self.par[rx] += self.par[ry]
            self.par[ry] = rx
    
    def is_same(self, x, y):
        return self.root(x)==self.root(y)
    
    def count(self, x):
        return -self.par[self.root(x)]

def dfs():
    stack = [(0, -1)]
    in_order = [-1]*N
    in_order_now = 0
    par = [-1]*N
    size = [1]*N
    
    while stack:
        v, pv = stack.pop()
        in_order[v] = in_order_now
        in_order_now += 1
        
        for nv in G[v]:
            if nv==pv:
                continue
            
            par[nv] = v
            stack.append((nv, v))
    
    vs = [(in_order[v], v) for v in range(1, N)]
    vs.sort(reverse=True)
    
    for _, v in vs:
        size[par[v]] += size[v]
    
    return size
    
N, M, X = map(int, input().split())
uf = Unionfind(N)
G = [[] for _ in range(N)]
edges = []

for _ in range(M):
    x, y, z = map(int, input().split())
    
    if not uf.is_same(x-1, y-1):
        uf.unite(x-1, y-1)
        G[x-1].append(y-1)
        G[y-1].append(x-1)
        edges.append((x-1, y-1, z))

size = dfs()
ans = 0
MOD = 10**9+7

for x, y, z in edges:
    m = min(size[x], size[y])
    ans += m*(N-m)*pow(X, z, MOD)
    ans %= MOD

print(ans)
0