結果

問題 No.1207 グラフX
ユーザー NoneNone
提出日時 2021-03-19 09:58:23
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 3,633 bytes
コンパイル時間 197 ms
コンパイル使用メモリ 82,708 KB
実行使用メモリ 181,036 KB
最終ジャッジ日時 2024-04-28 22:32:20
合計ジャッジ時間 8,245 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
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 #

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

    def find(self, x):
        """ 根を見つける関数を定義(同時にxを直接根にくっつける操作も行う)"""
        tmp = []
        parents = self.parents
        while parents[x] >= 0:
            tmp.append(x)
            x = parents[x]
        for y in tmp:
            parents[y] = x
        return x

    def union(self, x, y):
        """ 二つの木をくっつける(子を多く持つ方を根とした親子関係)。これは破壊的操作を行う。"""
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return False
        if self.parents[x] > self.parents[y]:
            x, y = y, x
        self.parents[x] += self.parents[y]
        self.parents[y] = x
        return True

    def same(self, x, y):
        """ xとyが同じ根の子かを判定 """
        return self.find(x) == self.find(y)

    def size(self, x):
        """ xの根のparent(= 要素数)を返す """
        return -self.parents[self.find(x)]

    def members(self, x):
        """ xが属するグループの要素をリストとして返す O(N)"""
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        """ 全ての根の要素をリストとして返す O(N)"""
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        """ グループの数を返す O(N)"""
        return len(self.roots())

    def size_list(self):
        """ 各グループの要素数のリストを返す(根の番号は返さない) O(N)"""
        return [-x for x in self.parents if x < 0]

    def all_group_members(self):
        """ {根:[根の子(根を含む)のリスト],...}を辞書で返す O(N)"""
        res = [[] for _ in range(self.n)]
        for i in range(self.n):
            x = self.find(i)
            res[x].append(i)
        return {r: res[r] for r in self.roots()}

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


def dfs(start=0,goal=None):
    p,t=start,0
    parents[p]=-2
    next_set=[(p,t)]
    if p==goal:
        return t
    while next_set:
        p,t=next_set.pop()
        if p>=0:
            for q in edges[p]:
                if q in parents:
                    continue
                if q==goal:
                    return t+1
                parents[q]=p
                next_set.append((~q,t+1))
                next_set.append((q,t+1))
        else: # 帰りがけ処理
            p=~p
            par=parents[p]
            size[par]+=size[p]
    return -1

def example():
    global input
    example = iter(
        """
5 5 5
1 4 3
2 4 4
3 5 7
2 3 8
2 3 10

        """
            .strip().split("\n"))
    input = lambda: next(example)




#####( main )####################################################################
import sys
input=sys.stdin.readline


# example()




N,M,X=map(int,input().split())

data=[]
for _ in range(M):
    p,q,cost=map(int,input().split())
    p,q = p-1,q-1
    data.append((p,q,cost))


size=[1]*N
parents=[-1]*N

UF=UnionFind(N)
edges=[[] for _ in range(N)]
used=[0]*M

data2=[]
for p,q,dist in data:
    if UF.union(p,q):
        edges[p].append(q)
        edges[q].append(p)
        data2.append((p,q,dist))


for i in range(N):
    if parents[i]==-1:
        dfs(start=i)

MOD=10**9+7

res=0
for p,q,cost in data2:
    if parents[q]!=p:
        q,p=p,q
    n=UF.size(p)
    m=size[q]
    res+=m*(n-m)*pow(X,cost,MOD)
    res%=MOD

print(res)
0