Codeforces Gym 100342J Problem J. Triatrip bitset 求三元环的数量
Problem J. Triatrip
Time Limit: 20 Sec
Memory Limit: 256 MB
题目连接
http://codeforces.com/gym/100342/attachments
Description
The travel agency “Four Russians” is offering the new service for their clients. Unlike other agencies that only suggest one-way or roundtrip for airline tickets to their customers, “Four Russians” offers the brand new idea — triatrip. Triatrip traveler starts in some city A, flies to some city B, then flies to some city C, and returns to the city A.
Now the managers of the agency started to wonder, how many different triatrips they can offer to their customers. Given a map of all possible flights, help them to find that out.
Input
The first line of the input file contains two integer numbers n — the number of cities that are served by airlines that agree to sell their tickets via the agency (3 ≤ n ≤ 1500). The following n lines contain a sequence of n characters each — the j-th character of the i-th line is ‘+’ if it is possible to fly from the i-th city to the j-th one, and ‘-’ if it is not. The i-th character of the i-th line is ‘-’.
Output
Output one integer number — the number of triatrips that the agency can offer to its customers.
Sample Input
4
--+-
+--+
-+--
--+-
Sample Output
2
HINT
题意
给你一个临街矩阵,然后让你找有多少个三元环
题解:
这道题数据范围只有1500,所以可以n^2,我们暴力枚举两个点,假设为A->B,然后我们预处理出有哪些点可以到A,B可以到哪些点,这样就可以得到俩集合,然后再交一下,再统计一下集合里面元素的个数就好了
这个可以用bitset来解决,但是窝们太弱了,完全不知道这个东西,于是就手写的BITSET= =,我的队友太神了
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; const int t=;
const int N=;
int v[N][N],f[N][],g[N][],sum[<<t],p,n;
long long cnt=;
char s[N]; int main()
{
// freopen("b.txt","r",stdin);
freopen("triatrip.in","r",stdin);
freopen("triatrip.out","w",stdout);
for(int i=;i<(<<t);i++)
{
for(int j=;j<t;j++)
if(i&(<<j)) sum[i]++;
}
scanf("%d",&n);
int p=(n-)/t+;
for(int i=;i<n;i++)
{
scanf("%s",s);
for(int j=;j<n;j++)
{
if(s[j]=='+') v[i][j]=;
}
}
for(int i=;i<n;i++)
{
for(int j=;j<p;j++)
{
for(int k=;k<t;k++)
{
if(v[i][j*t+k]) f[i][j]|=<<k;
if(v[j*t+k][i]) g[i][j]|=<<k;
}
// cout<<f[i][j]<<" "<<g[i][j]<<endl;
}
}
for(int i=;i<n;i++)
{
for(int j=;j<n;j++)
if(v[j][i])
{
for(int k=;k<p;k++)
{
int x=f[i][k]&g[j][k];
cnt+=(long long)sum[x];
// cout<<i<<" "<<j<<" "<<x<<" "<<sum[x]<<endl;
}
}
}
printf("%lld\n",cnt/3LL);
return ;
}
Codeforces Gym 100342J Problem J. Triatrip bitset 求三元环的数量的更多相关文章
- Codeforces Gym 100342J Problem J. Triatrip 求三元环的数量 bitset
Problem J. Triatrip Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100342/at ...
- Codeforces Gym 100342J Problem J. Triatrip 三元环
题目链接: http://codeforces.com/gym/100342 题意: 求三元环的个数 题解: 用bitset分别统计每个点的出度的边和入度的边. 枚举每一条边(a,b),计算以b为出度 ...
- Gym 100342J Triatrip (求三元环的数量) (bitset优化)
<题目链接> 题目大意:用用邻接矩阵表示一个有向图,现在让你求其中三元环的数量. 解题分析:先预处理得到所有能够直接到达每个点的集合$arrive[N]$和所有能够由当前点到达的集合$to ...
- Gym - 100342J Triatrip (bitset求三元环个数)
https://vjudge.net/problem/Gym-100342J 题意:给出一个邻接矩阵有向图,求图中的三元环的个数. 思路: 利用bitset暴力求解,记得最后需要/3. #includ ...
- Gym - 100342J:Triatrip(Bitset加速求三元环的数量)
题意:求有向图里面有多少个三元环. 思路:枚举起点A,遍历A可以到的B,然后求C的数量,C的数量位B可以到是地方X集合,和可以到A的地方Y集合的交集(X&Y). B点可以枚举,也可以遍历.(两 ...
- Codeforces Gym 100342C Problem C. Painting Cottages 转化题意
Problem C. Painting CottagesTime Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/10 ...
- Codeforces Gym 100610 Problem A. Alien Communication Masterclass 构造
Problem A. Alien Communication Masterclass Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codefo ...
- Codeforces Gym 100610 Problem E. Explicit Formula 水题
Problem E. Explicit Formula Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/10 ...
- Codeforces Gym 100002 Problem F "Folding" 区间DP
Problem F "Folding" Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/ ...
随机推荐
- TCP/IP详解学习笔记(10)-TCP连接的建立与中止
TCP是一个面向连接的协议,所以在连接双方发送数据之前,都需要首先建立一条连接.这和前面讲到的协议完全不同.前面讲的所有协议都只是发送数据而已,大多数都不关心发送的数据是不是送到,UDP尤其明显,从编 ...
- iOS开发:AFNetworking、MKNetworkKit和ASIHTTPRequest比较
转:http://www.xue5.com/Mobile/iOS/747036.html 之前一直在使用ASIHTTPRequest作为网络库,但是由于其停止更新,iOS7上可能出现更多的问题,于是决 ...
- ubuntu1204上不能正常用emacs配合gocode进行自动补全
我按gocode的页面https://github.com/nsf/gocode上去做,可是还是未成功,,我确认auto-complete在c-mode中是可以使用的,因为有补全出来了, 我再找了ht ...
- golang学习遭遇错误原因分析续
7. error: reference to field ‘Printf’ in object which has no fields or methods f.Printf("%v&quo ...
- 指针和引用的比较(P105)
指针和引用的比较? 虽然使用引用和指针都可间接访问另一个值,但它们之间有两个重要区别. 第一个区别在于引用总是指向某个对象:定义引用时没有初始化是错误的. 第二个重要区别则是赋值行为的差异:给引用赋值 ...
- CSS hack大全
1.什么是CSS hack? CSS hack是通过在CSS样式中加入一些特殊的符号,让不同的浏览器识别不同的符号(什么样的浏览器识别什么样的符号是有标准的,CSS hack就是让你记住这个标准),以 ...
- 【windows核心编程】DLL相关(1)
DLL相关的东西 1.DLL的加载方式 隐式: #pragma comment(lib, "XX.lib"); 编译器去查找名为XX.dll的DLL,除了名字相同,该DLL和该LI ...
- codevs1796-最小完全图
表示第一篇就是水题. 根据Prim的思想,我们可以证明:dis ( a , b ) > max { a b 最小生成树路径上的边权 } 把所有边sort一遍用并查集维护就可以了 #include ...
- LeetCode题解——String to Integer(atoi)
题目: 字符串转换为数字. 解法: 这道题的意思是要考虑到,如果有前置的空字符,则跳过:如果超出数字范围,则返回最大/最小整数:如果碰到第一个不能转换的字符,则返回. 代码: class Soluti ...
- SRM DIV1 500pt DP
SRM 501 DIV1 500pt SRM 502 DIV1 500pt SRM 508 DIV1 500pt SRM 509 DIV1 500pt SRM 511 DIV1 500pt SRM 5 ...