Language: C++

#include <iostream>
#include <cstring>
#include <vector>
using namespace std;
typedef long long i64;
const int MAXN = 1e5 + 10;
 
i64 answer;
i64 totCnt;
i64 N, M;
i64 check[MAXN];
i64 cnt[3];
 
bool flag;
 
vector<pair<i64, i64> > Adj[MAXN];
 
inline void addEdge(i64 u, i64 v, i64 w)
{
	Adj[u].emplace_back(v, w);
}
 
void DFS(int cur)
{
	cnt[check[cur]] ++;
 
	for(auto x : Adj[cur])
	{
		totCnt += (x.second == 1);
 
		if(check[x.first] != -1)
		{
			if((check[cur] + x.second) % 3 != check[x.first])
				flag = true;
		}
		else
		{
			check[x.first] = (check[cur] + x.second) % 3;
			DFS(x.first);
		}
	}
}
 
int main()
{
	ios::sync_with_stdio(false), cin.tie(nullptr);
 
	cin >> N >> M;
	for (int i = 1; i <= M; i++)
	{
		i64 u, v;
		cin >> u >> v;
		addEdge(u, v, 1);
		addEdge(v, u, 2);
	}
 
	memset(check, -1, sizeof check);
 
	for(int i = 1; i <= N; i++)
	{
		if(check[i] != -1)
			continue;
 
		check[i] = 0, totCnt = 0;
		cnt[0] = cnt[1] = cnt[2] = 0;
		flag = false;
 
		DFS(i);
		if(flag)
			answer += (cnt[0] + cnt[1] + cnt[2]) * (cnt[0] + cnt[1] + cnt[2]);
		else if(!cnt[0] || !cnt[1] || !cnt[2])
			answer += totCnt;
		else
			answer += cnt[0] * cnt[1] + cnt[1] * cnt[2] + cnt[2] * cnt[0];
 
	}
 
	cout << answer << endl ;
 
	return 0;
}
#include <iostream>
#include <cstring>
#include <vector>
using namespace std;
typedef long long i64;
const int MAXN = 1e5 + 10;

i64 answer;
i64 totCnt;
i64 N, M;
i64 check[MAXN];
i64 cnt[3];

bool flag;

vector<pair<i64, i64> > Adj[MAXN];

inline void addEdge(i64 u, i64 v, i64 w)
{
	Adj[u].emplace_back(v, w);
}

void DFS(int cur)
{
	cnt[check[cur]] ++;

	for(auto x : Adj[cur])
	{
		totCnt += (x.second == 1);

		if(check[x.first] != -1)
		{
			if((check[cur] + x.second) % 3 != check[x.first])
				flag = true;
		}
		else
		{
			check[x.first] = (check[cur] + x.second) % 3;
			DFS(x.first);
		}
	}
}

int main()
{
	ios::sync_with_stdio(false), cin.tie(nullptr);

	cin >> N >> M;
	for (int i = 1; i <= M; i++)
	{
		i64 u, v;
		cin >> u >> v;
		addEdge(u, v, 1);
		addEdge(v, u, 2);
	}

	memset(check, -1, sizeof check);

	for(int i = 1; i <= N; i++)
	{
		if(check[i] != -1)
			continue;

		check[i] = 0, totCnt = 0;
		cnt[0] = cnt[1] = cnt[2] = 0;
		flag = false;

		DFS(i);
		if(flag)
			answer += (cnt[0] + cnt[1] + cnt[2]) * (cnt[0] + cnt[1] + cnt[2]);
		else if(!cnt[0] || !cnt[1] || !cnt[2])
			answer += totCnt;
		else
			answer += cnt[0] * cnt[1] + cnt[1] * cnt[2] + cnt[2] * cnt[0];

	}

	cout << answer << endl ;

	return 0;
}
Submission

Compiler output:
a.cc:87:11: error: redefinition of 'const int MAXN'
   87 | const int MAXN = 1e5 + 10;
      |           ^~~~
a.cc:6:11: note: 'const int MAXN' previously defined here
    6 | const int MAXN = 1e5 + 10;
      |           ^~~~
a.cc:89:5: error: redefinition of 'i64 answer'
   89 | i64 answer;
      |     ^~~~~~
a.cc:8:5: note: 'i64 answer' previously declared here
    8 | i64 answer;
      |     ^~~~~~
a.cc:90:5: error: redefinition of 'i64 totCnt'
   90 | i64 totCnt;
      |     ^~~~~~
a.cc:9:5: note: 'i64 totCnt' previously declared here
    9 | i64 totCnt;
      |     ^~~~~~
a.cc:91:5: error: redefinition of 'i64 N'
   91 | i64 N, M;
      |     ^
a.cc:10:5: note: 'i64 N' previously declared here
   10 | i64 N, M;
      |     ^
a.cc:91:8: error: redefinition of 'i64 M'
   91 | i64 N, M;
      |        ^
a.cc:10:8: note: 'i64 M' previously declared here
   10 | i64 N, M;
      |        ^
a.cc:92:5: error: redefinition of 'i64 check [100010]'
   92 | i64 check[MAXN];
      |     ^~~~~
a.cc:11:5: note: 'i64 check [100010]' previously declared here
   11 | i64 check[MAXN];
      |     ^~~~~
a.cc:93:5: error: redefinition of 'i64 cnt [3]'
   93 | i64 cnt[3];
      |     ^~~
a.cc:12:5: note: 'i64 cnt [3]' previously declared here
   12 | i64 cnt[3];
      |     ^~~
a.cc:95:6: error: redefinition of 'bool flag'
   95 | bool flag;
      |      ^~~~
a.cc:14:6: note: 'bool flag' previously declared here
   14 | bool flag;
      |      ^~~~
a.cc:97:25: error: redefinition of 'std::vector<std::pair<long long int, long long int> > Adj [100010]'
   97 | vector<pair<i64, i64> > Adj[MAXN];
      |                         ^~~
a.cc:16:25: note: 'std::vector<std::pair<long long int, long long int> > Adj [100010]' previously declared here
   16 | vector<pair<i64, i64> > Adj[MAXN];
      |                         ^~~
a.cc:99:13: error: redefinition of 'void addEdge(i64, i64, i64)'
   99 | inline void addEdge(i64 u, i64 v, i64 w)
      |             ^~~~~~~
a.cc:18:13: note: 'void addEdge(i64, i64, i64)' previously defined here
   18 | inline void addEdge(i64 u, i64 v, i64 w)
      |             ^~~~~~~
a.cc:104:6: error: redefinition of 'void DFS(int)'
  104 | void DFS(int cur)
      |      ^~~
a.cc:23:6: note: 'void DFS(int)' previously defined here
   23 | void DFS(int cur)
      |      ^~~
a.cc:125:5: error: redefinition of 'int main()'
  125 | int main()
      |     ^~~~
a.cc:44:5: note: 'int main()' previously defined here
   44 | int main()
      |     ^~~~
a.cc:163:1: error: 'Submission' does not name a type
  163 | Submission
      | ^~~~~~~~~~