結果

問題 No.1207 グラフX
ユーザー rlangevinrlangevin
提出日時 2023-02-02 00:53:59
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,091 ms / 2,000 ms
コード長 1,882 bytes
コンパイル時間 273 ms
コンパイル使用メモリ 86,928 KB
実行使用メモリ 205,108 KB
最終ジャッジ日時 2023-09-14 10:29:32
合計ジャッジ時間 33,939 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 734 ms
197,184 KB
testcase_01 AC 733 ms
197,232 KB
testcase_02 AC 737 ms
196,020 KB
testcase_03 AC 745 ms
198,952 KB
testcase_04 AC 732 ms
201,544 KB
testcase_05 AC 1,091 ms
191,660 KB
testcase_06 AC 1,075 ms
193,988 KB
testcase_07 AC 1,067 ms
192,848 KB
testcase_08 AC 664 ms
160,128 KB
testcase_09 AC 669 ms
179,588 KB
testcase_10 AC 1,089 ms
185,800 KB
testcase_11 AC 1,091 ms
191,328 KB
testcase_12 AC 640 ms
162,328 KB
testcase_13 AC 427 ms
113,972 KB
testcase_14 AC 733 ms
205,108 KB
testcase_15 AC 655 ms
161,848 KB
testcase_16 AC 418 ms
112,104 KB
testcase_17 AC 546 ms
142,520 KB
testcase_18 AC 456 ms
145,632 KB
testcase_19 AC 579 ms
132,280 KB
testcase_20 AC 734 ms
200,604 KB
testcase_21 AC 139 ms
83,180 KB
testcase_22 AC 562 ms
151,116 KB
testcase_23 AC 587 ms
161,168 KB
testcase_24 AC 413 ms
142,088 KB
testcase_25 AC 731 ms
200,004 KB
testcase_26 AC 613 ms
160,260 KB
testcase_27 AC 701 ms
190,904 KB
testcase_28 AC 667 ms
167,112 KB
testcase_29 AC 657 ms
179,848 KB
testcase_30 AC 426 ms
115,960 KB
testcase_31 AC 384 ms
106,328 KB
testcase_32 AC 365 ms
122,252 KB
testcase_33 AC 398 ms
116,064 KB
testcase_34 AC 658 ms
164,696 KB
testcase_35 AC 174 ms
83,388 KB
testcase_36 AC 656 ms
170,924 KB
testcase_37 AC 592 ms
158,556 KB
testcase_38 AC 286 ms
95,664 KB
testcase_39 AC 399 ms
117,620 KB
testcase_40 AC 281 ms
97,432 KB
testcase_41 AC 552 ms
128,956 KB
testcase_42 AC 69 ms
71,360 KB
testcase_43 AC 70 ms
71,452 KB
testcase_44 AC 71 ms
71,504 KB
testcase_45 AC 684 ms
190,608 KB
testcase_46 AC 680 ms
190,828 KB
testcase_47 AC 681 ms
190,840 KB
testcase_48 AC 679 ms
190,832 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
readline = sys.stdin.readline

class UnionFind(object):
    def __init__(self, n=1):
        self.par = [i for i in range(n)]
        self.rank = [0 for _ in range(n)]
        self.size = [1 for _ in range(n)]

    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x != y:
            if self.rank[x] < self.rank[y]:
                x, y = y, x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
            self.par[y] = x
            self.size[x] += self.size[y]

    def is_same(self, x, y):
        return self.find(x) == self.find(y)

    def get_size(self, x):
        x = self.find(x)
        return self.size[x]
    
    
def non_rec_dfs(s):
    stack = []
    stack.append(~s)
    stack.append(s)
    par = [-1] * N
    sz = [1] * N
    global ans
    while stack:
        u = stack.pop()
        if u >= 0:

            stack.append(~u)
            for v in G[u]:
                if v == par[u]:
                    continue
                par[v] = u
                stack.append(v)
        else:
            u = ~u
            sz[par[u]] += sz[u]
            if par[u] != -1:
                ans += dic[u*N+par[u]] * sz[u] * (N - sz[u])
                ans %= mod


N, M, X = map(int, readline().split())
mod = 10 ** 9 + 7
U = UnionFind(N)
D = []
for i in range(M):
    x, y, z = map(int, readline().split())
    x, y = x - 1, y - 1
    D.append((z, x, y))
D.sort()

G = [[] for i in range(N)]
dic = dict()
for z, x, y in D:
    if U.is_same(x, y):
        continue
    U.union(x, y)
    G[x].append(y)
    G[y].append(x)
    v = pow(X, z, mod)
    dic[x * N + y] = v
    dic[y * N + x] = v
    
ans = 0
non_rec_dfs(0)
print(ans)
0