题意:求有向图里面有多少个三元环。

思路:枚举起点A,遍历A可以到的B,然后求C的数量,C的数量位B可以到是地方X集合,和可以到A的地方Y集合的交集(X&Y)。

B点可以枚举,也可以遍历。(两种都试过,区别不大。)

枚举代码:

#include<cstdio>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=;
bitset<maxn>s[maxn];
bitset<maxn>f[maxn];
char c[maxn]; long long ans;
int main()
{
freopen("triatrip.in","r",stdin);//必须加上,不然得WA
freopen("triatrip.out","w",stdout);
int N,i,j;
scanf("%d",&N);
for(i=;i<=N;i++){
scanf("%s",c+);
for(j=;j<=N;j++){
if(c[j]=='+'){
s[i].set(j);
f[j].set(i);
}
}
}
for(i=;i<=N;i++)
for(j=;j<=N;j++)
if(s[i][j])
ans+=(s[j]&f[i]).count();
printf("%lld\n",ans/);
return ;
}

遍历代码:

#include<cstdio>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=;
bitset<maxn>s[maxn];
bitset<maxn>f[maxn];
const int maxm=; char c[maxn];
int Laxt[maxn],Next[maxm],To[maxm],cnt;
long long ans;
void update(int N)
{
for(int i=;i<=N;i++) s[i].reset();
for(int i=;i<=N;i++) f[i].reset();
memset(Laxt,,sizeof(Laxt));
cnt=ans=;
}
void add(int u,int v)
{
Next[++cnt]=Laxt[u];
Laxt[u]=cnt;
To[cnt]=v;
}
int main()
{
freopen("triatrip.in","r",stdin);//必须加上,不然得WA
freopen("triatrip.out","w",stdout);
int N,i,j;
while(~scanf("%d",&N)){
update(N);
for(i=;i<=N;i++){
scanf("%s",c+);
for(j=;j<=N;j++){
if(c[j]=='+'){
add(i,j);
s[i].set(j);
f[j].set(i);
}
}
}
for(i=;i<=N;i++){
for(j=Laxt[i];j;j=Next[j]){
int u=To[j];
ans+=(s[u]&f[i]).count();
}
}
printf("%lld\n",ans/);
}
return ;
}

Gym - 100342J:Triatrip(Bitset加速求三元环的数量)的更多相关文章

  1. Gym 100342J Triatrip (求三元环的数量) (bitset优化)

    <题目链接> 题目大意:用用邻接矩阵表示一个有向图,现在让你求其中三元环的数量. 解题分析:先预处理得到所有能够直接到达每个点的集合$arrive[N]$和所有能够由当前点到达的集合$to ...

  2. Codeforces Gym 100342J Problem J. Triatrip 求三元环的数量 bitset

    Problem J. Triatrip Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100342/at ...

  3. Codeforces Gym 100342J Problem J. Triatrip bitset 求三元环的数量

    Problem J. TriatripTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100342/att ...

  4. Gym - 100342J Triatrip (bitset求三元环个数)

    https://vjudge.net/problem/Gym-100342J 题意:给出一个邻接矩阵有向图,求图中的三元环的个数. 思路: 利用bitset暴力求解,记得最后需要/3. #includ ...

  5. codeforce Gym 100342J Triatrip (bitset)

    傻逼题,但是为什么别人的O(n^3)不会T?只是因为用了bitset优化... 附上一张bitset基本操作的表 #include<bits/stdc++.h> using namespa ...

  6. Codeforces Gym 100342J Problem J. Triatrip 三元环

    题目链接: http://codeforces.com/gym/100342 题意: 求三元环的个数 题解: 用bitset分别统计每个点的出度的边和入度的边. 枚举每一条边(a,b),计算以b为出度 ...

  7. [hdu 6184 Counting Stars(三元环计数)

    hdu 6184 Counting Stars(三元环计数) 题意: 给一张n个点m条边的无向图,问有多少个\(A-structure\) 其中\(A-structure\)满足\(V=(A,B,C, ...

  8. FJWC2019 子图 (三元环计数、四元环计数)

    给定 n 个点和 m 条边的一张图和一个值 k ,求图中边数为 k 的联通子图个数 mod 1e9+7. \(n \le 10^5, m \le 2 \times 10^5, 1 \le k \le ...

  9. 三元环HDU 6184

    HDU - 6184 C - Counting Stars 题目大意:有n个点,m条边,问有一共有多少个‘structure’也就是满足V=(A,B,C,D) and E=(AB,BC,CD,DA,A ...

随机推荐

  1. Linux 系统的常用命令之 rm ,rm -rf , rm -f 以及rm 命令的其他参数命令

    1.rm -rf * 删除当前目录下的所有文件,这个命令很危险,应避免使用. 所删除的文件,一般都不能恢复! 2.rm -f 其中的,f参数 (f --force ) 忽略不存在的文件,不显示任何信息 ...

  2. (48)C#网络4 web

    WebClient 类 提供用于将数据发送到和接收来自通过 URI 确认的资源数据的常用方法 private delegate string delegatehWeb(); private void ...

  3. 快速掌握RabbitMQ(四)——两种消费模式和QOS的C#实现

    本篇介绍一下RabbitMQ中的消费模式,在前边的所有栗子中我们采用的消费者都是EventingBasicConsumer,其实RabbitMQ中还有其他两种消费模式:BasicGet和QueueBa ...

  4. idea抛异常方式

    选中需要抛异常的行,按alt+enter或者ctrl+alt+t,然后上下键选择自己抛异常的方式即可,如下图:

  5. python远程访问hive

    #!/usr/bin/pythonimport syssys.path.append('/home/zhoujie/Downloads/hive-0.7.0-cdh3u0/lib/py')from h ...

  6. 使用crontab定时执行脚本时别忘了输出重定向>

    原文:https://blog.csdn.net/solmyr_biti/article/details/50683279 -------------------------------------- ...

  7. SQL SELECT TOP, LIMIT, ROWNUM 子句

    SQL SELECT TOP, LIMIT, ROWNUM 子句 SQL SELECT TOP 子句 SELECT TOP 子句用于规定要返回的记录的数目. SELECT TOP 子句对于拥有数千条记 ...

  8. 基于 orange(nginx+openresty) + docker 实现微服务 网关功能

    摘要 基于 orange(nginx+openresty) + docker 实现微服务 网关功能 ;以实现 docker 独立容器 来跑 独立语言独立环境 在 同一个授权下 运行相关组合程序..年初 ...

  9. 怎样去除JSP页面提示:Cannot return from outside a function or method.

     今天用myeclipse10写JSP页面时出现: Cannot return from outside a function or method. onClick="return ch ...

  10. mysql 安装与启动

    1.下载mysql installer 2.安装 一直点next,直到finish. 3.安装时的配置 安装完后,选择立即开始配置. 选择standard configuration 勾选安装mysq ...