結果

問題 No.3200 Sinking Islands
ユーザー titia
提出日時 2025-07-12 01:16:21
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 665 ms / 2,000 ms
コード長 1,256 bytes
コンパイル時間 186 ms
コンパイル使用メモリ 82,232 KB
実行使用メモリ 143,688 KB
最終ジャッジ日時 2025-07-12 01:16:36
合計ジャッジ時間 13,224 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 20
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline


N,M=map(int,input().split())

# UnionFind

Group = [i for i in range(N+1)] # グループ分け
Nodes = [1]*(N+1) # 各グループのノードの数

def find(x):
    while Group[x] != x:
        x=Group[x]
    return x

def Union(x,y):
    if find(x) != find(y):
        if Nodes[find(x)] < Nodes[find(y)]:
            
            Nodes[find(y)] += Nodes[find(x)]
            Nodes[find(x)] = 0
            Group[find(x)] = find(y)
            
        else:
            Nodes[find(x)] += Nodes[find(y)]
            Nodes[find(y)] = 0
            Group[find(y)] = find(x)

BR=[list(map(int,input().split())) for i in range(M)]

Q=int(input())

B=[]

for i in range(Q):
    x=int(input())
    B.append(x-1)

SET=set(B)

ANS=N*(N-1)//2

for i in range(M):
    if i in SET:
        pass
    else:
        if find(BR[i][0]) != find(BR[i][1]):
            x=Nodes[find(BR[i][0])]
            y=Nodes[find(BR[i][1])]
            ANS-=x*y
            Union(BR[i][0],BR[i][1])




LANS=[ANS]

for x in B[::-1]:
    z,w=BR[x]

    if find(z)!=find(w):
        x0=Nodes[find(z)]
        y0=Nodes[find(w)]
        ANS-=x0*y0
        
        Union(z,w)

    LANS.append(ANS)

LANS.reverse()

for x in LANS[1:]:
    print(x)
0