結果

問題 No.1207 グラフX
ユーザー 👑 KazunKazun
提出日時 2020-08-30 16:28:41
言語 PyPy3
(7.3.13)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 3,126 bytes
コンパイル時間 451 ms
コンパイル使用メモリ 87,048 KB
実行使用メモリ 475,072 KB
最終ジャッジ日時 2023-08-09 15:18:01
合計ジャッジ時間 43,573 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 777 ms
177,740 KB
testcase_01 AC 782 ms
178,008 KB
testcase_02 AC 772 ms
175,356 KB
testcase_03 AC 772 ms
176,584 KB
testcase_04 AC 754 ms
175,716 KB
testcase_05 AC 1,794 ms
471,920 KB
testcase_06 TLE -
testcase_07 AC 1,624 ms
470,716 KB
testcase_08 AC 809 ms
140,588 KB
testcase_09 AC 809 ms
156,412 KB
testcase_10 AC 1,683 ms
355,440 KB
testcase_11 AC 1,631 ms
475,072 KB
testcase_12 AC 800 ms
147,748 KB
testcase_13 AC 537 ms
97,140 KB
testcase_14 AC 841 ms
185,196 KB
testcase_15 AC 802 ms
154,952 KB
testcase_16 AC 603 ms
102,260 KB
testcase_17 AC 681 ms
135,696 KB
testcase_18 AC 544 ms
136,776 KB
testcase_19 AC 687 ms
114,984 KB
testcase_20 AC 825 ms
186,200 KB
testcase_21 AC 179 ms
79,032 KB
testcase_22 AC 680 ms
141,276 KB
testcase_23 AC 695 ms
149,068 KB
testcase_24 AC 498 ms
129,192 KB
testcase_25 AC 825 ms
185,372 KB
testcase_26 AC 747 ms
157,948 KB
testcase_27 AC 830 ms
164,884 KB
testcase_28 AC 809 ms
158,016 KB
testcase_29 AC 776 ms
164,784 KB
testcase_30 AC 570 ms
112,788 KB
testcase_31 AC 474 ms
91,792 KB
testcase_32 AC 480 ms
110,796 KB
testcase_33 AC 524 ms
111,512 KB
testcase_34 AC 833 ms
151,720 KB
testcase_35 AC 235 ms
80,648 KB
testcase_36 AC 842 ms
156,548 KB
testcase_37 AC 760 ms
143,812 KB
testcase_38 AC 362 ms
91,356 KB
testcase_39 AC 509 ms
116,540 KB
testcase_40 AC 317 ms
82,988 KB
testcase_41 AC 657 ms
116,008 KB
testcase_42 AC 93 ms
71,700 KB
testcase_43 AC 91 ms
71,700 KB
testcase_44 AC 90 ms
71,488 KB
testcase_45 AC 731 ms
182,240 KB
testcase_46 AC 728 ms
182,556 KB
testcase_47 AC 770 ms
182,332 KB
testcase_48 AC 724 ms
182,524 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