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
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
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 4Sample 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<cstdio>
#include<cstring>
#include<cmath>
#include<set>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long LL;
const int bas = 1e5+1;
const int N = 1e5+5;
vector<int>G[N];
set<LL>_hash;
int deg[N],vis[N],bel[N];
int n,m;
void init(){
for(int i=0;i<N;i++){
deg[i]=vis[i]=bel[i]=0;
G[i].clear();
}
_hash.clear();
}
void work(){
int x=sqrt(1.0*m);
LL ans=0,cnt;
for(int a=1;a<=n;a++){
vis[a]=1;
for(int i=0;i<G[a].size();i++)
bel[G[a][i]]=a; //所有从a出发的边bel[b]=a,表示ab边
for(int i=0;i<G[a].size();i++){
int b=G[a][i];
cnt=0;//以bc为公共边的三角形数量
if(vis[b]) continue;
if(deg[b]<=x){
for(int j=0;j<G[b].size();j++){//枚举从b出发的边
int c=G[b][j];
if(bel[c]==a) cnt++;//构成abc三角形
}
}
else{
for(int j=0;j<G[a].size();j++){//从a点出发到达的c点
int c=G[a][j];
if(_hash.find(1LL*b*bas+c)!=_hash.end())//说明bc相连
cnt++;
}
}
ans+=(cnt-1)*cnt/2;
}
}
printf("%lld\n",ans);
}
int main(){
while(scanf("%d%d",&n,&m)!=EOF){
init();
for(int i=0;i<m;i++){
int u,v;
scanf("%d%d",&u,&v);
deg[u]++;
deg[v]++;
G[u].push_back(v);
G[v].push_back(u);
_hash.insert(1LL*u*bas+v);//uv边的哈希值
_hash.insert(1LL*v*bas+u);//vu边的哈希值
}
work();
}
return 0;
}