結果

問題 No.1207 グラフX
ユーザー 👑 KazunKazun
提出日時 2020-08-30 16:23:02
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 3,102 bytes
コンパイル時間 333 ms
コンパイル使用メモリ 82,532 KB
実行使用メモリ 191,716 KB
最終ジャッジ日時 2024-04-27 10:03:47
合計ジャッジ時間 29,409 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 637 ms
175,852 KB
testcase_01 AC 637 ms
176,032 KB
testcase_02 AC 628 ms
176,092 KB
testcase_03 AC 634 ms
176,128 KB
testcase_04 AC 665 ms
176,260 KB
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 AC 627 ms
133,948 KB
testcase_09 AC 628 ms
148,684 KB
testcase_10 RE -
testcase_11 RE -
testcase_12 AC 627 ms
140,204 KB
testcase_13 AC 401 ms
94,496 KB
testcase_14 AC 670 ms
174,148 KB
testcase_15 AC 638 ms
147,080 KB
testcase_16 AC 459 ms
99,156 KB
testcase_17 AC 543 ms
129,300 KB
testcase_18 AC 422 ms
131,660 KB
testcase_19 AC 545 ms
111,272 KB
testcase_20 AC 670 ms
175,940 KB
testcase_21 AC 123 ms
77,996 KB
testcase_22 AC 526 ms
135,496 KB
testcase_23 AC 546 ms
136,488 KB
testcase_24 AC 399 ms
120,900 KB
testcase_25 AC 711 ms
175,344 KB
testcase_26 AC 587 ms
150,896 KB
testcase_27 AC 662 ms
164,364 KB
testcase_28 AC 655 ms
150,204 KB
testcase_29 AC 620 ms
158,448 KB
testcase_30 AC 435 ms
109,352 KB
testcase_31 AC 364 ms
89,336 KB
testcase_32 AC 358 ms
107,460 KB
testcase_33 AC 397 ms
108,724 KB
testcase_34 AC 632 ms
144,516 KB
testcase_35 AC 173 ms
80,060 KB
testcase_36 AC 615 ms
154,860 KB
testcase_37 AC 558 ms
136,780 KB
testcase_38 AC 255 ms
89,680 KB
testcase_39 AC 379 ms
110,576 KB
testcase_40 AC 250 ms
81,512 KB
testcase_41 AC 514 ms
111,656 KB
testcase_42 AC 40 ms
55,672 KB
testcase_43 AC 38 ms
55,124 KB
testcase_44 AC 37 ms
55,720 KB
testcase_45 AC 564 ms
168,784 KB
testcase_46 AC 567 ms
168,880 KB
testcase_47 AC 584 ms
168,728 KB
testcase_48 AC 580 ms
168,836 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=[0]*(N-1)
U=Union_Find(N+1)

k=0
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[k]=(x,y,z)
        k+=1
#----------------------------
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