結果

問題 No.1207 グラフX
ユーザー WSKRWSKR
提出日時 2020-09-22 22:25:40
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
RE  
実行時間 -
コード長 2,422 bytes
コンパイル時間 467 ms
コンパイル使用メモリ 11,128 KB
実行使用メモリ 71,236 KB
最終ジャッジ日時 2023-09-09 05:04:50
合計ジャッジ時間 68,761 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,934 ms
71,032 KB
testcase_01 AC 1,913 ms
71,064 KB
testcase_02 AC 1,891 ms
71,116 KB
testcase_03 AC 1,888 ms
70,992 KB
testcase_04 AC 1,884 ms
71,236 KB
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 AC 1,400 ms
45,780 KB
testcase_09 AC 1,564 ms
56,592 KB
testcase_10 RE -
testcase_11 RE -
testcase_12 AC 1,428 ms
49,420 KB
testcase_13 AC 682 ms
18,704 KB
testcase_14 AC 1,806 ms
68,284 KB
testcase_15 AC 1,534 ms
54,540 KB
testcase_16 AC 689 ms
21,932 KB
testcase_17 AC 1,164 ms
42,676 KB
testcase_18 AC 1,060 ms
43,844 KB
testcase_19 AC 1,068 ms
30,276 KB
testcase_20 AC 1,848 ms
69,860 KB
testcase_21 AC 122 ms
8,816 KB
testcase_22 AC 1,208 ms
43,816 KB
testcase_23 AC 1,317 ms
49,588 KB
testcase_24 AC 927 ms
39,960 KB
testcase_25 AC 1,853 ms
69,192 KB
testcase_26 AC 1,446 ms
53,248 KB
testcase_27 AC 1,688 ms
62,172 KB
testcase_28 AC 1,576 ms
56,836 KB
testcase_29 AC 1,616 ms
61,584 KB
testcase_30 AC 784 ms
29,448 KB
testcase_31 AC 566 ms
14,888 KB
testcase_32 AC 704 ms
31,128 KB
testcase_33 AC 723 ms
30,200 KB
testcase_34 AC 1,524 ms
53,788 KB
testcase_35 AC 129 ms
9,380 KB
testcase_36 AC 1,528 ms
56,296 KB
testcase_37 AC 1,291 ms
45,548 KB
testcase_38 AC 332 ms
17,660 KB
testcase_39 AC 789 ms
32,904 KB
testcase_40 AC 448 ms
10,008 KB
testcase_41 AC 1,000 ms
31,252 KB
testcase_42 AC 17 ms
8,460 KB
testcase_43 AC 17 ms
8,452 KB
testcase_44 AC 17 ms
8,368 KB
testcase_45 AC 1,873 ms
64,760 KB
testcase_46 AC 1,867 ms
64,812 KB
testcase_47 AC 1,836 ms
64,724 KB
testcase_48 AC 1,839 ms
64,892 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