hdu 6184 Counting Stars(求无向图的三元环数量)

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 .
 

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.

2n105 1mmin(2×105,n(n1)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.

1u,vn

n3×105 , m6×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;
}









  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值