結果

問題 No.1207 グラフX
ユーザー NoneNone
提出日時 2021-03-19 10:27:19
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 847 ms / 2,000 ms
コード長 3,487 bytes
コンパイル時間 187 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 198,552 KB
最終ジャッジ日時 2024-04-28 22:58:37
合計ジャッジ時間 33,432 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 770 ms
194,684 KB
testcase_01 AC 751 ms
194,808 KB
testcase_02 AC 745 ms
191,216 KB
testcase_03 AC 768 ms
198,552 KB
testcase_04 AC 759 ms
194,480 KB
testcase_05 AC 822 ms
173,924 KB
testcase_06 AC 847 ms
173,024 KB
testcase_07 AC 815 ms
173,028 KB
testcase_08 AC 651 ms
147,340 KB
testcase_09 AC 707 ms
171,260 KB
testcase_10 AC 844 ms
172,616 KB
testcase_11 AC 844 ms
173,136 KB
testcase_12 AC 654 ms
158,080 KB
testcase_13 AC 374 ms
107,892 KB
testcase_14 AC 782 ms
191,308 KB
testcase_15 AC 694 ms
167,552 KB
testcase_16 AC 382 ms
107,740 KB
testcase_17 AC 555 ms
142,872 KB
testcase_18 AC 474 ms
133,256 KB
testcase_19 AC 634 ms
125,740 KB
testcase_20 AC 806 ms
195,328 KB
testcase_21 AC 125 ms
82,172 KB
testcase_22 AC 580 ms
139,604 KB
testcase_23 AC 589 ms
147,452 KB
testcase_24 AC 436 ms
126,888 KB
testcase_25 AC 780 ms
194,304 KB
testcase_26 AC 627 ms
162,080 KB
testcase_27 AC 730 ms
181,416 KB
testcase_28 AC 705 ms
173,308 KB
testcase_29 AC 699 ms
167,336 KB
testcase_30 AC 412 ms
113,556 KB
testcase_31 AC 324 ms
102,664 KB
testcase_32 AC 346 ms
113,868 KB
testcase_33 AC 385 ms
115,528 KB
testcase_34 AC 697 ms
164,464 KB
testcase_35 AC 153 ms
82,304 KB
testcase_36 AC 675 ms
166,900 KB
testcase_37 AC 590 ms
148,024 KB
testcase_38 AC 234 ms
91,520 KB
testcase_39 AC 401 ms
116,492 KB
testcase_40 AC 261 ms
96,372 KB
testcase_41 AC 481 ms
123,748 KB
testcase_42 AC 42 ms
52,864 KB
testcase_43 AC 42 ms
52,736 KB
testcase_44 AC 43 ms
52,736 KB
testcase_45 AC 755 ms
196,072 KB
testcase_46 AC 732 ms
195,696 KB
testcase_47 AC 735 ms
195,688 KB
testcase_48 AC 746 ms
195,824 KB
権限があれば一括ダウンロードができます

ソースコード

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=start
    parents[p]=-2
    next_set=[p]
    while next_set:
        p=next_set.pop()
        if p>=0:
            for q in edges[p]:
                if parents[q]!=-1:
                    continue
                parents[q]=p
                next_set.append(~q)
                next_set.append(q)
        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


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 UF.roots():
    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)%MOD*pow(X,cost,MOD)%MOD
    res%=MOD

print(res)
0