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 .
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
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
1 6
题解:
比赛的时候完全不会做。。。后来搜了个大佬的博客:http://blog.csdn.net/jaihk662/article/details/77750832
看懂了几乎照着打了一遍。。。不过我觉得这里面用map会比用set更好理解
做法就是定义一个t为sqrt(m),遍历每一个点,先遍历一次它所有连接的边,将相邻的点打上link标记,然后再遍历一遍,看另一个点的度数是否大于t,小于就用o(n^3)暴力做法遍历该点的所有边看是否可以连回初始点,否则的话就枚举初始点的所有连接的点是否连通。。。然后这里还要用一个组合数学,题目要求的是要两两三元环组成的四元环的个数。。就是每一个点的三元环数目sum*(sum-1)/2,累加就是结果
代码:
#include<iostream>
#include<cstring>
#include<stdio.h>
#include<math.h>
#include<string>
#include<stdio.h>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<deque>
#include<algorithm>
using namespace std;
#define INF 100861111
#define ll long long
#define eps 1e-7
#define lson k*2
#define rson k*2+1
const int maxn=100005;
vector<int>p[maxn];
map<pair<int,int>,int>M;
int vis[maxn],link[maxn],out[maxn];
int main()
{
ll ans,sum;
int n,i,j,m,k,x,y,z,t;
while(scanf("%d%d",&n,&m)!=EOF)
{
t=sqrt(m);
M.clear();
for(i=1;i<=n;i++)
{
p[i].clear();
vis[i]=out[i]=link[i]=0;
}
for(i=1;i<=m;i++)
{
scanf("%d%d",&x,&y);
p[x].push_back(y);
p[y].push_back(x);
out[x]++;
out[y]++;
pair<int,int>temp1=make_pair(x,y);
M[temp1]=1;
pair<int,int>temp2=make_pair(y,x);
M[temp2]=1;
}
ans=0;
for(i=1;i<=n;i++)
{
x=i;
vis[x]=1;
for(j=0;j<p[x].size();j++)
link[p[x][j]]=x;
for(j=0;j<p[x].size();j++)
{
sum=0;
y=p[x][j];
if(vis[y])
continue;
if(out[y]<=t)
{
for(k=0;k<p[y].size();k++)
{
z=p[y][k];
if(link[z]==x)
sum++;
}
}
else
{
for(k=0;k<p[x].size();k++)
{
z=p[x][k];
pair<int,int>temp=make_pair(z,y);
if(M[temp]!=0)
sum++;
}
}
ans+=sum*(sum-1)/2;
}
}
printf("%lld\n",ans);
}
return 0;
}