結果

問題 No.1207 グラフX
ユーザー AEnAEn
提出日時 2022-12-10 15:48:06
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 2,911 bytes
コンパイル時間 1,925 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 458,368 KB
最終ジャッジ日時 2024-04-22 23:55:24
合計ジャッジ時間 22,026 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 767 ms
128,956 KB
testcase_01 AC 764 ms
120,960 KB
testcase_02 AC 754 ms
121,728 KB
testcase_03 AC 757 ms
121,128 KB
testcase_04 AC 758 ms
121,344 KB
testcase_05 TLE -
testcase_06 TLE -
testcase_07 TLE -
testcase_08 AC 740 ms
104,448 KB
testcase_09 AC 753 ms
112,256 KB
testcase_10 TLE -
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 typing import List
import sys
sys.setrecursionlimit(10**8)

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