用 个 set 维护这张图,同时维护一个变量 ,记录当前没有边相连的点数。
一、对于操作 ,连接两个点 :
-
如果之前 没边相连, 减一;如果之前 没边相连, 同样减一。
-
然后再在第 和 个 set 中加边。
二、对于操作 ,清除与 相连的所有边:
-
对于每条与 相连的边,遍历第 个 set。
-
设 与 相连。在第 个 set 中用 lower_bound 找到 ,在 中删除。
-
如果现在 没边了, 加一。
-
遍历完 的所有边后,把第 个 set 清空, 加一。
#include<bits/stdc++.h>
using namespace std;
const int N=3e5+10,INF=0x3f3f3f3f;
int read(){int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();return x*f;}
void Write(int x){if(x<0){putchar('-'),Write(-x);return;}if(x<10){putchar(x+'0');return;}Write(x/10),putchar(x%10+'0');}
void write(int x,char *s){Write(x),printf("%s",s);}
int n,m,ans;
set<int>s[N];
set<int>::iterator it;
void solve()
{
n=read(),m=read(),ans=n;
while(m--)
{
int opt=read(),x=read(),y;
if(opt==1) y=read(),ans-=s[x].empty()+s[y].empty(),s[x].insert(y),s[y].insert(x);
else
{
for(it=s[x].begin();it!=s[x].end();it++)
{
int y=*it;
s[y].erase(s[y].lower_bound(x)),ans+=s[y].empty();
}
if(s[x].size()) ans++,s[x].clear();
}
write(ans,"\n");
}
}
signed main()
{
int T=1;
while(T--) solve();
}