Codeforce 1243 D. 0-1 MST(补图搜索,MST,bfs)

3 篇文章 0 订阅
1 篇文章 0 订阅

在这里插入图片描述


题意转化为求 由边权为0的边连通的图的连通块的数量 c n t cnt cnt,答案为 c n t − 1 cnt - 1 cnt1

用一个 set 维护 所有还没被访问过的点,然后用 bfs 算法对这个图进行遍历,每次遍历一个连通块,把遍历到的点删掉。

最差每一次遍历都会使得 set 的大小减一,复杂度是均摊的 O ( n log ⁡ n + n log ⁡ m ) O(n\log n + n\log m) O(nlogn+nlogm)
(set在删除时,it必须指向下一个,否则 it++会指向其它地方)


代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 10;
int n,m;
set<int> g[maxn],st;
int vis[maxn];
void bfs(int x) {
	queue<int> q;
	st.erase(x);
	q.push(x);
	while(!q.empty()) {
		int top = q.front();
		q.pop();
		vis[top] = 1;
		for(auto it = st.begin(); it != st.end();) {
			int v = *it++;
			if(g[v].count(top) == 0) {
				st.erase(v);
				q.push(v);
			}
		}
	}
}
int main() {
	scanf("%d%d",&n,&m);
	for(int i = 1; i <= n; i++)
		st.insert(i);
	for(int i = 1; i <= m; i++) {
		int x,y;scanf("%d%d",&x,&y);
		g[x].insert(y);
		g[y].insert(x);
	}
	int cnt = 0;
	for(int i = 1; i <= n; i++) {
		if(!vis[i]) {
			cnt++;
			bfs(i);
		}
	}
	printf("%d\n",cnt - 1);
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值