zoukankan      html  css  js  c++  java
  • 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
    ",cnt/3LL);
        return 0;
    }
  • 相关阅读:
    使用beanUtils操纵javabean
    反射
    JDK5.0新特性(静态导入、自动装箱/拆箱、增强for循环、可变参数、枚举、泛形)
    Junit测试框架
    Eclipse常用快捷键
    Linux最全基础指令
    log file sync等待事件
    数据库要不要部署在docker容器内?
    MySQL启动报错-The server quit without updating PID file[FAILED]mysql/mysql.pid).
    MySQL数据库启动异常-[ERROR] [MY-011971]
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4708878.html
Copyright © 2011-2022 走看看