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=20;
const int N=1510;
int v[N][N],f[N][80],g[N][80],sum[1<<t],p,n;
long long cnt=0;
char s[N];

int main()
{
  //  freopen("b.txt","r",stdin);
    freopen("triatrip.in","r",stdin);
    freopen("triatrip.out","w",stdout);
    for(int i=0;i<(1<<t);i++)
    {
        for(int j=0;j<t;j++)
            if(i&(1<<j)) sum[i]++;
    }
    scanf("%d",&n);
    int p=(n-1)/t+1;
    for(int i=0;i<n;i++)
    {
        scanf("%s",s);
        for(int j=0;j<n;j++)
        {
            if(s[j]=='+') v[i][j]=1;
        }
    }
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<p;j++)
        {
            for(int k=0;k<t;k++)
            {
                if(v[i][j*t+k]) f[i][j]|=1<<k;
                if(v[j*t+k][i]) g[i][j]|=1<<k;
            }
      //      cout<<f[i][j]<<" "<<g[i][j]<<endl;
        }
    }
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
            if(v[j][i])
            {
                for(int k=0;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 0;
}

 

posted @ 2015-08-06 18:54  qscqesze  阅读(671)  评论(0编辑  收藏  举报