結果
| 問題 |
No.3200 Sinking Islands
|
| コンテスト | |
| ユーザー |
norioc
|
| 提出日時 | 2025-07-12 08:07:39 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 660 ms / 2,000 ms |
| コード長 | 1,914 bytes |
| コンパイル時間 | 338 ms |
| コンパイル使用メモリ | 82,500 KB |
| 実行使用メモリ | 149,424 KB |
| 最終ジャッジ日時 | 2025-07-12 08:07:54 |
| 合計ジャッジ時間 | 13,527 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 20 |
ソースコード
class UnionFind:
def __init__(self, n: int):
self.data = [-1] * (n+1)
self.nexts = [i for i in range(n+1)]
def root(self, a: int) -> int:
if self.data[a] < 0: return a
self.data[a] = self.root(self.data[a])
return self.data[a]
def unite(self, a: int, b: int) -> bool:
pa = self.root(a)
pb = self.root(b)
if pa == pb: return False
if self.data[pa] > self.data[pb]:
pa, pb = pb, pa
self.data[pa] += self.data[pb] # pa を pb をつなげる
self.data[pb] = pa
self.nexts[pa], self.nexts[pb] = self.nexts[pb], self.nexts[pa]
return True
def is_same(self, a: int, b: int) -> bool:
return self.root(a) == self.root(b)
def size(self, a: int) -> int:
"""a が属する集合のサイズ"""
return -self.data[self.root(a)]
def group(self, a: int):
"""a が属する集合"""
yield a
x = a
while self.nexts[x] != a:
x = self.nexts[x]
yield x
from collections import defaultdict
from itertools import accumulate
from math import comb
N, M = map(int, input().split())
adj = defaultdict(list)
edges = []
for _ in range(M):
U, V = map(lambda x: int(x)-1, input().split())
edges.append((U, V))
Q = int(input())
B = []
for _ in range(Q):
B.append(int(input()) - 1)
uf = UnionFind(N)
s = set(B)
tot = comb(N, 2) # 行き来できないペア数
for i, (u, v) in enumerate(edges):
if i not in s:
if uf.is_same(u, v): continue
a = uf.size(u)
b = uf.size(v)
tot -= a * b
uf.unite(u, v)
res = []
for bi in reversed(B):
u, v = edges[bi]
if uf.is_same(u, v):
res.append(tot)
else:
a = uf.size(u)
b = uf.size(v)
res.append(tot)
tot -= a * b
uf.unite(u, v)
print(*reversed(res), sep='\n')
norioc