您的位置:首页 > 其它

Codeforces Gym100342J Triatrip

2015-08-07 10:34 639 查看

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  codeforces