結果

問題 No.2200 Weird Shortest Path
ユーザー minatominato
提出日時 2023-03-09 06:07:41
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,070 ms / 2,000 ms
コード長 1,052 bytes
コンパイル時間 301 ms
コンパイル使用メモリ 81,656 KB
実行使用メモリ 108,532 KB
最終ジャッジ日時 2023-10-18 06:11:27
合計ジャッジ時間 33,529 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
53,484 KB
testcase_01 AC 38 ms
53,484 KB
testcase_02 AC 40 ms
53,484 KB
testcase_03 AC 53 ms
63,264 KB
testcase_04 AC 74 ms
72,860 KB
testcase_05 AC 38 ms
53,484 KB
testcase_06 AC 42 ms
55,532 KB
testcase_07 AC 49 ms
61,212 KB
testcase_08 AC 73 ms
72,864 KB
testcase_09 AC 71 ms
70,816 KB
testcase_10 AC 40 ms
53,484 KB
testcase_11 AC 39 ms
53,484 KB
testcase_12 AC 41 ms
53,484 KB
testcase_13 AC 39 ms
53,484 KB
testcase_14 AC 39 ms
53,484 KB
testcase_15 AC 71 ms
70,816 KB
testcase_16 AC 40 ms
53,484 KB
testcase_17 AC 415 ms
86,452 KB
testcase_18 AC 634 ms
92,732 KB
testcase_19 AC 991 ms
105,556 KB
testcase_20 AC 970 ms
105,156 KB
testcase_21 AC 454 ms
87,020 KB
testcase_22 AC 596 ms
91,276 KB
testcase_23 AC 1,039 ms
106,568 KB
testcase_24 AC 284 ms
81,856 KB
testcase_25 AC 991 ms
105,848 KB
testcase_26 AC 1,012 ms
106,272 KB
testcase_27 AC 431 ms
86,600 KB
testcase_28 AC 780 ms
99,724 KB
testcase_29 AC 1,033 ms
107,232 KB
testcase_30 AC 996 ms
106,240 KB
testcase_31 AC 665 ms
94,028 KB
testcase_32 AC 1,053 ms
107,820 KB
testcase_33 AC 1,070 ms
107,696 KB
testcase_34 AC 1,047 ms
107,584 KB
testcase_35 AC 1,034 ms
108,192 KB
testcase_36 AC 1,048 ms
107,860 KB
testcase_37 AC 1,055 ms
107,628 KB
testcase_38 AC 1,052 ms
108,532 KB
testcase_39 AC 603 ms
92,344 KB
testcase_40 AC 560 ms
103,700 KB
testcase_41 AC 418 ms
89,184 KB
testcase_42 AC 556 ms
103,968 KB
testcase_43 AC 992 ms
103,364 KB
testcase_44 AC 1,008 ms
103,144 KB
testcase_45 AC 990 ms
103,068 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind:
    def __init__(self, n):
        self.n = n
        self.parent_or_size = [-1] * n

    def merge(self, a, b):
        x = self.root(a)
        y = self.root(b)
        if x == y:
            return False
        if -self.parent_or_size[x] < -self.parent_or_size[y]:
            x, y = y, x
        self.parent_or_size[x] += self.parent_or_size[y]
        self.parent_or_size[y] = x
        return True

    def same(self, a, b):
        return self.root(a) == self.root(b)

    def root(self, a):
        if self.parent_or_size[a] < 0:
            return a
        self.parent_or_size[a] = self.root(self.parent_or_size[a])
        return self.parent_or_size[a]

    def size(self, a):
        return -self.parent_or_size[self.root(a)]


N, M = map(int, input().split())

uf = UnionFind(N)
es = []
for i in range(M):
    a, b, c = map(int, input().split())
    es.append((c, a - 1, b - 1))

es.sort()
ans = 0
for c, a, b in es:
    if not uf.same(a, b):
        ans += c * uf.size(a) * uf.size(b)
        uf.merge(a, b)

print(ans)
0