无向图中三元环的个数

#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int bas = 1e5+1;
const int maxn = 1e5+5;
vector<int> G[maxn];
int n, m;
//unordered_set<LL> _hash;
set<LL> _hash;
int deg[maxn], vis[maxn];
int bel[maxn];
void init()
{
    for(int i = 1; i <= n; ++i)
    {
        deg[i] = bel[i] = vis[i] = 0;
        G[i].clear();
    }
}
void work()
{
    int x = sqrt(1.0*m);
    LL ans = 0;
    for(int a = 1; a <= n; ++a)
    {
        vis[a] = 1;
        //扫一遍与a相连的所有点,为下面提供O(1)判两点是否存在连边 
        for(int i = 0; i < G[a].size(); ++i)
        bel[G[a][i]] = a;
        for(int i = 0; i < G[a].size(); ++i)
        {
            int b = G[a][i];
            if(vis[b])
                continue;
            if(deg[b] <= x) 
            {
                //如果b度数<=sqrt(m),则枚举b的所有边 
                for(int j = 0; j < G[b].size(); ++j)
                {
                    int c = G[b][j];
                    //printf("ccc %d %d %d\n",c,bel[c],a);
                    if(bel[c] == a)
                    {
                        ++ans;
                     } 
                }
            }
            else
            {
                //如果b度数>sqrt(m),则枚举a的所有边 
                for(int j = 0; j < G[a].size(); ++j)
                {
                    int c = G[a][j];
                    if(_hash.find(1ll*b*bas+c) != _hash.end())
                    {
                        ++ans;
                    }

                }
            }
        }
    }
    //统计后每个三元环的每条边都会被统计一次,所以 应该/3 
    printf("%lld\n", ans/3);
}
int main()
{
    int t, u, v;
    while(~scanf("%d %d", &n, &m))
    {
        init(); _hash.clear();
        for(int i = 1; i <= m; ++i)
        {
            scanf("%d %d", &u, &v);
            ++deg[u], ++deg[v];
            G[u].push_back(v);
            G[v].push_back(u);
            _hash.insert(1ll*u*bas+v);
            _hash.insert(1ll*v*bas+u);
        }
        work();
    }   
    return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值