結果

問題 No.1207 グラフX
ユーザー naniwazunaniwazu
提出日時 2020-08-30 17:56:17
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,093 bytes
コンパイル時間 128 ms
コンパイル使用メモリ 81,748 KB
実行使用メモリ 155,224 KB
最終ジャッジ日時 2024-04-27 12:45:20
合計ジャッジ時間 25,734 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 789 ms
136,472 KB
testcase_06 AC 793 ms
136,548 KB
testcase_07 AC 792 ms
135,844 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 827 ms
136,876 KB
testcase_11 AC 781 ms
136,288 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 35 ms
52,608 KB
testcase_43 AC 35 ms
52,480 KB
testcase_44 AC 34 ms
52,736 KB
testcase_45 AC 505 ms
149,668 KB
testcase_46 AC 505 ms
149,492 KB
testcase_47 AC 504 ms
149,640 KB
testcase_48 AC 502 ms
149,732 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)
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
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