結果

問題 No.1207 グラフX
ユーザー 👑 KazunKazun
提出日時 2020-08-30 16:26:36
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 3,083 bytes
コンパイル時間 189 ms
コンパイル使用メモリ 82,160 KB
実行使用メモリ 200,952 KB
最終ジャッジ日時 2024-04-27 10:07:31
合計ジャッジ時間 35,565 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 788 ms
183,284 KB
testcase_01 AC 786 ms
183,704 KB
testcase_02 AC 781 ms
184,304 KB
testcase_03 AC 782 ms
184,972 KB
testcase_04 AC 768 ms
183,776 KB
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 AC 807 ms
138,432 KB
testcase_09 AC 773 ms
155,944 KB
testcase_10 RE -
testcase_11 RE -
testcase_12 AC 759 ms
146,712 KB
testcase_13 AC 493 ms
94,780 KB
testcase_14 AC 830 ms
183,120 KB
testcase_15 AC 798 ms
154,288 KB
testcase_16 AC 571 ms
100,232 KB
testcase_17 AC 660 ms
132,900 KB
testcase_18 AC 524 ms
134,944 KB
testcase_19 AC 714 ms
112,660 KB
testcase_20 AC 815 ms
184,816 KB
testcase_21 AC 141 ms
78,116 KB
testcase_22 AC 661 ms
139,068 KB
testcase_23 AC 669 ms
143,468 KB
testcase_24 AC 471 ms
123,760 KB
testcase_25 AC 809 ms
184,180 KB
testcase_26 AC 727 ms
156,080 KB
testcase_27 AC 790 ms
171,752 KB
testcase_28 AC 790 ms
156,756 KB
testcase_29 AC 755 ms
169,668 KB
testcase_30 AC 539 ms
112,032 KB
testcase_31 AC 438 ms
89,688 KB
testcase_32 AC 443 ms
112,028 KB
testcase_33 AC 484 ms
110,660 KB
testcase_34 AC 796 ms
149,244 KB
testcase_35 AC 194 ms
79,756 KB
testcase_36 AC 755 ms
161,480 KB
testcase_37 AC 696 ms
141,084 KB
testcase_38 AC 322 ms
90,552 KB
testcase_39 AC 462 ms
112,020 KB
testcase_40 AC 296 ms
81,352 KB
testcase_41 AC 649 ms
114,384 KB
testcase_42 AC 45 ms
55,248 KB
testcase_43 AC 45 ms
55,392 KB
testcase_44 AC 45 ms
55,324 KB
testcase_45 AC 725 ms
177,600 KB
testcase_46 AC 719 ms
177,304 KB
testcase_47 AC 700 ms
177,272 KB
testcase_48 AC 699 ms
177,324 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

N,M,X=map(int,input().split())
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