ABC302E

· · 题解

用 n 个 set 维护这张图,同时维护一个变量 ans,记录当前没有边相连的点数。

一、对于操作 1,连接两个点 x,y:

  1. 如果之前 x 没边相连,ans 减一;如果之前 y 没边相连,ans 同样减一。

  2. 然后再在第 x 和 y 个 set 中加边。

二、对于操作 2,清除与 x 相连的所有边:

  1. 对于每条与 x 相连的边,遍历第 x 个 set。

  2. 设 y 与 x 相连。在第 y 个 set 中用 lower_bound 找到 x,在 y 中删除。

  3. 如果现在 y 没边了,ans 加一。

  4. 遍历完 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();
}