結果

問題 No.2200 Weird Shortest Path
ユーザー roarisroaris
提出日時 2023-01-30 01:25:06
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 693 ms / 2,000 ms
コード長 1,272 bytes
コンパイル時間 354 ms
コンパイル使用メモリ 87,032 KB
実行使用メモリ 105,900 KB
最終ジャッジ日時 2023-09-12 06:53:45
合計ジャッジ時間 26,282 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
71,580 KB
testcase_01 AC 73 ms
71,368 KB
testcase_02 AC 74 ms
71,496 KB
testcase_03 AC 82 ms
76,268 KB
testcase_04 AC 92 ms
76,904 KB
testcase_05 AC 74 ms
71,424 KB
testcase_06 AC 75 ms
75,036 KB
testcase_07 AC 80 ms
75,940 KB
testcase_08 AC 91 ms
76,868 KB
testcase_09 AC 90 ms
76,928 KB
testcase_10 AC 74 ms
71,504 KB
testcase_11 AC 74 ms
71,472 KB
testcase_12 AC 74 ms
71,112 KB
testcase_13 AC 74 ms
71,108 KB
testcase_14 AC 75 ms
71,524 KB
testcase_15 AC 92 ms
76,824 KB
testcase_16 AC 73 ms
71,524 KB
testcase_17 AC 304 ms
87,284 KB
testcase_18 AC 459 ms
95,424 KB
testcase_19 AC 668 ms
102,328 KB
testcase_20 AC 639 ms
101,468 KB
testcase_21 AC 329 ms
90,376 KB
testcase_22 AC 404 ms
91,532 KB
testcase_23 AC 685 ms
102,900 KB
testcase_24 AC 231 ms
83,448 KB
testcase_25 AC 671 ms
105,372 KB
testcase_26 AC 671 ms
102,736 KB
testcase_27 AC 310 ms
87,260 KB
testcase_28 AC 530 ms
96,332 KB
testcase_29 AC 675 ms
102,900 KB
testcase_30 AC 656 ms
101,932 KB
testcase_31 AC 452 ms
92,948 KB
testcase_32 AC 693 ms
103,312 KB
testcase_33 AC 680 ms
103,128 KB
testcase_34 AC 683 ms
103,304 KB
testcase_35 AC 676 ms
103,844 KB
testcase_36 AC 680 ms
103,808 KB
testcase_37 AC 677 ms
103,696 KB
testcase_38 AC 684 ms
103,476 KB
testcase_39 AC 381 ms
91,988 KB
testcase_40 AC 374 ms
104,356 KB
testcase_41 AC 336 ms
91,084 KB
testcase_42 AC 373 ms
105,900 KB
testcase_43 AC 403 ms
103,288 KB
testcase_44 AC 400 ms
103,188 KB
testcase_45 AC 399 ms
103,204 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

class Unionfind:
    def __init__(self, n):
        self.par = [-1]*n
        self.rank = [1]*n
    
    def root(self, x):
        r = x
        
        while not self.par[r]<0:
            r = self.par[r]
        
        t = x
        
        while t!=r:
            tmp = t
            t = self.par[t]
            self.par[tmp] = r
        
        return r
    
    def unite(self, x, y):
        rx = self.root(x)
        ry = self.root(y)
        
        if rx==ry:
            return
        
        if self.rank[rx]<=self.rank[ry]:
            self.par[ry] += self.par[rx]
            self.par[rx] = ry
            
            if self.rank[rx]==self.rank[ry]:
                self.rank[ry] += 1
        else:
            self.par[rx] += self.par[ry]
            self.par[ry] = rx
    
    def is_same(self, x, y):
        return self.root(x)==self.root(y)
    
    def count(self, x):
        return -self.par[self.root(x)]
    
N, M = map(int, input().split())
edges = [tuple(map(int, input().split())) for _ in range(M)]
edges.sort(key=lambda t: t[2])
ans = 0
uf = Unionfind(N)

for u, v, w in edges:
    if not uf.is_same(u-1, v-1):
        ans += uf.count(u-1)*uf.count(v-1)*w
        uf.unite(u-1, v-1)

print(ans)
0