题目描述
输入描述
输出描述
输入样例
5 5 3
1 2
1 3
2 3
3 4
4 5
3 4
2 3
4 5
输出样例
2 2 3
数据范围
由于删边统计较难实现,因此通过离线逆序的操作,将所有的删边操作更改为加边操作,并判断添加了该边之前两点的祖先是否相同,若不同则说明正序操作的删除该边后,连通块将增加。
将所有被炸的边存入,且最初的连通块数目即为 n n n (此时各个点都是独立的连通块),遍历所有初始的边,若该边在之后会被炸毁则不建。由此可得进行了所有的炸毁操作得到的连通块状态。
然后对所有的炸毁操作进行逆序遍历,如果某条边在加上前,两点处于不同的连通块,则在加上该边后连通块的数量将 − 1 -1 −1 。
#include <bits/stdc++.h>
#define int long long
#define PII pair<int,int>
using namespace std;
const int N=2e5+5;
PII p[N];
PII del[N];
map <PII,int> mp;
int f[N];
int find(int x){
if(f[x]==x)
return x;
return f[x]=find(f[x]);
}
vector <int> ans;
signed main(){
int n,m,k;
cin>>n>>m>>k;
for(int i=1;i<=m;i++)
cin>>p[i].first>>p[i].second;
for(int i=1;i<=k;i++){
cin>>del[i].first>>del[i].second;
mp[{del[i].first,del[i].second}]=1;
mp[{del[i].second,del[i].first}]=1;
}
for(int i=1;i<=n;i++)
f[i]=i;
int tot=n;//初始时所有的点都为独立的连通块
for(int i=1;i<=m;i++){//进行了所有的炸毁操作后的连通块状态
if(!mp[{p[i].first,p[i].second}]){//该边如果在之后不会被炸毁 则进行加边操作
int x=find(p[i].first), y=find(p[i].second);
if(x!=y){
tot--;
f[y]=x;
}
}
}
for(int i=k;i>=1;i--){
ans.push_back(tot);
int x=find(del[i].first), y=find(del[i].second);
if(x!=y){//加上该边前 两点处在不同的连通块中 则加边后连通块-1
f[y]=x;
tot--;
}
}
for(int i=k-1;i>=0;i--) cout<<ans[i]<<' ';
}