bieset学习、

本文介绍了一种使用bitset进行优化的Floyd算法实现方法,该方法可以有效减少内存占用并提高算法效率。通过使用C++标准库中的bitset,可以在解决图论问题中寻找不可达点对时显著提升性能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

参考:blackcat

bitset 优化floyd

#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <utility>
#include <bitset>

using namespace std;
#define LL long long
#define pb push_back
#define mk make_pair
#define pill pair<int, int>
#define mst(a, b)	memset(a, b, sizeof a)
#define REP(i, x, n)	for(int i = x; i <= n; ++i)
const int MOD = 1e9 + 7;
const int qq = 1e3 + 10;
const int INF = 1e9 + 10;
int n, m;
bitset<qq> mp[qq];
void Floyd() {
	for(int i = 1; i <= n; ++i) {
		for(int j = 1; j <= n; ++j) {
			if(mp[j][i])	mp[j] |= mp[i];
		}
	}
}

int main(){
	scanf("%d%d", &n, &m);
	for(int a, b, i = 1; i <= m; ++i) {
		scanf("%d%d", &a, &b);
		mp[a][b] = 1;
	}
	Floyd();
	int ans = 0;
	for(int i = 1; i <= n; ++i) {
		for(int j = i + 1; j <= n; ++j) {
			if(!mp[i][j] && !mp[j][i])	ans++;
		}
	}
	printf("%d\n", ans);
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值