HDU 6184 Counting Stars(无向图三元环计数)

87 篇文章 0 订阅

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

题目大意:

  有一个有向无环图,求有多少共用一条边的三元环对。

解题思路:

  题目求共用一条边的三元环对数,如果我们可以求出经过每一条边的三元环个数,那么答案就是 eEC2num[e]
  至于每个边上三元环的个数,我们只要把不同的三元环计数修改点,每次找到三元环时更新构成它的三条边的答案。
  三元环计数的时间复杂度为 O(E×E) ,实现方法为,对于每条边只从度数小的一段枚举。然后对于每个点枚举两条边,如果它们直线的点之间有边则构成一个三元环。

AC代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <cmath>
#include <map>
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;i<E;++i)
        cnt[i]=0;
    save[0]=0;
}

int main()
{
    while(~scanf("%d%d", &V, &E))
    {
        init();
        for(int i=0;i<E;++i)
        {
            int 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]<deg[v] || (deg[u]==deg[v] && u<v))//只从度数小的方向枚举
                {
                    save[++save[0]]=v;
                    save_id[save[0]]=i;
                }
            }
            for(int i=1;i<=save[0];++i)
                for(int j=1;j<i;++j)
                {
                    int 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;i<E;++i)
            ans+=cnt[i]*(cnt[i]-1ll)/2ll;
        printf("%lld\n", ans);
    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值