# HDU 6184 Counting Stars（无向图三元环计数）

Counting Stars

Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 459 Accepted Submission(s): 115

Problem Description
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) and E=(AB,BC,CD,DA,AC), we call G as an “A-structure”.

It is defined that “A-structure” G1=V1+E1 and G2=V2+E2 are same only in the condition that V1=V2 and E1=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≤105, 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≤n

∑n≤3×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

Source
2017ACM/ICPC广西邀请赛-重现赛（感谢广西大学）

Recommend
liuyiding | We have carefully selected several similar problems for you: 6193 6192 6191 6190 6189

### 题目大意：

有一个有向无环图，求有多少共用一条边的三元环对。

### 解题思路：

题目求共用一条边的三元环对数，如果我们可以求出经过每一条边的三元环个数，那么答案就是eEC2num[e]$\sum_{e\in E} C_{num[e]}^{2}$
至于每个边上三元环的个数，我们只要把不同的三元环计数修改点，每次找到三元环时更新构成它的三条边的答案。
三元环计数的时间复杂度为O(E×E)$O(E\times \sqrt{E})$，实现方法为，对于每条边只从度数小的一段枚举。然后对于每个点枚举两条边，如果它们直线的点之间有边则构成一个三元环。

### AC代码：

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <cmath>
#include <map>
using namespace std;
#define LL long long
#define fi first
#define se second

const LL MAXV=200000+3;
const int MAXE=200000+3;

struct HashMap
{
const static int mod=20011;
const static int maxn=500003;
int next[maxn];//指向链表下一个节点
int size;//当前节点数
int u[maxn], v[maxn] ,val[maxn];//键，值
void clear()
{
size=0;
}
inline void insert(int _u,int _v, int _val)
{
if(_u>_v)
swap(_u, _v);
int p=(_u*MAXV+_v)%mod;//取模后对应的链
u[size]=_u;
v[size]=_v;
val[size]=_val;
}
int find(int _u, int _v)
{
if(_u>_v)
swap(_u, _v);
int p=(_u*MAXV+_v)%mod;
if(u[i]==_u && v[i]==_v)
return val[i];
return -1;//没找到
}
}hm;

struct Edge
{
int from, to, next;
}edge[2*MAXE];

int V, E, deg[MAXV], cnt[MAXE];
int save[MAXV], save_id[MAXV];

void init()//初始化
{
hm.clear();
for(int i=1;i<=V;++i)
{
deg[i]=0;
}
for(int i=0;i<E;++i)
cnt[i]=0;
save[0]=0;
}

int main()
{
while(~scanf("%d%d", &V, &E))
{
init();
for(int i=0;i<E;++i)
{
int id=i<<1;
int u, v;
scanf("%d%d", &u, &v);
hm.insert(u, v, i);
edge[id].from=u;
edge[id].to=v;
id|=1;
edge[id].from=v;
edge[id].to=u;
++deg[u];
++deg[v];
}
for(int u=1;u<=V;++u)
{
save[0]=0;
{
int v=edge[i].to;
if(deg[u]<deg[v] || (deg[u]==deg[v] && u<v))//只从度数小的方向枚举
{
save[++save[0]]=v;
save_id[save[0]]=i;
}
}
for(int i=1;i<=save[0];++i)
for(int j=1;j<i;++j)
{
int id=hm.find(save[i], save[j]);
if(~id)//形成三元环，更新构成它的三条边
{
++cnt[id];
++cnt[save_id[i]>>1];
++cnt[save_id[j]>>1];
}
}
}
LL ans=0;
for(int i=0;i<E;++i)
ans+=cnt[i]*(cnt[i]-1ll)/2ll;
printf("%lld\n", ans);
}

return 0;
}