Counting Stars
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 309 Accepted Submission(s): 82
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 .
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
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
题意:求一个无向图有多少个子图包含四个点 五条边
解:就是求有多少个相连的三元环,暴力查找,虽然说是暴力 可是却包含了很多优化方式
将点分成两部分,出度小于sqrt(m)和大于sqrt(m),暴力枚举连接两个三元环的那条边
对于出度小的直接n^3遍历,对于出度大的枚举起点的x的两条边判断是否连通
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<vector>
#include<map>
#include <set>
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5+10;
typedef long long LL;
vector<int>p[N];
set<LL>st;
int vis[N],out[N], link[N];
int main()
{
int m;
LL n;
while(scanf("%lld %d", &n, &m)!=EOF)
{
for(int i=0;i<=n;i++) p[i].clear();
st.clear();
memset(link,0,sizeof(link));
memset(out,0,sizeof(out));
for(int i=0;i<m;i++)
{
LL x, y;
scanf("%lld %lld", &x, &y);
p[x].push_back(y),p[y].push_back(x);
st.insert(x*n+y),st.insert(y*n+x);
out[x]++,out[y]++;
}
int maxt=sqrt(1.0*m);
memset(vis,0,sizeof(vis));
LL ans=0;
int x, y, z;
for(int i=1;i<=n;i++)
{
vis[i]=1,x=i;
for(int j=0;j<p[x].size();j++)
{
y=p[x][j];
link[y]=x;
}
for(int j=0;j<p[x].size();j++)
{
y=p[x][j];
if(vis[y]) continue;
LL sum=0;
if(out[y]<=maxt)
{
for(int k=0;k<p[y].size();k++)
{
int z=p[y][k];
if(link[z]==x) sum++;
}
}
else
{
for(int k=0;k<p[x].size();k++)
{
int z=p[x][k];
if(st.find(z*n+y)!=st.end()) sum++;
}
}
ans=ans+(sum*(sum-1)/2);
}
}
printf("%lld\n",ans);
}
return 0;
}