Codeforces Gym 100342J Problem J. Triatrip 求三元环的数量 bitset

时间:2022-11-18 17:54:20

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

题意

给出邻接矩阵,有向图,找出三元环的个数

题解

bitset 暴力枚举一条边,出度入度的集合用到bitset

代码:

 //作者:1085422276
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include<bits/stdc++.h>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
const int inf = ;
inline ll read()
{
ll x=,f=;
char ch=getchar();
while(ch<''||ch>'')
{
if(ch=='-')f=-;
ch=getchar();
}
while(ch>=''&&ch<='')
{
x=x*+ch-'';
ch=getchar();
}
return x*f;
}
ll exgcd(ll a,ll b,ll &x,ll &y)
{
ll temp,p;
if(b==){
x=;y=;
return a;
}
p=exgcd(b,a%b,x,y);
temp=x;x=y;y=temp-(a/b)*y;
return p;
}
//*******************************
#define N 1510
bitset<N> in[N],out[N],he;
char mp[][];
int main()
{ freopen("triatrip.in","r",stdin);
freopen("triatrip.out","w",stdout);
int n;
n=read();
for(int i=;i<=n;i++)
scanf("%s",mp[i]);
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
if(i==j)continue;
if(mp[i][j-]=='+')
{
in[i][j]=true;
out[j][i]=true;
}
}
}
ll ans=;
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
if(i==j)continue;
if(mp[i][j-]=='+')
he=(in[j])&(out[i]),ans+=he.count();
}
}
cout<<ans/<<endl;
return ;
}