結果
問題 |
No.3200 Sinking Islands
|
ユーザー |
![]() |
提出日時 | 2025-07-11 21:40:54 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 678 ms / 2,000 ms |
コード長 | 1,005 bytes |
コンパイル時間 | 360 ms |
コンパイル使用メモリ | 82,400 KB |
実行使用メモリ | 165,208 KB |
最終ジャッジ日時 | 2025-07-11 21:41:08 |
合計ジャッジ時間 | 12,743 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 20 |
ソースコード
N,M=map(int, input().split()) A=[] for i in range(M): a,b=map(int, input().split()) a-=1;b-=1 A.append((a,b)) par=[i for i in range(N)] rank=[0]*(N) friend=[0]*N block=[0]*N size=[1]*N def find(x): if par[x]==x: return x else: par[x]=find(par[x]) return par[x] #同じ集合か判定 def same(x,y): return find(x)==find(y) def union(x,y): x=find(x) y=find(y) if x==y: return if rank[x]>rank[y]: par[y]=x size[x]+=size[y] else: par[x]=y size[y]+=size[x] if rank[x]==rank[y]: rank[y]+=1 Q=int(input()) B=[] for i in range(Q): s=int(input()) B.append(s-1) BB=set(B) for i in range(M): if i not in BB: u,v=A[i] union(u,v) al=N*(N-1)//2 F=set() for i in range(N): f=find(i) if f not in F: F.add(f) c=size[f] al-=c*(c-1)//2 ans=[] for b in B[::-1]: ans.append(al) u,v=A[b] if not same(u,v): uu,vv=size[find(u)],size[find(v)] union(u,v) al-=uu*vv for i in ans[::-1]: print(i)