結果

問題 No.748 yuki国のお財布事情
ユーザー maspymaspy
提出日時 2020-03-04 18:59:36
言語 Python3
(3.11.6 + numpy 1.26.0 + scipy 1.11.3)
結果
AC  
実行時間 365 ms / 2,000 ms
コード長 1,428 bytes
コンパイル時間 172 ms
コンパイル使用メモリ 10,784 KB
実行使用メモリ 47,460 KB
最終ジャッジ日時 2023-08-04 03:18:26
合計ジャッジ時間 6,157 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
8,172 KB
testcase_01 AC 16 ms
8,128 KB
testcase_02 AC 16 ms
8,132 KB
testcase_03 AC 16 ms
8,216 KB
testcase_04 AC 16 ms
8,128 KB
testcase_05 AC 16 ms
8,068 KB
testcase_06 AC 17 ms
8,132 KB
testcase_07 AC 17 ms
8,040 KB
testcase_08 AC 17 ms
8,036 KB
testcase_09 AC 16 ms
8,112 KB
testcase_10 AC 17 ms
8,044 KB
testcase_11 AC 17 ms
8,040 KB
testcase_12 AC 16 ms
8,124 KB
testcase_13 AC 51 ms
13,712 KB
testcase_14 AC 72 ms
16,556 KB
testcase_15 AC 56 ms
14,108 KB
testcase_16 AC 128 ms
23,104 KB
testcase_17 AC 284 ms
37,268 KB
testcase_18 AC 315 ms
40,256 KB
testcase_19 AC 316 ms
47,460 KB
testcase_20 AC 275 ms
40,640 KB
testcase_21 AC 317 ms
38,232 KB
testcase_22 AC 16 ms
8,132 KB
testcase_23 AC 16 ms
8,120 KB
testcase_24 AC 16 ms
8,072 KB
testcase_25 AC 283 ms
32,520 KB
testcase_26 AC 364 ms
38,504 KB
testcase_27 AC 365 ms
38,028 KB
testcase_28 AC 199 ms
35,224 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