跳到正文

题解

ABC302E

ran_qwq约 3 分钟
原文
文章信息与版本

文章信息

收录
2026年2月3日
更新
2026年2月3日
篇幅
约 3 分钟

版本

  1. v1当前

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

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

  1. 如果之前 xx 没边相连,ansans 减一;如果之前 yy 没边相连,ansans 同样减一。

  2. 然后再在第 xx 和 yy 个 set 中加边。

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

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

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

  3. 如果现在 yy 没边了,ansans 加一。

  4. 遍历完 xx 的所有边后,把第 xx 个 set 清空,ansans 加一。

C++
#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();
}
原文