三元环计数(bitset)

· · 个人记录

#include<cmath>
#include<cstring>
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#include<set>
#include<bitset>
#define int long long
#define reg register
using namespace std;
const int maxn=4e6+5;
inline int read()
{
    int x=0,f=1;
    char ch=getchar();
    while(ch<'0' || ch>'9')
    {
        if(ch=='-')
        f=-1;
        ch=getchar();
    }
    while(ch>='0' && ch<='9')
    {
        x=x*10+ch-'0';
        ch=getchar();
    }
    return x*f;
}
bitset<1505> a[1505],b[1505];
int n,m;
signed main()
{   
    n=read(),m=read();
    for(int i=1;i<=m;i++)
    {
        int x=read(),y=read();
        b[x][y]=1;
        a[y][x]=1;
    }
    int ans=0;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            if(b[i][j])
            {
                ans=ans+(b[j] & a[i]).count();
            }
        }
    }   
    cout<<ans/3<<endl;
    return 0;
}
/*
6 6
1 2
2 3
3 1
4 5
5 6 
6 4
*/