Tomash keeps wandering off and getting lost while he is walking along the streets of Berland. It's no surprise! In his home town, for any pair of intersections there is exactly one way to walk from one intersection to the other one. The capital of Berland is very different!
Tomash has noticed that even simple cases of ambiguity confuse him. So, when he sees a group of four distinct intersections a, b, c and d, such that there are two paths from a to c — one through b and the other one through d, he calls the group a "damn rhombus". Note that pairs (a, b), (b, c), (a, d), (d, c) should be directly connected by the roads. Schematically, a damn rhombus is shown on the figure below:
![](https://i-blog.csdnimg.cn/blog_migrate/2c5476e6f28b47a465732d7bc0e477fa.png)
Other roads between any of the intersections don't make the rhombus any more appealing to Tomash, so the four intersections remain a "damn rhombus" for him.
Given that the capital of Berland has n intersections and m roads and all roads are unidirectional and are known in advance, find the number of "damn rhombi" in the city.
When rhombi are compared, the order of intersections b and d doesn't matter.
The first line of the input contains a pair of integers n, m (1 ≤ n ≤ 3000, 0 ≤ m ≤ 30000) — the number of intersections and roads, respectively. Next m lines list the roads, one per line. Each of the roads is given by a pair of integers ai, bi (1 ≤ ai, bi ≤ n;ai ≠ bi) — the number of the intersection it goes out from and the number of the intersection it leads to. Between a pair of intersections there is at most one road in each of the two directions.
It is not guaranteed that you can get from any intersection to any other one.
Print the required number of "damn rhombi".
枚举起点,记录2步到x1, x2, ..., xn的路径数,每个点为起点的damn number等于所有路径中选2个的组合!只要在中间加一下当前路径数-1就ok(4条:3+2+1=6)
#include <iostream>
#include <queue>
#include <cstring>
#include <cstdio>
#include <vector>
#include <unordered_map>
#define MAX 30004
using namespace std;
const int inf = 0x3f3f3f3f;
int p[MAX];
int nEdge;
int vis[MAX];
int dis[MAX];
struct edge{
int v,pre;
}e[MAX];
typedef long long ll;
int cnt;
int n,m,k;
struct damn{
int a, b, c, d;
damn():a(0), b(0), c(0), d(0){}
damn(int a0, int b0, int c0, int d0):a(a0), b(b0), c(c0), d(d0){}
bool operator == (const damn &p) const{
return (a==p.a && b==p.b && c==p.c && d==p.d);
}
};
int dr[4]={0, -1, 0, 1};
int dc[4]={-1, 0, 1, 0};
bool flag;
void addEdge(int u,int v)
{
nEdge++;
e[nEdge].v = v;
e[nEdge].pre = p[u];
p[u] = nEdge;
}
int main(){
//freopen("d:\\out.txt", "r", stdin);
memset(p, 0, sizeof(p));
cin>>n>>m;
for (int i=0;i<m; i++){
int x0, y0;
scanf("%d%d", &x0, &y0);
addEdge(x0, y0);
}
ll cnt=0; ll nnt;
//cout<<"g"<<endl;
for (int i=1; i<=n; i++){
memset(vis, 0, sizeof(vis));
//nnt=0;
//cout<<"i="<<i<<endl;
for (int j=p[i]; j; j=e[j].pre){
//cout<<"j="<<e[j].v<<endl;
//cout<<"k="<<e[k].v<<endl;
int x=e[j].v;
for (int k=p[x]; k; k=e[k].pre){
// cout<<"l="<<e[l].v<<endl;
//cout<<"ll="<<e[ll].v<<endl;
int xx=e[k].v;
if (xx==i) continue;
vis[xx]++;
cnt+=vis[xx]-1;
//cout<<"find "<<i<<" "<<x<<" "<<y<<" "<<xx<<endl;
}
}
}
cout<<cnt<<endl;
return 0;
}