https://codeforces.com/problemset/problem/489/D
思路:
枚举每个点跑bfs,就跑两层。然后每一个vis[i]标记点出现的次数。对于该出现的次数有C(2,vis[i])的任意选择两条路线合成菱形
#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=3010;
typedef long long LL;
inline LL read(){LL x=0,f=1;char ch=getchar(); while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
vector<LL>g[maxn];
LL vis[maxn];
LL ans=0;
LL bfs(LL u,LL n){
memset(vis,0,sizeof(vis));
queue<LL>que;
que.push(u);
for(LL i=0;i<g[u].size();i++){
LL v=g[u][i];
que.push(v);
}
que.pop();
while(!que.empty()){
LL x=que.front();que.pop();
for(LL i=0;i<g[x].size();i++){
LL v=g[x][i];
if(v!=u) {
vis[v]++;
}
}
}
LL sum=0;
for(LL i=1;i<=n;i++){
sum+=(vis[i]-1)*vis[i]/2;
}
return sum;
}
int main(void)
{
cin.tie(0);std::ios::sync_with_stdio(false);
LL n,m;cin>>n>>m;
for(LL i=1;i<=m;i++){
LL u,v;cin>>u>>v;
g[u].push_back(v);
}
for(LL i=1;i<=n;i++) ans+=bfs(i,n);
cout<<ans<<"\n";
return 0;
}