結果

問題 No.1207 グラフX
ユーザー WSKRWSKR
提出日時 2020-09-22 22:36:31
言語 PyPy3
(7.3.15)
結果
MLE  
実行時間 -
コード長 2,115 bytes
コンパイル時間 1,572 ms
コンパイル使用メモリ 86,848 KB
実行使用メモリ 821,612 KB
最終ジャッジ日時 2023-09-09 05:19:26
合計ジャッジ時間 16,947 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 672 ms
144,368 KB
testcase_01 AC 685 ms
144,424 KB
testcase_02 AC 687 ms
144,532 KB
testcase_03 AC 689 ms
144,212 KB
testcase_04 AC 676 ms
144,404 KB
testcase_05 AC 1,329 ms
314,368 KB
testcase_06 AC 1,315 ms
318,112 KB
testcase_07 AC 1,331 ms
318,844 KB
testcase_08 AC 645 ms
118,528 KB
testcase_09 AC 699 ms
129,108 KB
testcase_10 MLE -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * (n+1)

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

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)



mod = 10 ** 9 + 7
from collections import deque

def main():
    N, M, X = map(int, input().split())
    Uni = UnionFind(N)
    res = 0
    que = []
    G = [[] for i in range(N+1)]
    for i in range(M):
        x, y, z = map(int, input().split())
        if Uni.same(x, y) == True:
            continue
        else:
            Uni.union(x, y)
            G[x].append(y)
            G[y].append(x)
            que.append((x, y, z))

    #ここで得られる構造は木構造
    visited = [False for i in range(N+1)]
    depth = [0 for i in range(N+1)]
    child_cnt = [1 for i in range(N+1)]

    stack = deque([])
    def dfs(s_node):
        visited[s_node] = True
        for node in G[s_node]:
            if visited[node] == False:
                depth[node] = depth[s_node] + 1
                dfs(node)
        stack.append(s_node)  


    dfs(1)
    while stack:
        s_node = stack.popleft()
        for node in G[s_node]:
            if depth[node] > depth[s_node]:
                child_cnt[s_node] += child_cnt[node]    
    for x, y, z in que:
        if depth[x] > depth[y]:
            res += pow(X, z, mod) * child_cnt[x]*(N-child_cnt[x])
            res %= mod
        else:
            res += pow(X, z, mod) * child_cnt[y]*(N-child_cnt[y])
            res %= mod
    print(res)
    return


if __name__ == "__main__":
    main()





0