Little A is an astronomy lover, and he has found that the sky was so beautiful!
So he is counting stars now!
There are n stars in the sky, and little A has connected them by m non-directional edges.
It is guranteed that no edges connect one star with itself, and every two edges connect different pairs of stars.
Now little A wants to know that how many different "A-Structure"s are there in the sky, can you help him?
An "A-structure" can be seen as a non-directional subgraph G, with a set of four nodes V and a set of five edges E.
If V=(A,B,C,D)V=(A,B,C,D) and E=(AB,BC,CD,DA,AC)E=(AB,BC,CD,DA,AC), we call G as an "A-structure".
It is defined that "A-structure" G1=V1+E1G1=V1+E1 and G2=V2+E2G2=V2+E2 are same only in the condition that V1=V2V1=V2 and E1=E2E1=E2.
Input
There are no more than 300 test cases.
For each test case, there are 2 positive integers n and m in the first line.
2≤n≤1052≤n≤105, 1≤m≤min(2×105,n(n−1)2)1≤m≤min(2×105,n(n−1)2)
And then m lines follow, in each line there are two positive integers u and v, describing that this edge connects node u and node v.
1≤u,v≤n1≤u,v≤n
∑n≤3×105∑n≤3×105,∑m≤6×105∑m≤6×105
Output
For each test case, just output one integer--the number of different "A-structure"s in one line.
Sample Input
4 5 1 2 2 3 3 4 4 1 1 3 4 6 1 2 2 3 3 4 4 1 1 3 2 4
Sample Output
1 6
给你一个结构,这个结构是由有一条公共边的两个三元环构成的
将点分为两类,深度大于sqrt(m)和小于的,暴力查找与当前这个点相连点的相连点是否相连 (说的有点绕,画个图就明白了)
灵魂画师。。。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e5 + 10;
const int base=1e5;
unordered_set<ll> s;
int dep[maxn],vis[maxn],p[maxn],n, m, u, v, b;
vector<int> G[maxn];
ll solve()
{
ll ans = 0;
for(int u = 1;u <= n;u++)
{
vis[u] = 1;
for(auto v:G[u]) p[v] = u;
for(auto v:G[u])
{
if(vis[v]) continue;int cnt = 0;
if(dep[v] <= b)for(auto ww:G[v])if(p[ww]==u)cnt++;
if(dep[v]>b)for(auto ww:G[u])if(s.find(1LL*v*base+ww)!=s.end())cnt++;
ans+=1LL*cnt*(cnt-1)/2;
}
}
return ans;
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
b=sqrt(m + 0.5);
for(int i=1;i<=n;i++){vis[i]=p[i]=dep[i]=0;G[i].clear();} s.clear();
for(int i=0;i<m;i++)
{
scanf("%d%d",&u,&v);dep[u]++,dep[v]++;G[u].emplace_back(v);G[v].emplace_back(u);s.insert(u+1LL*v*base); s.insert(v+1LL*u*base);
}
printf("%lld\n",solve());
}
return 0;
}