Counting Stars
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 459 Accepted Submission(s): 115
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.
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 4
Sample Output
1
6
Source
2017ACM/ICPC广西邀请赛-重现赛(感谢广西大学)
Recommend
liuyiding | We have carefully selected several similar problems for you: 6193 6192 6191 6190 6189
有一个有向无环图,求有多少共用一条边的三元环对。
题目求共用一条边的三元环对数,如果我们可以求出经过每一条边的三元环个数,那么答案就是 ∑e∈EC2num[e]
至于每个边上三元环的个数,我们只要把不同的三元环计数修改点,每次找到三元环时更新构成它的三条边的答案。
三元环计数的时间复杂度为 O(E×E‾‾√) ,实现方法为,对于每条边只从度数小的一段枚举。然后对于每个点枚举两条边,如果它们直线的点之间有边则构成一个三元环。
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define LL long long
#define fi first
#define se second
const LL MAXV=200000+3;
const int MAXE=200000+3;
struct HashMap
{
const static int mod=20011;
const static int maxn=500003;
int head[mod];//链表头指针
int next[maxn];//指向链表下一个节点
int size;//当前节点数
int u[maxn], v[maxn] ,val[maxn];//键,值
void clear()
{
size=0;
memset(head,-1,sizeof head);
}
inline void insert(int _u,int _v, int _val)
{
if(_u>_v)
swap(_u, _v);
int p=(_u*MAXV+_v)%mod;//取模后对应的链
u[size]=_u;
v[size]=_v;
val[size]=_val;
next[size]=head[p];
head[p]=size++;
}
int find(int _u, int _v)
{
if(_u>_v)
swap(_u, _v);
int p=(_u*MAXV+_v)%mod;
for(int i=head[p];~i;i=next[i])//沿着链找到目标值
if(u[i]==_u && v[i]==_v)
return val[i];
return -1;//没找到
}
}hm;
struct Edge
{
int from, to, next;
}edge[2*MAXE];
int V, E, deg[MAXV], cnt[MAXE];
int head[MAXV];
int save[MAXV], save_id[MAXV];
void init()//初始化
{
hm.clear();
for(int i=1;i<=V;++i)
{
deg[i]=0;
head[i]=-1;
}
for(int i=0;i0;
save[0]=0;
}
int main()
{
while(~scanf("%d%d", &V, &E))
{
init();
for(int i=0;iint id=i<<1;
int u, v;
scanf("%d%d", &u, &v);
hm.insert(u, v, i);
edge[id].from=u;
edge[id].to=v;
edge[id].next=head[u];
head[u]=id;
id|=1;
edge[id].from=v;
edge[id].to=u;
edge[id].next=head[v];
head[v]=id;
++deg[u];
++deg[v];
}
for(int u=1;u<=V;++u)
{
save[0]=0;
for(int i=head[u];~i;i=edge[i].next)
{
int v=edge[i].to;
if(deg[u]//只从度数小的方向枚举
{
save[++save[0]]=v;
save_id[save[0]]=i;
}
}
for(int i=1;i<=save[0];++i)
for(int j=1;jint id=hm.find(save[i], save[j]);
if(~id)//形成三元环,更新构成它的三条边
{
++cnt[id];
++cnt[save_id[i]>>1];
++cnt[save_id[j]>>1];
}
}
}
LL ans=0;
for(int i=0;i1ll)/2ll;
printf("%lld\n", ans);
}
return 0;
}