当前位置: 首页 > news >正文

ABC 258 G Triangle(bitset 优化)

ABC 258 G Triangle(bitset 优化)

ABC 258 G Triangle

大意:给出一个邻接矩阵 ,用来记录两两元素间是否连接 , 计算其中三元环的数目。

思路:

不妨先想暴力解法

for(int i = 1 ; i <= n ; i ++){for(int j = i + 1 ; j <= n ; j ++){for(int k = j + 1 ; k <= n ; k ++){res += (a[i][j] && a[j][k] && a[k][i]);}}}

考虑 bitset 优化这个式子 , 可以仿照传递闭包的优化方式

优化成 if(a[i][j]) res += (a[i] & a[j]).count();

注意这样会重复计数 , 去重即可。

对于一个三元组 , 我们要求 第二维大于第一维 , 以 (1 , 2 , 3) 为例 , 会有 (1 , 2 , 3) (1 , 3 , 2) (2 , 3 , 1) 三种计数方式 , 也就是每个三元组重复记录了三次 , 除 3 即可。

时间复杂度  O ( n 3 w ) 时间复杂度~O(\frac{n^3}{w}) 时间复杂度 O(wn3)

#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define int long long
const int N = 5e3 + 10;
const int mod = 1e9 + 7;
typedef pair<int,int>PII;bitset<N>bit[N];
int n , a[N][N];
string s;
signed main(){IOScin >> n;for(int i = 1 ; i <= n ; i ++){cin >> s;for(int j = 0 ; j < n ; j ++){if(s[j] == '0') a[i][j + 1] = 0;else a[i][j + 1] = bit[i][j + 1] = 1;}}int cnt = 0;for(int i = 1 ; i <= n ; i ++){for(int j = i + 1 ; j <= n ; j ++){if(a[i][j]) cnt += (bit[i] & bit[j]).count(); }}cout << cnt / 3 << "\n";return 0;
}
//freopen("文件名.in","r",stdin);
//freopen("文件名.out","w",stdout);
http://www.lryc.cn/news/139278.html

相关文章:

  • 使用StreamLold写入 Starrocks报错:Caused by org
  • WX1860- ngbe-1.2.5 xdp程序在路由模式下,使用iperf工具测试数据包不转发,用jmeter可以
  • PHPStudy 安装tp8 php8.2.9 安装XDbug、redis扩展
  • 结构体指针和结构体数组指针
  • libdrm全解析二十 —— 源码全解析(17)
  • 基于docker搭建owncloud Harbor 构建镜像
  • 往Buildroot中增加Qt项目
  • C#-Tolewer和ToUpper的使用
  • RabbitMQ集群搭建和测试总结_亲测
  • SQLSTATE[IMSSP]: The active result for the query contains no fields.
  • 在Flutter应用内部实现分屏功能
  • Docker常用操作命令(二)
  • vue3 tailwindcss的使用
  • redis 基础篇(redis 理解)
  • C++系列-函数重载
  • leetcode-23.合并k个升序链表-day17
  • Linux scp命令
  • vue 简单实验 v-bind 变量与html属性绑定
  • 114.(cesium篇)cesium去掉时间轴并用按钮控制运动
  • 2023年清洁能源与智能电网国际会议(CCESG 2023)
  • RISC-V中国峰会 | 256核服务器高调亮相,谁与争锋?
  • 树套树小结
  • android 解决sdk代码冲突
  • C++逆天合集
  • stm32之15.超声波与灯光功能一起实现(进阶)
  • 美创科技荣获“2023年网络安全优秀创新成果大赛—杭州分站赛”两项优胜奖
  • 使用gdb+gdbserver远程调试aarch64平台程序
  • 【CesiumJS入门】(9)获取地表两点的距离及中心点——EllipsoidGeodesic
  • OLED透明屏介绍:领先科技的革命性创新
  • ESXI补丁更新