結果

問題 No.1207 グラフX
ユーザー shotoyooshotoyoo
提出日時 2020-08-30 14:44:47
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,462 ms / 2,000 ms
コード長 1,556 bytes
コンパイル時間 168 ms
コンパイル使用メモリ 82,412 KB
実行使用メモリ 348,732 KB
最終ジャッジ日時 2024-04-27 07:40:32
合計ジャッジ時間 36,138 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 785 ms
166,568 KB
testcase_01 AC 785 ms
166,752 KB
testcase_02 AC 813 ms
166,600 KB
testcase_03 AC 771 ms
166,828 KB
testcase_04 AC 787 ms
166,356 KB
testcase_05 AC 1,375 ms
346,740 KB
testcase_06 AC 1,371 ms
347,564 KB
testcase_07 AC 1,366 ms
348,704 KB
testcase_08 AC 696 ms
144,980 KB
testcase_09 AC 741 ms
151,964 KB
testcase_10 AC 1,462 ms
317,636 KB
testcase_11 AC 1,375 ms
348,732 KB
testcase_12 AC 698 ms
145,948 KB
testcase_13 AC 434 ms
117,424 KB
testcase_14 AC 830 ms
165,648 KB
testcase_15 AC 765 ms
154,796 KB
testcase_16 AC 454 ms
115,672 KB
testcase_17 AC 714 ms
133,632 KB
testcase_18 AC 513 ms
126,724 KB
testcase_19 AC 601 ms
132,656 KB
testcase_20 AC 814 ms
165,788 KB
testcase_21 AC 128 ms
83,992 KB
testcase_22 AC 606 ms
134,368 KB
testcase_23 AC 626 ms
141,168 KB
testcase_24 AC 458 ms
121,180 KB
testcase_25 AC 829 ms
165,232 KB
testcase_26 AC 668 ms
145,680 KB
testcase_27 AC 775 ms
158,140 KB
testcase_28 AC 765 ms
152,900 KB
testcase_29 AC 738 ms
154,920 KB
testcase_30 AC 443 ms
115,208 KB
testcase_31 AC 399 ms
113,632 KB
testcase_32 AC 387 ms
111,132 KB
testcase_33 AC 422 ms
112,212 KB
testcase_34 AC 738 ms
148,616 KB
testcase_35 AC 165 ms
85,152 KB
testcase_36 AC 704 ms
149,888 KB
testcase_37 AC 656 ms
138,716 KB
testcase_38 AC 268 ms
93,572 KB
testcase_39 AC 433 ms
113,408 KB
testcase_40 AC 305 ms
108,320 KB
testcase_41 AC 559 ms
127,860 KB
testcase_42 AC 40 ms
52,616 KB
testcase_43 AC 39 ms
53,972 KB
testcase_44 AC 40 ms
54,416 KB
testcase_45 AC 765 ms
168,576 KB
testcase_46 AC 745 ms
168,108 KB
testcase_47 AC 759 ms
168,508 KB
testcase_48 AC 755 ms
168,036 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