結果

問題 No.1207 グラフX
ユーザー 👑 KazunKazun
提出日時 2020-08-30 16:28:41
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,729 ms / 2,000 ms
コード長 3,126 bytes
コンパイル時間 178 ms
コンパイル使用メモリ 82,560 KB
実行使用メモリ 462,520 KB
最終ジャッジ日時 2024-04-27 10:09:26
合計ジャッジ時間 39,422 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 803 ms
183,840 KB
testcase_01 AC 788 ms
183,640 KB
testcase_02 AC 795 ms
183,912 KB
testcase_03 AC 852 ms
184,956 KB
testcase_04 AC 781 ms
184,220 KB
testcase_05 AC 1,729 ms
458,872 KB
testcase_06 AC 1,711 ms
458,896 KB
testcase_07 AC 1,625 ms
462,520 KB
testcase_08 AC 771 ms
138,812 KB
testcase_09 AC 785 ms
155,972 KB
testcase_10 AC 1,431 ms
335,824 KB
testcase_11 AC 1,618 ms
455,732 KB
testcase_12 AC 762 ms
146,272 KB
testcase_13 AC 493 ms
95,088 KB
testcase_14 AC 828 ms
183,152 KB
testcase_15 AC 785 ms
154,124 KB
testcase_16 AC 552 ms
100,356 KB
testcase_17 AC 654 ms
133,156 KB
testcase_18 AC 518 ms
135,496 KB
testcase_19 AC 678 ms
113,264 KB
testcase_20 AC 810 ms
184,616 KB
testcase_21 AC 140 ms
77,904 KB
testcase_22 AC 665 ms
139,552 KB
testcase_23 AC 675 ms
143,504 KB
testcase_24 AC 465 ms
123,832 KB
testcase_25 AC 817 ms
184,444 KB
testcase_26 AC 739 ms
156,352 KB
testcase_27 AC 834 ms
172,192 KB
testcase_28 AC 796 ms
156,580 KB
testcase_29 AC 748 ms
169,428 KB
testcase_30 AC 531 ms
111,996 KB
testcase_31 AC 433 ms
89,744 KB
testcase_32 AC 434 ms
112,184 KB
testcase_33 AC 480 ms
110,768 KB
testcase_34 AC 775 ms
149,640 KB
testcase_35 AC 190 ms
80,028 KB
testcase_36 AC 760 ms
161,908 KB
testcase_37 AC 708 ms
141,428 KB
testcase_38 AC 320 ms
90,300 KB
testcase_39 AC 465 ms
111,748 KB
testcase_40 AC 296 ms
81,464 KB
testcase_41 AC 617 ms
114,324 KB
testcase_42 AC 45 ms
55,984 KB
testcase_43 AC 44 ms
55,068 KB
testcase_44 AC 45 ms
55,816 KB
testcase_45 AC 718 ms
177,244 KB
testcase_46 AC 699 ms
177,372 KB
testcase_47 AC 698 ms
176,940 KB
testcase_48 AC 696 ms
177,284 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class Union_Find():
    def __init__(self,N):
        """0,1,...,n-1を要素として初期化する.

        n:要素数
        """
        self.n=N
        self.parents=[-1]*N
        self.rank=[0]*N

    def find(self, x):
        """要素xの属している族を調べる.

        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,yを同一視する.

        x,y:要素
        """
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.rank[x]>self.rank[y]:
            self.parents[x] += self.parents[y]
            self.parents[y] = x
        else:
            self.parents[y] += self.parents[x]
            self.parents[x] = y

            if self.rank[x]==self.rank[y]:
                self.rank[y]+=1

    def size(self, x):
        """要素xの属している要素の数.

        x:要素
        """
        return -self.parents[self.find(x)]

    def same(self, x, y):
        """要素x,yは同一視されているか?

        x,y:要素
        """
        return self.find(x) == self.find(y)

    def members(self, x):
        """要素xが属している族の要素.
        ※族の要素の個数が欲しいときはsizeを使うこと!!

        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())
#================================================
from  collections import deque
import sys

N,M,X=map(int,input().split())
sys.setrecursionlimit(2*10**5)

Mod=10**9+7

E=[[] for _ in range(N+1)]
F=[]
U=Union_Find(N+1)

for i in range(M):
    x,y,z=tuple(map(int,input().split()))

    if not U.same(x,y):
        U.union(x,y)
        E[x].append(y)
        E[y].append(x)
        F.append((x,y,z))
#----------------------------
Parent=[-1]*(N+1)
Parent[1]=0

Depth=[-1]*(N+1)
Depth[1]=0

Direct_Children=[set() for _ in range(N+1)]
Q=deque([1])
while Q:
    u=Q.popleft()
    for v in E[u]:
        if Parent[v]==-1:
            Q.append(v)
            Parent[v]=u
            Direct_Children[u].add(v)
            Depth[v]=Depth[u]+1

Children=[-1]*(N+1)
def Children_Count(x):
    if Children[x]!=-1:
        return Children[x]

    X=0
    for a in Direct_Children[x]:
        X+=Children_Count(a)
    Children[x]=X+1
    return X+1

_=Children_Count(1)

Ans=0
for (x,y,z) in F:
    if Depth[x]<Depth[y]:
        a,b=x,y
    else:
        a,b=y,x

    Ans+=Children[b]*(N-Children[b])*pow(X,z,Mod)
    Ans%=Mod
print(Ans)
0