結果

問題 No.1207 グラフX
ユーザー WSKRWSKR
提出日時 2020-09-22 22:26:41
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 2,422 bytes
コンパイル時間 520 ms
コンパイル使用メモリ 86,592 KB
実行使用メモリ 148,476 KB
最終ジャッジ日時 2023-09-09 05:05:43
合計ジャッジ時間 32,661 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 726 ms
144,292 KB
testcase_01 AC 695 ms
143,644 KB
testcase_02 AC 675 ms
144,188 KB
testcase_03 AC 672 ms
144,108 KB
testcase_04 AC 683 ms
143,744 KB
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 AC 666 ms
119,004 KB
testcase_09 AC 721 ms
131,276 KB
testcase_10 RE -
testcase_11 RE -
testcase_12 AC 652 ms
124,000 KB
testcase_13 AC 435 ms
90,356 KB
testcase_14 AC 717 ms
143,948 KB
testcase_15 AC 698 ms
129,188 KB
testcase_16 AC 437 ms
93,712 KB
testcase_17 AC 565 ms
114,500 KB
testcase_18 AC 455 ms
113,496 KB
testcase_19 AC 554 ms
102,424 KB
testcase_20 AC 720 ms
145,140 KB
testcase_21 AC 159 ms
79,224 KB
testcase_22 AC 561 ms
115,816 KB
testcase_23 AC 579 ms
123,800 KB
testcase_24 AC 431 ms
110,232 KB
testcase_25 AC 713 ms
144,416 KB
testcase_26 AC 609 ms
125,700 KB
testcase_27 AC 692 ms
135,304 KB
testcase_28 AC 668 ms
131,876 KB
testcase_29 AC 655 ms
135,416 KB
testcase_30 AC 445 ms
100,580 KB
testcase_31 AC 392 ms
87,604 KB
testcase_32 AC 385 ms
102,444 KB
testcase_33 AC 423 ms
100,888 KB
testcase_34 AC 679 ms
126,208 KB
testcase_35 AC 208 ms
80,432 KB
testcase_36 AC 637 ms
129,032 KB
testcase_37 AC 596 ms
118,304 KB
testcase_38 AC 310 ms
89,040 KB
testcase_39 AC 415 ms
103,148 KB
testcase_40 AC 289 ms
82,808 KB
testcase_41 AC 531 ms
102,964 KB
testcase_42 AC 72 ms
71,292 KB
testcase_43 AC 72 ms
71,476 KB
testcase_44 AC 73 ms
71,492 KB
testcase_45 AC 632 ms
143,948 KB
testcase_46 AC 659 ms
143,948 KB
testcase_47 AC 629 ms
144,004 KB
testcase_48 AC 663 ms
143,960 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n + 1) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())


mod = 10 ** 9 + 7


def main():
    N, M, X = map(int, input().split())
    weight_cost = [0 for i in range(N+1)]
    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)]

    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)
        for node in G[s_node]:
            if depth[node] > depth[s_node]:
                child_cnt[s_node] += child_cnt[node]

    dfs(1)
    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