Language: C++

#include<bits/stdc++.h>
clock_t t=clock();
namespace IO
{
	using namespace std;
	char buf[1<<22],Out[1<<22],*p1=buf,*p2=buf;
	int p3=-1,f=0;
	inline void file()
	{
		#ifdef NTFOrz
		freopen("file.in","r",stdin);
		#endif
	}
	inline char getchar()
	{
		return (p1==p2)&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++;
	}
	inline void flush(){fwrite(Out,1,p3+1,stdout),p3=-1;}
	inline void check(){if (p3>(1<<21)) flush();}
	inline void putchar(char a){Out[++p3]=a;check();}
	inline void putline(string s)
	{
		int tmp=p3;
		while (p3-tmp<s.size()) putchar(s[p3-tmp]);
		putchar('\n');
	}
	inline char gc()
	{
		char c=getchar();
		while (!c||c=='\n'||c==' ') c=getchar();
		return c;
	}
	inline void chktime()
	{
		#ifdef NTFOrz
		printf("%dms\n",clock()-t);
		#endif
	}
}
namespace my_std
{
	using namespace std;
	#define getchar IO::getchar
	#define putchar IO::putchar
    #define Re register
    #define rep(i,a,b) for (Re int i=(a);i<=(b);i++)
    #define drep(i,a,b) for (Re int i=(a);i>=(b);i--)
    #define repv(i,v) for (Re int i=0;i<v.size();i++)
    #define go(u) for (Re int i=head[(u)];i;i=e[i].nxt)
    #define writesp(x) write(x),putchar(' '),IO::check()
    #define writeln(x) write(x),putchar('\n'),IO::check()
    #define mem(x,v) memset(x,v,sizeof(x))
    #define pb push_back
    #define fir first
    #define sec second
    #define MP make_pair
    typedef long long ll;
    typedef unsigned long long ull;
    typedef pair<int,int> pii;
    const int INF=0x7fffffff;
    inline int read()
    {
        ll sum=0,f=0;char c=getchar();
        while (!isdigit(c)) f|=(c=='-'),c=getchar();
        while (isdigit(c)) sum=(sum<<1)+(sum<<3)+(c^48),c=getchar();
        return f?-sum:sum;
    }
    void write(int k)
    {
        if (k<0) putchar('-'),k=-k;
        if (k>=10) write(k/10);
        putchar(k%10+48);
    }
	#define templ template<typename T>
    templ inline void chkmax(T &x,T y){if (x<y) x=y;}
    templ inline void chkmin(T &x,T y){if (x>y) x=y;}
}
using namespace my_std;
const int N=100010;
int n,m,ecnt,head[N],size,flag,path,cnt[5],col[N];
ll ans;
struct edge{int to,nxt,dis;}e[N<<1];
inline void add_edge(int u,int v,int w){e[++ecnt].to=v,e[ecnt].nxt=head[u],e[ecnt].dis=w,head[u]=ecnt;}
inline int add(int x,int y){return x+y>=3?x+y-3:x+y;}
void dfs(int u)
{
	size++,cnt[col[u]]++;
	go(u)
	{
		int v=e[i].to;
		path+=(e[i].dis==1);
		if (col[v]==INF)
		{
			col[v]=add(col[u],e[i].dis);
			dfs(v);
		}
		else if (col[v]!=add(col[u],e[i].dis)) flag=1;
	}
}
int main()
{
	IO::file();
	n=read(),m=read();
	rep(i,1,n) col[i]=INF;
	rep(i,1,m)
	{
		int u=read(),v=read();
		add_edge(u,v,1),add_edge(v,u,2);
	}
	rep(i,1,n) if (col[i]==INF)
	{
		size=flag=path=col[i]=0;
		memset(cnt,0,sizeof(cnt));
		dfs(i);
		if (!flag)
		{
			if (cnt[0]&&cnt[1]&&cnt[2]) ans+=1ll*cnt[0]*cnt[1]+1ll*cnt[1]*cnt[2]+1ll*cnt[2]*cnt[0];
			else ans+=path;
		}
		else ans+=1ll*size*size;
	}
	writeln(ans);
	IO::flush();
	IO::chktime();
	return 0;
}

Compiler output:
a.cc: In function 'void dfs(int)':
a.cc:87:9: error: reference to 'size' is ambiguous
   87 |         size++,cnt[col[u]]++;
      |         ^~~~
In file included from /usr/include/c++/14/string:53,
                 from /usr/include/c++/14/bitset:52,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:52,
                 from a.cc:1:
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:80:22: note:                 'int size'
   80 | int n,m,ecnt,head[N],size,flag,path,cnt[5],col[N];
      |                      ^~~~
a.cc:49:31: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   49 |     #define go(u) for (Re int i=head[(u)];i;i=e[i].nxt)
      |                               ^
a.cc:88:9: note: in expansion of macro 'go'
   88 |         go(u)
      |         ^~
a.cc: In function 'int main()':
a.cc:104:13: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
  104 |         rep(i,1,n) col[i]=INF;
      |             ^
a.cc:46:36: note: in definition of macro 'rep'
   46 |     #define rep(i,a,b) for (Re int i=(a);i<=(b);i++)
      |                                    ^
a.cc:105:13: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
  105 |         rep(i,1,m)
      |             ^
a.cc:46:36: note: in definition of macro 'rep'
   46 |     #define rep(i,a,b) for (Re int i=(a);i<=(b);i++)
      |                                    ^
a.cc:110:13: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
  110 |         rep(i,1,n) if (col[i]==INF)
      |             ^
a.cc:46:36: note: in definition of macro 'rep'
   46 |     #define rep(i,a,b) for (Re int i=(a);i<=(b);i++)
      |                                    ^
a.cc:112:17: error: reference to 'size' is ambiguous
  112 |                 size=flag=path=col[i]=0;
      |                 ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:80:22: note:                 'int size'
   80 | int n,m,ecnt,head[N],size,flag,path,cnt[5],col[N];
      |                      ^~~~
a.cc:120:31: error: reference to 'size' is ambiguous
  120 |                 else ans+=1ll*size*size;
      |                               ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:80:22: note:                 'int size'
   80 | int n,m,ecnt,head[N],size,flag,path,cnt[5],col[N];
      |                      ^~~~
a.cc:120:36: error: reference to 'size' is ambiguous
  120 |                 else ans+=1ll*size*size;
      |                                    ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:80:22: note:                 'int size'
   80 | int n,m,ecnt,head[N],size,flag,path,cnt[5],col[N];
      |                      ^~~~