結果

問題 No.748 yuki国のお財布事情
ユーザー maspymaspy
提出日時 2020-03-04 18:59:36
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 339 ms / 2,000 ms
コード長 1,428 bytes
コンパイル時間 119 ms
コンパイル使用メモリ 13,056 KB
実行使用メモリ 49,900 KB
最終ジャッジ日時 2024-10-14 00:23:54
合計ジャッジ時間 6,025 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 30 ms
10,752 KB
testcase_01 AC 29 ms
10,752 KB
testcase_02 AC 29 ms
10,752 KB
testcase_03 AC 28 ms
10,752 KB
testcase_04 AC 29 ms
10,752 KB
testcase_05 AC 27 ms
10,752 KB
testcase_06 AC 28 ms
10,624 KB
testcase_07 AC 31 ms
10,752 KB
testcase_08 AC 27 ms
10,624 KB
testcase_09 AC 28 ms
10,752 KB
testcase_10 AC 27 ms
10,752 KB
testcase_11 AC 27 ms
10,752 KB
testcase_12 AC 28 ms
10,880 KB
testcase_13 AC 61 ms
15,656 KB
testcase_14 AC 85 ms
18,096 KB
testcase_15 AC 71 ms
15,712 KB
testcase_16 AC 136 ms
25,692 KB
testcase_17 AC 292 ms
39,752 KB
testcase_18 AC 317 ms
42,512 KB
testcase_19 AC 327 ms
49,900 KB
testcase_20 AC 281 ms
43,268 KB
testcase_21 AC 326 ms
40,732 KB
testcase_22 AC 27 ms
10,752 KB
testcase_23 AC 26 ms
10,752 KB
testcase_24 AC 27 ms
10,752 KB
testcase_25 AC 283 ms
35,024 KB
testcase_26 AC 339 ms
40,980 KB
testcase_27 AC 323 ms
40,724 KB
testcase_28 AC 194 ms
37,536 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/env python3
# %%
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from operator import itemgetter

# %%
N, M, K = map(int, readline().split())
data = list(map(int, read().split()))
A = data[: 3 * M:3]
B = data[1: 3 * M:3]
C = data[2: 3 * M:3]
E = data[3 * M:]

E = [e - 1 for e in E]

# %%


class UnionFind:
    def __init__(self, N):
        self.root = list(range(N))
        self.size = [1] * (N)

    def find_root(self, x):
        root = self.root
        while root[x] != x:
            root[x] = root[root[x]]
            x = root[x]
        return x

    def merge(self, x, y):
        x = self.find_root(x)
        y = self.find_root(y)
        if x == y:
            return False
        sx, sy = self.size[x], self.size[y]
        if sx < sy:
            self.root[x] = y
            self.size[y] += sx
        else:
            self.root[y] = x
            self.size[x] += sy
        return True


# %%
uf = UnionFind(N + 1)
merge = uf.merge
for i in E:
    a, b, c = A[i], B[i], C[i]
    merge(a, b)

seE = set(E)
restA = [x for i, x in enumerate(A) if i not in seE]
restB = [x for i, x in enumerate(B) if i not in seE]
restC = [x for i, x in enumerate(C) if i not in seE]


# %%
answer = 0
restABC = sorted(zip(restA, restB, restC), key=itemgetter(2))
for a, b, c in restABC:
    if not merge(a, b):
        answer += c
print(answer)
0