結果

問題 No.1207 グラフX
ユーザー naniwazunaniwazu
提出日時 2020-08-30 17:39:56
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 1,831 bytes
コンパイル時間 75 ms
コンパイル使用メモリ 12,928 KB
実行使用メモリ 119,808 KB
最終ジャッジ日時 2024-04-27 12:23:49
合計ジャッジ時間 77,565 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 TLE -
testcase_02 TLE -
testcase_03 TLE -
testcase_04 TLE -
testcase_05 TLE -
testcase_06 TLE -
testcase_07 TLE -
testcase_08 AC 1,620 ms
55,396 KB
testcase_09 AC 1,763 ms
66,408 KB
testcase_10 TLE -
testcase_11 TLE -
testcase_12 AC 1,590 ms
58,332 KB
testcase_13 AC 790 ms
22,656 KB
testcase_14 TLE -
testcase_15 AC 1,778 ms
64,128 KB
testcase_16 AC 805 ms
26,368 KB
testcase_17 AC 1,327 ms
50,588 KB
testcase_18 AC 1,210 ms
51,836 KB
testcase_19 AC 1,208 ms
36,352 KB
testcase_20 TLE -
testcase_21 AC 156 ms
11,264 KB
testcase_22 AC 1,328 ms
51,524 KB
testcase_23 AC 1,489 ms
58,388 KB
testcase_24 AC 1,051 ms
47,104 KB
testcase_25 TLE -
testcase_26 AC 1,578 ms
62,564 KB
testcase_27 AC 1,927 ms
72,916 KB
testcase_28 AC 1,816 ms
66,772 KB
testcase_29 AC 1,861 ms
72,368 KB
testcase_30 AC 892 ms
35,328 KB
testcase_31 AC 664 ms
18,432 KB
testcase_32 AC 834 ms
37,120 KB
testcase_33 AC 845 ms
36,352 KB
testcase_34 AC 1,693 ms
63,332 KB
testcase_35 AC 158 ms
12,032 KB
testcase_36 AC 1,715 ms
65,904 KB
testcase_37 AC 1,464 ms
53,884 KB
testcase_38 AC 386 ms
21,632 KB
testcase_39 AC 903 ms
38,912 KB
testcase_40 AC 521 ms
12,672 KB
testcase_41 AC 1,115 ms
37,120 KB
testcase_42 AC 27 ms
11,136 KB
testcase_43 AC 26 ms
11,136 KB
testcase_44 AC 26 ms
11,136 KB
testcase_45 TLE -
testcase_46 TLE -
testcase_47 TLE -
testcase_48 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

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

def dfs_size(x):
    sizex = 1
    for y in Tree[x]:
        if y != parent[x]:
            parent[y] = x
            sizex += dfs_size(y)
    size[x] = sizex
    return sizex

import sys
sys.setrecursionlimit(1<<30)
mod = 10**9+7
N,M,X = map(int,input().split())
Tree = [[] for _ in range(N)]
edges = []
uf = UnionFind(N)
for i in range(M):
    x,y,z = map(int,input().split())
    if not uf.same(x-1,y-1):
        uf.union(x-1,y-1)
        Tree[x-1].append(y-1)
        Tree[y-1].append(x-1)
        edges.append((x,y,z))
size = [-1]*N
parent = [-1]*N
dfs_size(0)
ans = 0
for x,y,z in edges:
    s = min(size[x-1],size[y-1])
    ans += s*(N-s)*pow(X,z,mod)
    ans %= mod
print(ans)
0