ABC302E
用
一、对于操作 1 ,连接两个点 x,y :
-
如果之前
x 没边相连,ans 减一;如果之前y 没边相连,ans 同样减一。 -
然后再在第
x 和y 个 set 中加边。
二、对于操作 2 ,清除与 x 相连的所有边:
-
对于每条与
x 相连的边,遍历第x 个 set。 -
设
y 与x 相连。在第y 个 set 中用 lower_bound 找到x ,在y 中删除。 -
如果现在
y 没边了,ans 加一。 -
遍历完
x 的所有边后,把第x 个 set 清空,ans 加一。
#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();
}