結果
問題 | No.748 yuki国のお財布事情 |
ユーザー | nanae |
提出日時 | 2018-07-27 17:33:46 |
言語 | Python3 (3.12.2 + numpy 1.26.4 + scipy 1.12.0) |
結果 |
AC
|
実行時間 | 1,013 ms / 2,000 ms |
コード長 | 2,523 bytes |
コンパイル時間 | 81 ms |
コンパイル使用メモリ | 12,800 KB |
実行使用メモリ | 44,120 KB |
最終ジャッジ日時 | 2024-07-04 19:11:45 |
合計ジャッジ時間 | 9,188 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 32 ms
11,008 KB |
testcase_01 | AC | 29 ms
10,880 KB |
testcase_02 | AC | 29 ms
10,880 KB |
testcase_03 | AC | 29 ms
10,880 KB |
testcase_04 | AC | 29 ms
11,008 KB |
testcase_05 | AC | 30 ms
10,880 KB |
testcase_06 | AC | 29 ms
11,008 KB |
testcase_07 | AC | 29 ms
11,008 KB |
testcase_08 | AC | 29 ms
11,008 KB |
testcase_09 | AC | 30 ms
10,880 KB |
testcase_10 | AC | 30 ms
10,880 KB |
testcase_11 | AC | 28 ms
10,880 KB |
testcase_12 | AC | 29 ms
11,008 KB |
testcase_13 | AC | 102 ms
14,848 KB |
testcase_14 | AC | 193 ms
19,328 KB |
testcase_15 | AC | 121 ms
15,616 KB |
testcase_16 | AC | 335 ms
24,192 KB |
testcase_17 | AC | 621 ms
39,612 KB |
testcase_18 | AC | 816 ms
42,180 KB |
testcase_19 | AC | 1,013 ms
44,120 KB |
testcase_20 | AC | 877 ms
40,964 KB |
testcase_21 | AC | 834 ms
40,764 KB |
testcase_22 | AC | 30 ms
10,880 KB |
testcase_23 | AC | 29 ms
10,880 KB |
testcase_24 | AC | 30 ms
10,880 KB |
testcase_25 | AC | 498 ms
37,028 KB |
testcase_26 | AC | 798 ms
40,968 KB |
testcase_27 | AC | 787 ms
40,808 KB |
testcase_28 | AC | 610 ms
32,228 KB |
ソースコード
from operator import itemgetter N = None M = None K = None edges = [] must = [] def main(): get_instance() check_constraint() solve() def solve(): ans = sum(edges[i][2] for i in range(M)) uf = UnionFind(N) for i in range(K): ai, bi, ci = edges[must[i]-1] uf.unite(ai-1, bi-1) ans -= ci es = sorted(edges, key=itemgetter(2)) for i in range(M): if uf.numcc == 1: break ai, bi, ci = es[i] if not uf.same(ai-1, bi-1): uf.unite(ai-1, bi-1) ans -= ci print(ans) def get_instance(): global N, M, K, edges, must N, M, K = map(int, input().split()) edges = [tuple(map(int, input().split())) for i in range(M)] must = [int(input()) for i in range(K)] def check_constraint(): assert 1 <= N <= 10**5, "Out of Range" assert 0 <= M <= min(N*(N-1)//2, 10**5), "Out of Range" assert 0 <= K <= M, "Out of Range" nwes = [] for i in range(M): ai, bi, ci = edges[i] assert 1 <= ai < bi <= N, "Out of Range" assert 1 <= ci <= 10**9, "Out of Range" nwes.append((ai, bi)) assert len(set(nwes)) == M, "Not Simple" for i in range(K): assert 1 <= must[i] <= M, "Out of Range" if i > 0: assert must[i] > must[i-1], "Not Sorted" assert is_connected(), "Not Connected" def is_connected(): uf = UnionFind(N) for i in range(M): ai, bi, ci = edges[i] uf.unite(ai-1, bi-1) return all(uf.same(0, i) for i in range(N)) class UnionFind: def __init__(self, N): self.p = list(range(N)) self.rank = [0] * N self.size = [1] * N self.numcc = N def find_root(self, x): if self.p[x] != x: self.p[x] = self.find_root(self.p[x]) return self.p[x] def same(self, x, y): return self.find_root(x) == self.find_root(y) def unite(self, x, y): u = self.find_root(x) v = self.find_root(y) if u == v: return if self.rank[u] < self.rank[v]: self.p[u] = v self.size[v] += self.size[u] self.size[u] = 0 else: self.p[v] = u self.size[u] += self.size[v] self.size[v] = 0 if self.rank[u] == self.rank[v]: self.rank[u] += 1 self.numcc -= 1 def get_size(self, x): return self.size[self.find_root(x)] if __name__ == '__main__': main()