結果
問題 |
No.748 yuki国のお財布事情
|
ユーザー |
|
提出日時 | 2025-03-03 18:53:51 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 606 ms / 2,000 ms |
コード長 | 1,228 bytes |
コンパイル時間 | 443 ms |
コンパイル使用メモリ | 82,056 KB |
実行使用メモリ | 105,216 KB |
最終ジャッジ日時 | 2025-03-03 18:54:00 |
合計ジャッジ時間 | 8,691 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 26 |
ソースコード
class Unionfind: def __init__(self, num): self.num = num self.parents = [-1for _ in range(num)] def find(self, x): if self.parents[x] < 0: return x else: self.parents[x] = self.find(self.parents[x]) return self.parents[x] def union(self, x, y): x = self.find(x) y = self.find(y) if x == y: return if self.find(x) > self.find(y): x, y = y, x self.parents[x] += self.parents[y] self.parents[y] = x def size(self, x): return -self.parents[self.find(x)] def same(self, x, y): return self.find(x) == self.find(y) def main(): n, m, k = map(int, input().split()) e = [list(map(int, input().split()))for _ in range(m)] ban = set([int(input())-1 for _ in range(k)]) uf = Unionfind(n) ans = 0 s = [] for i, x in enumerate(e): u, v, w = x if i in ban: uf.union(u-1, v-1) else: s.append((w, u-1, v-1)) s.sort() for w, u, v, in s: if uf.same(u, v): ans += w else: uf.union(u, v) print(ans) if __name__ == "__main__": main()