結果

問題 No.1207 グラフX
ユーザー hir355hir355
提出日時 2020-08-30 14:53:12
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,149 ms / 2,000 ms
コード長 1,481 bytes
コンパイル時間 167 ms
コンパイル使用メモリ 82,160 KB
実行使用メモリ 138,584 KB
最終ジャッジ日時 2024-04-27 07:46:14
合計ジャッジ時間 33,371 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 765 ms
134,552 KB
testcase_01 AC 762 ms
134,248 KB
testcase_02 AC 757 ms
133,092 KB
testcase_03 AC 759 ms
137,484 KB
testcase_04 AC 760 ms
134,724 KB
testcase_05 AC 1,132 ms
132,448 KB
testcase_06 AC 1,105 ms
131,400 KB
testcase_07 AC 1,144 ms
132,220 KB
testcase_08 AC 656 ms
107,408 KB
testcase_09 AC 711 ms
119,152 KB
testcase_10 AC 1,149 ms
125,772 KB
testcase_11 AC 1,135 ms
132,600 KB
testcase_12 AC 678 ms
112,060 KB
testcase_13 AC 406 ms
84,224 KB
testcase_14 AC 784 ms
135,368 KB
testcase_15 AC 709 ms
120,208 KB
testcase_16 AC 408 ms
86,528 KB
testcase_17 AC 569 ms
104,928 KB
testcase_18 AC 478 ms
106,880 KB
testcase_19 AC 564 ms
93,480 KB
testcase_20 AC 770 ms
133,040 KB
testcase_21 AC 117 ms
76,868 KB
testcase_22 AC 576 ms
106,588 KB
testcase_23 AC 605 ms
111,900 KB
testcase_24 AC 440 ms
104,424 KB
testcase_25 AC 786 ms
137,016 KB
testcase_26 AC 650 ms
118,832 KB
testcase_27 AC 733 ms
124,200 KB
testcase_28 AC 708 ms
122,888 KB
testcase_29 AC 703 ms
128,952 KB
testcase_30 AC 423 ms
92,880 KB
testcase_31 AC 353 ms
82,160 KB
testcase_32 AC 384 ms
96,584 KB
testcase_33 AC 409 ms
94,500 KB
testcase_34 AC 699 ms
117,232 KB
testcase_35 AC 163 ms
77,900 KB
testcase_36 AC 680 ms
116,428 KB
testcase_37 AC 623 ms
110,312 KB
testcase_38 AC 265 ms
83,276 KB
testcase_39 AC 412 ms
96,568 KB
testcase_40 AC 246 ms
78,212 KB
testcase_41 AC 547 ms
94,156 KB
testcase_42 AC 38 ms
52,328 KB
testcase_43 AC 38 ms
53,272 KB
testcase_44 AC 38 ms
53,124 KB
testcase_45 AC 713 ms
138,448 KB
testcase_46 AC 717 ms
138,584 KB
testcase_47 AC 712 ms
138,456 KB
testcase_48 AC 718 ms
138,468 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

MOD = 10 ** 9 + 7


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

    # 検索
    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 unite(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return
        if self.rank[x] < self.rank[y]:
            self.par[x] = y
        else:
            self.par[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1

    # 同じ集合に属するか判定
    def same_check(self, x, y):
        return self.find(x) == self.find(y)


n, m, x = map(int, input().split())
uf = UnionFind(n)
g = [[] for _ in range(n)]
ans = 0
for _ in range(m):
    a, b, c = map(int, input().split())
    if not uf.same_check(a - 1, b - 1):
        uf.unite(a - 1, b - 1)
        g[a - 1].append((b - 1, pow(x, c, MOD)))
        g[b - 1].append((a - 1, pow(x, c, MOD)))
d = [-2] * n
c = [1] * n
s = [0]
ans = 0
while s:
    p = s.pop()
    if d[p] == -2:
        s.append(p)
        d[p] = -1
        for node, cost in g[p]:
            if d[node] == -2:
                s.append(node)
    else:
        d[p] = 0
        for node, cost in g[p]:
            if d[node] >= 0:
                c[p] += c[node]
                ans += (n - c[node]) * c[node] * cost
print(ans % MOD)
0