Codeforces Gym100342J Triatrip

Problem J. Triatrip

Input file: triatrip.in
Output file: triatrip.out
Time limit: 3 seconds
Memory limit: 256 megabytes

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 raveler 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.

Example

triatrip.in
4
–+-
+–+
-+–
–+-
triatrip.out
2

题意

一张有向图,求有多少个不同的三元环

题解

用bitset暴力就行(读错题了以为就是找环,开心的wa了很久。。。)

#include <cstdio>
#include <cstdlib>
#include <bitset>

using namespace std;

int n;
char G[1505][1505];
bitset<1505> A[1505];
bitset<1505> B[1505];
long long ans=0;
int main()
{
    freopen("triatrip.in","r",stdin);
    freopen("triatrip.out","w",stdout);
    scanf("%d",&n);
    for(int i=0;i<n;i++)
        scanf("%s",G[i]);
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            if(G[i][j]=='+')
                A[i][j]=B[j][i]=1;
        }
    }
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            if(A[i][j])
                ans+=(A[j]&B[i]).count();
        }
    }
    printf("%I64d\n",ans/3);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值