Problem:
Score : 1700 points

Problem Statement

We have a grid with N rows and N columns.
The cell at the i-th row and j-th column is denoted (i, j).

Initially, M of the cells are painted black, and all other cells are white.
Specifically, the cells (a_1, b_1), (a_2, b_2), ..., (a_M, b_M) are painted black.

Snuke will try to paint as many white cells black as possible, according to the following rule:

If two cells (x, y) and (y, z) are both black and a cell (z, x) is white for integers 1≤x,y,z≤N, paint the cell (z, x) black.

Find the number of black cells when no more white cells can be painted black.

Constraints

1≤N≤10^5

1≤M≤10^5

1≤a_i,b_i≤N

All pairs (a_i, b_i) are distinct.

Input

The input is given from Standard Input in the following format:

N M
a_1 b_1
a_2 b_2
:
a_M b_M

Output

Print the number of black cells when no more white cells can be painted black.

Sample Input 1

3 2
1 2
2 3

Sample Output 1

3

It is possible to paint one white cell black, as follows:

Since cells (1, 2) and (2, 3) are both black and a cell (3, 1) is white, paint the cell (3, 1) black.

Sample Input 2

2 2
1 1
1 2

Sample Output 2

4

It is possible to paint two white cells black, as follows:

Since cells (1, 1) and (1, 2) are both black and a cell (2, 1) is white, paint the cell (2, 1) black.

Since cells (2, 1) and (1, 2) are both black and a cell (2, 2) is white, paint the cell (2, 2) black.

Sample Input 3

4 3
1 2
1 3
4 4

Sample Output 3

3

No white cells can be painted black.

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include<bits/stdc++.h>
#define LL long long
#define uLL unsigned long long

using namespace std;
const int N=1e5+10;
int rd()
{
    int x=0,w=1;char ch=0;
    while(ch<'0'||ch>'9'){if(ch=='-') w=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+(ch^48);ch=getchar();}
    return x*w;
}
int n,ff[N],sz[N],co[N],sq[N],ts,c[3];
bool v2[N];
int findf(int x){return ff[x]==x?x:ff[x]=findf(ff[x]);}
vector<int> e[N],fe[N];
void adde(int x,int y){e[x].push_back(y),fe[y].push_back(x);}
LL m;
queue<int> q,q2;

int main()
{
    n=rd(),m=rd();
    for(int i=1;i<=n;++i) ff[i]=i;
    for(int i=1;i<=m;++i)
    {
	int x=rd(),y=rd();
	adde(x,y);
	if(findf(x)!=findf(y)) sz[findf(x)]+=sz[findf(y)],ff[findf(y)]=findf(x);
	++sz[findf(x)];
    }
    for(int h=1;h<=n;++h)
	if(findf(h)&&(int)e[h].size()&&(int)fe[h].size())
	{
	    m-=sz[findf(h)];
	    ff[h]=0,ts=0,q.push(h);
	    while(!q.empty())
	    {
		int x=q.front();
		q.pop();
		sq[++ts]=x;
		int nn=e[x].size(),mm=fe[x].size(),sx=e[x][0],tx=fe[x][0];
		for(int i=0;i<nn;++i)
		{
		    int y=e[x][i];
		    if(!(int)e[y].size()) adde(y,tx);
		}
		for(int i=0;i<mm;++i)
		{
		    int y=fe[x][i];
		    if(!(int)fe[y].size()) adde(sx,y);
		}
		for(int i=0;i<nn;++i)
		{
		    int y=e[x][i];
		    if(ff[y]) co[y]=(co[x]+1)%3,ff[y]=0,q.push(y);
		}
		for(int i=0;i<mm;++i)
		{
		    int y=fe[x][i];
		    if(!v2[y]) v2[y]=1,q2.push(y);
		}
		if(q.empty())
		{
		    while(!q2.empty()&&!ff[q2.front()]) q2.pop();
		    if(!q2.empty())
		    {
			int y=q2.front();
			q2.pop();
			mm=fe[y].size();
			for(int j=0;j<mm;++j)
			{
			    int z=fe[y][j];
			    if(!ff[z]){co[y]=(co[z]+1)%3,ff[y]=0,q.push(y);break;}
			}
		    }
		}
	    }
	    bool ok=0;
	    for(int i=1;i<=ts;++i)
	    {
		int x=sq[i],nn=e[x].size();
		for(int j=0;j<nn;++j)
		{
		    int y=e[x][j];
		    ok|=(co[y]-co[x]+3)%3!=1;
		}
	    }
	    if(ok) m+=1ll*ts*ts;
	    else
	    {
		c[0]=c[1]=c[2]=0;
		for(int i=1;i<=ts;++i) ++c[co[sq[i]]];
		m+=1ll*c[0]*c[1]+1ll*c[1]*c[2]+1ll*c[2]*c[0];
	    }
	}
    printf("%lld\n",m);
    return 0;
}