結果
問題 |
No.3200 Sinking Islands
|
ユーザー |
|
提出日時 | 2025-07-11 22:14:25 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 885 ms / 2,000 ms |
コード長 | 2,085 bytes |
コンパイル時間 | 208 ms |
コンパイル使用メモリ | 82,352 KB |
実行使用メモリ | 177,100 KB |
最終ジャッジ日時 | 2025-07-11 22:14:41 |
合計ジャッジ時間 | 15,166 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 20 |
ソースコード
class UnionFind: def __init__(self, n, w=None): self.par = [-1]*n self.rank = [0]*n self.siz = [1]*n self.cnt = n self.min_node = [i for i in range(n)] self.weight = w if w else [1]*n def root(self, x): if self.par[x] == -1: return x self.par[x] = self.root(self.par[x]) return self.par[x] def issame(self, x, y): return self.root(x) == self.root(y) def unite(self, x, y): px = self.root(x) py = self.root(y) if px == py: return False if self.rank[px] < self.rank[py]: px, py = py, px self.par[py] = px if self.rank[px] == self.rank[py]: self.rank[px] += 1 self.siz[px] += self.siz[py] self.cnt -= 1 self.min_node[px] = min(self.min_node[px], self.min_node[py]) self.weight[px] += self.weight[py] return False def count(self): return self.cnt def min(self, x): return self.min_node[self.root(x)] def getweight(self, x): return self.weight[self.root(x)] def size(self, x): return self.siz[self.root(x)] import sys input = sys.stdin.readline N, M = map(int, input().split()) UV = [list(map(int, input().split())) for _ in range(M)] Q = int(input()) B = [int(input()) for _ in range(Q)] UF = UnionFind(N) S = set(B) for i in range(M): if i+1 not in S: u, v = UV[i] u-=1 v-=1 UF.unite(u, v) tmp = 0 used = set() for i in range(N): r = UF.root(i) if r in used: continue used.add(r) cnt = UF.size(r) tmp += cnt*(N-cnt) tmp //= 2 ans = [tmp] for b in B[::-1]: b-=1 u, v = UV[b] u-=1 v-=1 if UF.issame(u, v)==False: ucnt = UF.size(u) vcnt = UF.size(v) UF.unite(u, v) cnt = UF.size(u) tmp = cnt*(N-cnt) - ucnt*(N-ucnt) - vcnt*(N-vcnt) tmp //= 2 ans.append(ans[-1]+tmp) else: ans.append(ans[-1]) ans.pop() for a in ans[::-1]: print(a)