結果

問題 No.1207 グラフX
ユーザー shotoyooshotoyoo
提出日時 2020-08-30 14:44:47
言語 PyPy3
(7.3.13)
結果
AC  
実行時間 1,598 ms / 2,000 ms
コード長 1,556 bytes
コンパイル時間 722 ms
コンパイル使用メモリ 86,912 KB
実行使用メモリ 358,308 KB
最終ジャッジ日時 2023-08-09 12:37:32
合計ジャッジ時間 37,621 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 777 ms
168,276 KB
testcase_01 AC 768 ms
168,536 KB
testcase_02 AC 766 ms
168,396 KB
testcase_03 AC 769 ms
168,684 KB
testcase_04 AC 779 ms
168,568 KB
testcase_05 AC 1,407 ms
356,464 KB
testcase_06 AC 1,598 ms
358,096 KB
testcase_07 AC 1,339 ms
358,308 KB
testcase_08 AC 700 ms
145,936 KB
testcase_09 AC 771 ms
153,688 KB
testcase_10 AC 1,385 ms
298,608 KB
testcase_11 AC 1,366 ms
358,072 KB
testcase_12 AC 730 ms
147,360 KB
testcase_13 AC 462 ms
119,932 KB
testcase_14 AC 827 ms
166,752 KB
testcase_15 AC 769 ms
152,724 KB
testcase_16 AC 468 ms
117,136 KB
testcase_17 AC 614 ms
134,916 KB
testcase_18 AC 527 ms
128,400 KB
testcase_19 AC 612 ms
135,292 KB
testcase_20 AC 822 ms
167,248 KB
testcase_21 AC 159 ms
85,004 KB
testcase_22 AC 620 ms
135,756 KB
testcase_23 AC 646 ms
141,740 KB
testcase_24 AC 470 ms
122,764 KB
testcase_25 AC 826 ms
166,788 KB
testcase_26 AC 698 ms
147,904 KB
testcase_27 AC 787 ms
158,616 KB
testcase_28 AC 763 ms
154,432 KB
testcase_29 AC 744 ms
156,220 KB
testcase_30 AC 468 ms
117,616 KB
testcase_31 AC 417 ms
115,260 KB
testcase_32 AC 395 ms
112,092 KB
testcase_33 AC 442 ms
114,460 KB
testcase_34 AC 732 ms
149,944 KB
testcase_35 AC 184 ms
85,816 KB
testcase_36 AC 716 ms
150,400 KB
testcase_37 AC 662 ms
139,804 KB
testcase_38 AC 286 ms
94,852 KB
testcase_39 AC 441 ms
115,500 KB
testcase_40 AC 337 ms
110,328 KB
testcase_41 AC 571 ms
129,188 KB
testcase_42 AC 71 ms
71,352 KB
testcase_43 AC 69 ms
71,304 KB
testcase_44 AC 72 ms
71,340 KB
testcase_45 AC 746 ms
168,216 KB
testcase_46 AC 744 ms
168,564 KB
testcase_47 AC 745 ms
168,504 KB
testcase_48 AC 745 ms
168,100 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


# inputna
n,m,X = list(map(int, input().split()))
M = 10**9+7
xyz = [list(map(int, input().split())) for _ in range(m)]
for i in range(m):
    xyz[i] = (xyz[i][0]-1, xyz[i][1]-1, xyz[i][2])
xyz.sort(key=lambda x: x[2])

class UnionFindTree:
    def __init__(self, n):
        self.n = n
        self.parent = list(range(n))
        self.size = [1] * n

    def root(self, i):
        inter = set()
        while self.parent[i]!=i:
            inter.add(i)
            i = self.parent[i]
        r = i
        for i in inter:
            self.parent[i] = r
        return r

    def connect(self, i, j):
        ri = self.root(i)
        rj = self.root(j)
        if ri==rj:
            return
        if self.size[ri]<self.size[rj]:
            self.parent[ri] = rj
            self.size[rj] += self.size[ri]
        else:
            self.parent[rj] = ri
            self.size[ri] += self.size[rj]
ans = 0
uf = UnionFindTree(n)
ss = [0] * n
es = [[] for _ in range(n)]
for x,y,z in xyz:
    rx = uf.root(x)
    ry = uf.root(y)
    if rx==ry:
        continue
    uf.connect(x,y)
    es[x].append((y,z))
    es[y].append((x,z))
ans = 0
size = [None]*n
start = 0
def sub(u, p):
    global ans
    res = 0
    for v,z in es[u]:
        if v==p:
            continue
        size = sub(v,u)
        res += size
        ans = ans + size * (n-size) * pow(X,z,M)
        ans %= M
    return res+1
sub(0,-1)
print(ans%M)
0