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<set>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long LL;
const int N = 1e5+5;
const int base = 1e5+5;
int deg[N],vis[N],bel[N];
vector<int>G[N];
set<LL>_hash;
int n,m;
void init(){
for(int i=0;i<N;i++){
vis[i]=deg[i]=bel[i]=0;
G[i].clear();
}
_hash.clear();
}
void work(){
int x=sqrt(m*1.0);
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连的所有点
for(int i=0;i<G[a].size();i++){ //寻找以ab为公共边的三角形
int b=G[a][i];
cnt=0;
if(vis[b]) continue;
if(deg[b]<=x){
for(int j=0;j<G[b].size();j++){ //寻找与b连的所有点c,如果ac相连,则构成三角形
int c=G[b][j];
if(bel[c]==a) cnt++;
}
}
else{
for(int j=0;j<G[a].size();j++){ //寻找与a连的另一点c
int c=G[a][j];
if(_hash.find(1LL*base*b+c)!=_hash.end()) cnt++; //若bc相连,则构成三角形
}
}
ans+=(cnt-1)*cnt/2;
}
}
printf("%lld\n",ans);
}
int main(){
while(~scanf("%d%d",&n,&m)){
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*base+v);
_hash.insert(1LL*v*base+u);
}
work();
}
return 0;
}