結果

問題 No.1207 グラフX
ユーザー WSKRWSKR
提出日時 2020-09-22 22:38:42
言語 PyPy3
(7.3.15)
結果
MLE  
実行時間 -
コード長 2,133 bytes
コンパイル時間 1,358 ms
コンパイル使用メモリ 86,860 KB
実行使用メモリ 846,216 KB
最終ジャッジ日時 2023-09-09 05:21:52
合計ジャッジ時間 15,887 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 672 ms
158,320 KB
testcase_01 AC 658 ms
158,580 KB
testcase_02 AC 662 ms
158,388 KB
testcase_03 AC 669 ms
158,224 KB
testcase_04 AC 668 ms
159,908 KB
testcase_05 AC 1,348 ms
328,060 KB
testcase_06 AC 1,329 ms
329,512 KB
testcase_07 AC 1,305 ms
329,172 KB
testcase_08 AC 640 ms
135,744 KB
testcase_09 AC 695 ms
142,220 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 #

from collections import deque
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


def main():
    N, M, X = map(int, input().split())
    Uni = UnionFind(N)
    res = 0
    que = []
    G = [[] for i in range(N+1)]
    tmp_in = [tuple(map(int, input().split())) for i in range(M)]
    for x, y, z in tmp_in:
        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