結果
問題 | No.748 yuki国のお財布事情 |
ユーザー | dice4084 |
提出日時 | 2022-12-27 23:27:18 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 599 ms / 2,000 ms |
コード長 | 2,427 bytes |
コンパイル時間 | 351 ms |
コンパイル使用メモリ | 86,896 KB |
実行使用メモリ | 104,484 KB |
最終ジャッジ日時 | 2023-08-14 13:19:08 |
合計ジャッジ時間 | 9,829 ms |
ジャッジサーバーID (参考情報) |
judge11 / judge13 |
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 74 ms
71,232 KB |
testcase_01 | AC | 77 ms
71,060 KB |
testcase_02 | AC | 74 ms
71,268 KB |
testcase_03 | AC | 73 ms
71,052 KB |
testcase_04 | AC | 72 ms
71,336 KB |
testcase_05 | AC | 71 ms
71,008 KB |
testcase_06 | AC | 73 ms
71,336 KB |
testcase_07 | AC | 75 ms
71,224 KB |
testcase_08 | AC | 69 ms
71,420 KB |
testcase_09 | AC | 69 ms
71,352 KB |
testcase_10 | AC | 71 ms
71,400 KB |
testcase_11 | AC | 73 ms
71,076 KB |
testcase_12 | AC | 72 ms
71,176 KB |
testcase_13 | AC | 221 ms
80,968 KB |
testcase_14 | AC | 226 ms
82,776 KB |
testcase_15 | AC | 152 ms
81,676 KB |
testcase_16 | AC | 308 ms
86,648 KB |
testcase_17 | AC | 497 ms
96,988 KB |
testcase_18 | AC | 599 ms
100,624 KB |
testcase_19 | AC | 525 ms
102,460 KB |
testcase_20 | AC | 454 ms
100,352 KB |
testcase_21 | AC | 563 ms
99,620 KB |
testcase_22 | AC | 70 ms
71,116 KB |
testcase_23 | AC | 69 ms
71,256 KB |
testcase_24 | AC | 70 ms
71,236 KB |
testcase_25 | AC | 325 ms
103,572 KB |
testcase_26 | AC | 552 ms
102,692 KB |
testcase_27 | AC | 522 ms
104,484 KB |
testcase_28 | AC | 285 ms
95,672 KB |
ソースコード
import sys input = sys.stdin.readline class UnionFind(): def __init__(self, n: int) -> None: """ Arguments: n {int} -- 木の全頂点数 """ self.par = [-1]*n self.rank = [0]*n self.siz = [1]*n def root(self, x: int) -> int: """ Arguments: x {int} -- 根を知りたい頂点 Return: root(x) {int} -- 入力 x の根にあたる頂点 """ if self.par[x] == -1: return x self.par[x] = self.root(self.par[x]) return self.par[x] def is_same(self, x: int, y: int) -> bool: """ 2頂点が同じグループに属するかを判定 Arguments: x, y {int} -- 判定したい2頂点 Return: {bool} -- 同じならTrue, 異なるならFalse """ return self.root(x) == self.root(y) def unite(self, x, y) -> bool: """ 頂点 x, y を同じグループとして結合 既に同じグループなら何もしない Arguments: x, y {int} -- 結合したい2頂点 Return: {bool} -- 結合したらTrue, 既に同じ場合はFalse """ if self.is_same(x, y): return False rx = self.root(x) ry = self.root(y) if self.rank[rx] < self.rank[ry]: rx, ry = ry, rx elif self.rank[rx] == self.rank[ry]: self.rank[rx] += 1 self.par[ry] = rx self.siz[rx] += self.siz[ry] return True def size(self, x) -> int: """ 頂点が属するグループの大きさ Arguments: x {int} -- 大きさを知りたい頂点 Return: size(x) {int} -- 頂点 x の属するグループの大きさ """ return self.siz[self.root(x)] n, m, k = map(int, input().split()) roads = [tuple(map(int, input().split())) for _ in range(m)] s = set() uf = UnionFind(n+1) cost = 0 all_cost = sum(roads[i][2] for i in range(m)) for i in range(m): s.add(roads[i]) for _ in range(k): e = int(input()) - 1 a, b = (roads[e][0], roads[e][1]) cost += roads[e][2] s.discard((a, b)) uf.unite(a, b) s = sorted(list(s), key=lambda x: x[2]) for a, b, c in s: if uf.unite(a, b): cost += c print(all_cost-cost)