无向图中三元环的个数

2人阅读 评论(0) 收藏 举报
分类:

#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;
}
查看评论

【神暴力】三元环题解

http://blog.csdn.net/a2520123/article/details/8078884 [题目描述] 给你一个包含n个点,m条边的无向图,求三元环个数....
  • willinglive
  • willinglive
  • 2014-11-18 13:36:45
  • 1168

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

Little A is an astronomy lover, and he has found that the sky was so beautiful! So he is counting ...
  • qq_37497322
  • qq_37497322
  • 2017-09-05 21:12:42
  • 347

无向图求环

1. 判断N结点的无向图G是否有环 假定:结点个数为M,边条数为E 遍历一遍,判断图分为几部分(假定为P部分,即图有 P 个连通分量) 对于每一个连通分量,如果无环则只能是树,即:边数=...
  • flowlayout
  • flowlayout
  • 2016-12-03 16:38:44
  • 223

[真正的单色三角形] BZOJ 3498 PA2009 Cakes

Description给定一个无自环重边的无向图,求这个图的三元环的个数以及补图的三元环个数。 n≤105,m≤105n ≤ 10^5,m ≤ 10^5PS. 可以在这里交Analysis这个东西 ...
  • u014609452
  • u014609452
  • 2017-03-04 23:39:51
  • 824

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

Counting StarsTime Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T...
  • Yasola
  • Yasola
  • 2017-09-05 20:49:24
  • 708

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

Little A is an astronomy lover, and he has found that the sky was so beautiful! So he is counting s...
  • yo_bc
  • yo_bc
  • 2017-09-06 17:19:53
  • 501

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

Counting Stars Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...
  • yjf3151731373
  • yjf3151731373
  • 2017-09-03 22:00:55
  • 437

求无向图中的三元环个数

无向图是个稀疏图,点数n
  • FlushHip
  • FlushHip
  • 2017-04-16 14:36:47
  • 2876

Gym-100342J Triatrip(求有向图三元环个数+bitset应用)

The travel agency “Four Russians” is offering the new service for their clients. Unlike other agenci...
  • yo_bc
  • yo_bc
  • 2017-09-03 15:39:25
  • 270

求简单无向图中环的个数

Question D. A Simple Task time limit per test 3 seconds memory limit per test 256 megab...
  • zhangyuehuan
  • zhangyuehuan
  • 2014-05-03 10:36:18
  • 2630
    个人资料
    持之以恒
    等级:
    访问量: 9339
    积分: 1043
    排名: 4万+
    最新评论