Counting Stars HDU - 6184(三元环计数)

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.

2n105, 1mmin(2×105,n(n1)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.

1u,vn

n3×105, m6×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

题意:

给一个n个点m条边的无向图,要求统计满足star(四个点五条边的子图)的个数。(2<=n<=1e5, 1<=m<=min(1e5, n*(n-1)/2));

思路:

①统计每个点的度数
②入度<=sqrt(m)的分为第一类,入度>sqrt(m)的分为第二类
③对于第一类,暴力每个点,然后暴力这个点的任意两条边,再判断这两条边的另一个端点是否连接
因为m条边最多每条边遍历一次,然后暴力的点的入度<=sqrt(m),所以复杂度约为O(msqrt(m))
④对于第二类,直接暴力任意三个点,判断这三个点是否构成环,因为这一类点的个数不会超过sqrt(m)个,所以复杂度约为O(sqrt(m)3)=O(msqrt(m))
⑤判断两个点是否连接可以用set,map和Hash都行,根据具体情况而行
这种做法建的是双向边,常数很大

经分析1个star是由两个有一条相同边的不同的三元环构成的,所以问题就是对三元环计数。统计一个边能构成几个三元环,然后ans += C(cnt, 2)即可。

#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long LL;
const int N = 1e5+5;
const int base = 1e5+5;
int deg[N],vis[N],bel[N];
vectorG[N];
set_hash;
int n,m;
void init(){
	for(int i=0;i



你可能感兴趣的:(图论)