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)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

解题说明:

AC代码:

#include<iostream>
#include<cstdio>
#include<string.h>
#include<cstring>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long ll;
const int INF=1e9+7,inf=0x3f;
const int N=1e5+5;
vector<int>ve[N];
set<ll>st;
bool mark[N];
int n,m,link[N];
void init(){
	memset(link,0,sizeof(link));
	memset(mark,false,sizeof(mark));
	for(int i=1;i<=n;i++){
		ve[i].clear();
	}
	st.clear();
}
int main(){
	ios::sync_with_stdio(false);
	int ip1,ip2;ll ans=0;
	while(cin>>n>>m){
		init();
		ans=0;
		int sq=sqrt(m); 
	 	for(int i=1;i<=m;i++){
	 	    cin>>ip1>>ip2;
			ve[ip1].push_back(ip2);
			ve[ip2].push_back(ip1);
			st.insert((ll)ip1*(n+1)+ip2);
			st.insert((ll)ip2*(n+1)+ip1);	
		}
		for(int i=1;i<=n;i++){
			for(int j=0;j<ve[i].size();j++){
				  link[ve[i][j]]=i;
			}
			mark[i]=true;
			for(int j=0;j<ve[i].size();j++){
				ll sum=0;
				int y=ve[i][j];
				if(mark[y])continue;
				if(ve[y].size()<=sq){
					for(int k=0;k<ve[y].size();k++){
						int z=ve[y][k];
						if(link[z]==i)sum++;
					}
				}else{
					for(int k=0;k<ve[i].size();k++){
						int z=ve[i][k]; 
						if(st.find((ll)z*(n+1)+y)!=st.end())sum++;
					}
				}
				ans+=(ll)sum*(sum-1)/2;
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/zxk_hi/article/details/81104272
今日推荐