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

55 篇文章 0 订阅

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) V=(A,B,C,D) and  E=(AB,BC,CD,DA,AC) E=(AB,BC,CD,DA,AC), we call G as an "A-structure". 

It is defined that "A-structure"  G1=V1+E1 G1=V1+E1 and  G2=V2+E2 G2=V2+E2 are same only in the condition that  V1=V2 V1=V2 and  E1=E2 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 2≤n≤105 1mmin(2×105,n(n1)2) 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. 

1u,vn 1≤u,v≤n 

n3×105 ∑n≤3×105, m6×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

题解:

比赛的时候完全不会做。。。后来搜了个大佬的博客: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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值