結果

問題 No.748 yuki国のお財布事情
ユーザー convexineqconvexineq
提出日時 2021-03-22 22:04:59
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 398 ms / 2,000 ms
コード長 1,368 bytes
コンパイル時間 335 ms
コンパイル使用メモリ 87,284 KB
実行使用メモリ 107,484 KB
最終ジャッジ日時 2023-08-16 01:08:53
合計ジャッジ時間 7,222 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
71,396 KB
testcase_01 AC 69 ms
71,468 KB
testcase_02 AC 69 ms
71,316 KB
testcase_03 AC 69 ms
71,440 KB
testcase_04 AC 69 ms
71,276 KB
testcase_05 AC 71 ms
71,324 KB
testcase_06 AC 71 ms
71,424 KB
testcase_07 AC 72 ms
71,124 KB
testcase_08 AC 68 ms
71,548 KB
testcase_09 AC 68 ms
71,280 KB
testcase_10 AC 68 ms
71,472 KB
testcase_11 AC 67 ms
71,608 KB
testcase_12 AC 70 ms
71,128 KB
testcase_13 AC 157 ms
81,272 KB
testcase_14 AC 190 ms
82,612 KB
testcase_15 AC 167 ms
82,612 KB
testcase_16 AC 217 ms
85,400 KB
testcase_17 AC 385 ms
103,184 KB
testcase_18 AC 358 ms
97,980 KB
testcase_19 AC 317 ms
93,040 KB
testcase_20 AC 290 ms
91,648 KB
testcase_21 AC 376 ms
99,352 KB
testcase_22 AC 68 ms
71,444 KB
testcase_23 AC 68 ms
71,424 KB
testcase_24 AC 68 ms
71,124 KB
testcase_25 AC 360 ms
106,428 KB
testcase_26 AC 381 ms
107,484 KB
testcase_27 AC 398 ms
105,372 KB
testcase_28 AC 216 ms
87,328 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind:
    def __init__(self, n):
        self.parent = list(range(n)) #親ノード
        self.size = [1]*n #グループの要素数
 
    def root(self, x): #root(x): xの根ノードを返す.
        while self.parent[x] != x:
            self.parent[x] = self.parent[self.parent[x]]
            x = self.parent[x]
        return x 
 
    def merge(self, x, y): #merge(x,y): xのいる組とyのいる組をまとめる
        x, y = self.root(x), self.root(y)
        if x == y: return False
        if self.size[x] < self.size[y]: x,y=y,x #xの要素数が大きいように
        self.size[x] += self.size[y] #xの要素数を更新
        self.parent[y] = x #yをxにつなぐ
        return True
 
    def issame(self, x, y): #same(x,y): xとyが同じ組ならTrue
        return self.root(x) == self.root(y)
        
    def getsize(self,x): #size(x): xのいるグループの要素数を返す
        return self.size[self.root(x)]

n,m,k = map(int,input().split())
abc = [list(map(int,input().split())) for _ in range(m)]
keep = [0]*m
for _ in range(k):
    keep[int(input())-1] = 1
use = []
UF = UnionFind(n)
for i,(a,b,c) in enumerate(abc):
    if keep[i]:
        UF.merge(a-1,b-1)
    else:
        use.append((c,a-1,b-1))
use.sort(key=lambda x:x[0])
ans = 0
for c,a,b in use:
    if not UF.merge(a,b):
        ans += c
print(ans)
0