結果
問題 |
No.3200 Sinking Islands
|
ユーザー |
|
提出日時 | 2025-07-14 14:12:04 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 707 ms / 2,000 ms |
コード長 | 934 bytes |
コンパイル時間 | 390 ms |
コンパイル使用メモリ | 82,292 KB |
実行使用メモリ | 149,848 KB |
最終ジャッジ日時 | 2025-07-14 14:12:21 |
合計ジャッジ時間 | 15,496 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 20 |
ソースコード
class UnionFind: def __init__(self, n): self.par = [-1] * n self.res = n * (n - 1) // 2 def find(self, x): if self.par[x] < 0: return x self.par[x] = self.find(self.par[x]) return self.par[x] def union(self, x, y): x, y = self.find(x), self.find(y) if x == y: return if self.par[x] > self.par[y]: x, y = y, x self.res -= self.par[x] * self.par[y] self.par[x] += self.par[y] self.par[y] = x N, M = map(int, input().split()) edges = [tuple(map(int, input().split())) for _ in range(M)] Q = int(input()) B = [int(input()) for _ in range(Q)] BS = set(B) uf = UnionFind(N) for i, (u, v) in enumerate(edges, 1): if i not in BS: uf.union(u - 1, v - 1) ans = [] for b in B[::-1]: ans.append(uf.res) u, v = edges[b - 1] uf.union(u - 1, v - 1) print(*ans[::-1], sep='\n')