結果

問題 No.2200 Weird Shortest Path
ユーザー minatominato
提出日時 2023-03-09 06:36:34
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 409 ms / 2,000 ms
コード長 1,283 bytes
コンパイル時間 220 ms
コンパイル使用メモリ 81,904 KB
実行使用メモリ 85,708 KB
最終ジャッジ日時 2023-10-18 06:11:44
合計ジャッジ時間 13,069 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
53,396 KB
testcase_01 AC 38 ms
53,396 KB
testcase_02 AC 40 ms
53,396 KB
testcase_03 AC 57 ms
63,152 KB
testcase_04 AC 76 ms
72,388 KB
testcase_05 AC 40 ms
51,920 KB
testcase_06 AC 45 ms
53,572 KB
testcase_07 AC 54 ms
60,784 KB
testcase_08 AC 77 ms
71,872 KB
testcase_09 AC 77 ms
71,420 KB
testcase_10 AC 41 ms
52,532 KB
testcase_11 AC 40 ms
52,248 KB
testcase_12 AC 42 ms
52,820 KB
testcase_13 AC 40 ms
52,108 KB
testcase_14 AC 40 ms
52,300 KB
testcase_15 AC 76 ms
71,504 KB
testcase_16 AC 40 ms
52,312 KB
testcase_17 AC 247 ms
78,912 KB
testcase_18 AC 326 ms
81,412 KB
testcase_19 AC 366 ms
85,032 KB
testcase_20 AC 389 ms
85,080 KB
testcase_21 AC 251 ms
80,076 KB
testcase_22 AC 306 ms
80,476 KB
testcase_23 AC 379 ms
84,952 KB
testcase_24 AC 221 ms
78,372 KB
testcase_25 AC 367 ms
84,560 KB
testcase_26 AC 374 ms
84,940 KB
testcase_27 AC 276 ms
79,604 KB
testcase_28 AC 341 ms
82,360 KB
testcase_29 AC 391 ms
85,140 KB
testcase_30 AC 377 ms
84,636 KB
testcase_31 AC 313 ms
81,196 KB
testcase_32 AC 409 ms
84,856 KB
testcase_33 AC 394 ms
84,864 KB
testcase_34 AC 398 ms
85,352 KB
testcase_35 AC 379 ms
85,240 KB
testcase_36 AC 391 ms
84,852 KB
testcase_37 AC 379 ms
84,852 KB
testcase_38 AC 398 ms
85,708 KB
testcase_39 AC 311 ms
80,716 KB
testcase_40 AC 238 ms
84,520 KB
testcase_41 AC 153 ms
79,756 KB
testcase_42 AC 237 ms
84,704 KB
testcase_43 AC 324 ms
84,992 KB
testcase_44 AC 332 ms
84,808 KB
testcase_45 AC 329 ms
84,808 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)


def valtotuple(val: int):
    a = val % N
    val //= N
    b = val % N
    c = val // N
    return c, a, b


def tupletoval(tup) -> int:
    c, a, b = tup
    return c * N * N + a * N + b


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

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

print(ans)
0