結果

問題 No.1207 グラフX
ユーザー AEnAEn
提出日時 2022-12-10 15:47:37
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 2,871 bytes
コンパイル時間 1,123 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 80,652 KB
最終ジャッジ日時 2024-04-22 23:56:59
合計ジャッジ時間 11,558 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 TLE -
testcase_02 TLE -
testcase_03 TLE -
testcase_04 TLE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 TLE -
testcase_09 TLE -
testcase_10 RE -
testcase_11 RE -
testcase_12 TLE -
testcase_13 AC 1,029 ms
22,784 KB
testcase_14 TLE -
testcase_15 TLE -
testcase_16 AC 1,040 ms
25,936 KB
testcase_17 AC 1,748 ms
49,548 KB
testcase_18 AC 1,554 ms
50,664 KB
testcase_19 AC 1,584 ms
35,584 KB
testcase_20 TLE -
testcase_21 AC 192 ms
11,904 KB
testcase_22 AC 1,734 ms
50,076 KB
testcase_23 AC 1,924 ms
56,768 KB
testcase_24 AC 1,366 ms
46,200 KB
testcase_25 TLE -
testcase_26 TLE -
testcase_27 TLE -
testcase_28 TLE -
testcase_29 TLE -
testcase_30 AC 1,152 ms
34,688 KB
testcase_31 AC 846 ms
18,816 KB
testcase_32 AC 1,050 ms
36,684 KB
testcase_33 AC 1,069 ms
35,584 KB
testcase_34 TLE -
testcase_35 AC 199 ms
12,800 KB
testcase_36 TLE -
testcase_37 AC 1,900 ms
52,640 KB
testcase_38 AC 515 ms
21,632 KB
testcase_39 AC 1,140 ms
38,240 KB
testcase_40 AC 655 ms
13,312 KB
testcase_41 AC 1,478 ms
36,608 KB
testcase_42 AC 42 ms
11,520 KB
testcase_43 AC 41 ms
11,648 KB
testcase_44 AC 41 ms
11,648 KB
testcase_45 TLE -
testcase_46 TLE -
testcase_47 TLE -
testcase_48 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

from typing import List

class UnionFind:
    """0-indexed"""
    def __init__(self, n):
        self.n = n
        self.parent = [-1] * n
        self.__group_count = n

    def unite(self, x, y) -> bool:
        """xとyをマージ"""
        x = self.root(x)
        y = self.root(y)
        if x == y:
            return False

        self.__group_count -= 1

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

        self.parent[x] += self.parent[y]
        self.parent[y] = x
        return True

    def is_same(self, x, y) -> bool:
        """xとyが同じ連結成分か判定"""
        return self.root(x) == self.root(y)

    def root(self, x) -> int:
        """xの根を取得"""
        if self.parent[x] < 0:
            return x
        else:
            # 経路圧縮あり
            # self.parent[x] = self.root(self.parent[x])
            # return self.parent[x]
            # 経路圧縮なし
            return self.root(self.parent[x])

    def size(self, x) -> int:
        """xが属する連結成分のサイズを取得"""
        return -self.parent[self.root(x)]

    def all_sizes(self) -> List[int]:
        """全連結成分のサイズのリストを取得 O(N)"""
        sizes = []
        for i in range(self.n):
            size = self.parent[i]
            if size < 0:
                sizes.append(-size)
        return sizes
    
    def members(self, x) -> List[int]:
        """xが属するグループのリストを返す O(N)"""
        mem = []
        r = self.root(x)
        for i in range(self.n):
            if self.root(i) == r:
                mem.append(i)
        return mem

    def groups(self) -> List[List[int]]:
        """全連結成分の内容のリストを取得 O(N・α(N))"""
        groups = dict()
        for i in range(self.n):
            p = self.root(i)
            if not groups.get(p):
                groups[p] = []
            groups[p].append(i)
        return list(groups.values())

    @property
    def group_count(self) -> int:
        """連結成分の数を取得 O(1)"""
        return self.__group_count

N, M, X = map(int, input().split())
uf = UnionFind(N)
G = [list() for _ in range(N)]
for i in range(M):
    x, y, z = map(int, input().split())
    x-=1;y-=1
    if uf.is_same(x,y):continue
    uf.unite(x,y)
    G[x].append((y,z))
    G[y].append((x,z))

sub_tree = [0]*N
def dfs(v, p):
    for next_v,_ in G[v]:
        if next_v == p:
            continue
        dfs(next_v, v)
    sub_tree[v] = 1
    for c,_ in G[v]:
        if c== p:
            continue
        sub_tree[v] += sub_tree[c]

res = 0
mod = 10**9+7
def dfs2(v, p):
    for nex, cost in G[v]:
        if nex==p:continue
        global res
        res += pow(X,cost,mod)*(sub_tree[nex]*(N-sub_tree[nex]))
        res %= mod
        dfs2(nex,v)
dfs(0,-1)
dfs2(0,-1)
print(res)
0