結果

問題 No.1207 グラフX
ユーザー naniwazunaniwazu
提出日時 2020-08-30 18:09:23
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,192 bytes
コンパイル時間 156 ms
コンパイル使用メモリ 82,612 KB
実行使用メモリ 179,200 KB
最終ジャッジ日時 2024-04-27 12:50:30
合計ジャッジ時間 30,915 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 925 ms
160,148 KB
testcase_06 AC 930 ms
159,768 KB
testcase_07 AC 918 ms
159,896 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 919 ms
159,620 KB
testcase_11 AC 902 ms
160,388 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 AC 39 ms
52,812 KB
testcase_43 AC 39 ms
53,616 KB
testcase_44 AC 39 ms
53,708 KB
testcase_45 AC 609 ms
179,200 KB
testcase_46 AC 605 ms
178,824 KB
testcase_47 AC 599 ms
178,828 KB
testcase_48 AC 616 ms
179,072 KB
権限があれば一括ダウンロードができます

ソースコード

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

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)
xyz = []
for i in range(M):
    x,y,z = map(int,input().split())
    xyz.append((x,y,z))
xyz.sort(key=lambda x:x[2])
for i in range(M):
    x,y,z = xyz[i]
    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
d = []
d.append(0)
while d:
    x = d.pop()
    if x == 0:
        if size[x] == 1:
            for y in Tree[x]:
                if parent[x] != y:
                    d.append(y)
                    parent[y] = x
    elif size[x] > 1 or len(Tree[x]) == 1:
        p = parent[x]
        size[p] += size[x]
        d.append(p)
    else:
        for y in Tree[x]:
            if parent[x] != y:
                d.append(y)
                parent[y] = x
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