【2017广西邀请赛】hdu 6184 Counting Stars 三元环+set

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
 

题意:

n个点m条边的无向图,问有多少个A-structure

其中A-structure满足V=(A,B,C,D) && E=(AB,BC,CD,DA,AC)



思路:

这东西就是两个有公共边的三元环。所以问题可以退化成求三元环,有个暴力的msqrtm的算法。


遍历每个点x,其相邻的点为y,如果y的度小于sqrt(m),那遍历y相邻的点z,判断x与z是否相连。因为遍历x和y相当于遍历m条边,而y相邻的点不超过sqrt(m),所以复杂度msqrtm;

如果y的度大于sqrt(m),那遍历x相邻的点z,判断y和z是否相连。仔细思考可以看出,这里相当于遍历了所有边的基础上遍历度大于sqrt(m)的点,在因为度大于sqrt(m)的点个数不超过sqrt(m),所以这里的复杂度为msqrtm


判断相连可以用多种方式,最简单的就是map,但这题不能用map,会超内存。用set 的话会超时,但是可以在第一种情况下可以用数组判断来优化。


//
//  main.cpp
//  1003
//
//  Created by zc on 2017/9/8.
//  Copyright © 2017年 zc. All rights reserved.
//

#include <iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<map>
#include<vector>
#include<set>
#define ll long long
using namespace std;
const int N=100005;
int n,m,x,y,du[N],fa[N];
bool v[N];
set<ll>st;
vector<int>r[N];

int main(int argc, const char * argv[]) {
    while(~scanf("%d%d",&n,&m))
    {
        st.clear();
        for(int i=1;i<=n;i++)   r[i].clear(),fa[i]=v[i]=du[i]=0;
        for(int i=0;i<m;i++)
        {
            scanf("%d%d",&x,&y);
            r[x].push_back(y);
            r[y].push_back(x);
            du[x]++;du[y]++;
            st.insert((ll)x*n+y);
            st.insert((ll)y*n+x);
        }
        memset(v,0,sizeof(v));
        ll ans=0;
        for(int i=1;i<=n;i++)
        {
            v[i]=1;
            for(int j=0;j<r[i].size();j++)  fa[r[i][j]]=i;
            for(int j=0;j<r[i].size();j++)
            {
                int u=r[i][j];
                if(v[u])    continue;
                int sum=0;
                if(du[u]<=(int)sqrt(m))
                {
                    for(int k=0;k<r[u].size();k++)
                        if(fa[r[u][k]]==i)    sum++;
                }
                else
                {
                    for(int k=0;k<r[i].size();k++)
                        if(st.find((ll)u*n+r[i][k])!=st.end())    sum++;
                }
                ans+=(ll)sum*(sum-1)/2;
            }
        }
        printf("%lld\n",ans);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值