結果

問題 No.748 yuki国のお財布事情
ユーザー stngstng
提出日時 2022-06-05 19:35:28
言語 PyPy3
(7.3.13)
結果
AC  
実行時間 545 ms / 2,000 ms
コード長 1,412 bytes
コンパイル時間 181 ms
コンパイル使用メモリ 81,696 KB
実行使用メモリ 109,480 KB
最終ジャッジ日時 2023-10-21 03:15:41
合計ジャッジ時間 8,827 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 36 ms
53,476 KB
testcase_01 AC 36 ms
53,476 KB
testcase_02 AC 36 ms
53,476 KB
testcase_03 AC 36 ms
53,476 KB
testcase_04 AC 33 ms
53,476 KB
testcase_05 AC 36 ms
53,476 KB
testcase_06 AC 34 ms
53,476 KB
testcase_07 AC 34 ms
53,476 KB
testcase_08 AC 34 ms
53,476 KB
testcase_09 AC 36 ms
53,476 KB
testcase_10 AC 37 ms
53,476 KB
testcase_11 AC 38 ms
53,476 KB
testcase_12 AC 36 ms
53,476 KB
testcase_13 AC 118 ms
78,356 KB
testcase_14 AC 184 ms
80,316 KB
testcase_15 AC 128 ms
78,564 KB
testcase_16 AC 266 ms
87,020 KB
testcase_17 AC 412 ms
92,080 KB
testcase_18 AC 545 ms
102,712 KB
testcase_19 AC 425 ms
109,480 KB
testcase_20 AC 369 ms
105,768 KB
testcase_21 AC 533 ms
101,692 KB
testcase_22 AC 36 ms
53,476 KB
testcase_23 AC 34 ms
53,476 KB
testcase_24 AC 35 ms
53,476 KB
testcase_25 AC 246 ms
88,336 KB
testcase_26 AC 426 ms
96,628 KB
testcase_27 AC 487 ms
97,620 KB
testcase_28 AC 252 ms
98,920 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind:
    def __init__(self, n):
        self.par = [i for i in range(n+1)]
        self.rank = [0] * (n+1)
        self.size = [1 for _ in range(n+1)]
    # 検索
    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]
    # 併合
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if self.rank[x] < self.rank[y]:
            self.par[x] = y
            self.size[y] += self.size[x]
        else:
            self.par[y] = x
            self.size[x] += self.size[y]
        if self.rank[x] == self.rank[y]:
            self.rank[x] += 1
    # 同じ集合に属するか判定
    def same_check(self, x, y):
        return self.find(x) == self.find(y)

n,m,k = map(int,input().split())
li = []
ab = [[] for i in range(n)]

for i in range(m):
    a,b,c = map(int,input().split())
    a -= 1
    b -= 1
    li.append([a,b,c,i])
    #ab[a].append([b,c])
    #[b].append([a,c])

e = [int(input())-1 for i in range(k)]

ki = UnionFind(n+1)
for i in range(k):
    a,b,c,idx = li[e[i]]
    ki.union(a,b)

e = set(e)

li.sort(key=lambda x:(x[2]))
#print(li)
ans = 0

for i in range(m):
    if li[i][3] in e:
        continue
    a,b,c,idx = li[i]
    if not ki.same_check(a,b):
        ki.union(a,b)
    else:
        ans += c
        #print(i,ans)

print(ans)
0