Topological Sort : Uniqueness Test
Difficulty: Hard

Given a directed, unweighted and acyclic graph, it is required to test the graph, whether in it, an ordering of all the vertices of the graph exists, such that :

* If there is a path from u to v, then v appears after u in the ordering.

is the __only one__ that exists in the graph or __not__.

Print in a single line `YES` or `NO`, according to the answer.  

__Note:__ In the case when no such ordering exists, "NO" is to be printed.



Input Format:
First line has two integers $N$, denoting the number of nodes in the graph and $M$, denoting the number of edges in the graph.  

The next $M$ lines each consist of three space separated integers $x$ $y$, where $x$ and $y$ denote the two nodes between which the directed edge $(x->y)$ exists.  


**Constraints**  
$2 \le N \le 2500$  
$1 \le x,y \le N$   

__ If there are multiple instances of the same edge between two given nodes, it is to be considered as a single edge only.__

Output Format:
Print a single line consisting either `YES` or `NO` according to the question.  

Sample Input:
    6 6
    5 1
    6 1
    6 3
    3 4
    4 2
    5 2

Sample Output:
    NO

Python 3 starter code:


Reference solution (C++):
'#'define pb push_back

using namespace std;

vector<'int'>adj[9000];

bool mat[9000][9000];

void dfs(int s,bool vis[],stack<'int'> &stk)

{
	
    vis[s]=true;
	for(int i=0;i<adj[s].size();i++)
	{
		int v=adj[s][i];
		if(!vis[v])
		{
			dfs(v,vis,stk);
		}
	}
	stk.push(s);
}

bool check(int n,int topo[])

{
	
    for(int i=0;i<n-1;i++)
	{
		int u=topo[i];
		int v=topo[i+1];
		if(!mat[u][v])
		return false;
	}
	return true;
}

int main() 

{
	
    int n,m,i,j,x,y;
	cin>>n>>m;
	for(i=0;i<=n+1;i++)
	for(j=0;j<=n+1;j++)
	mat[i][j]=false;
	for(i=0;i<m;i++)
	{
		cin>>x>>y;
		adj[x].pb(y);
		mat[x][y]=true;
	}
	stack<int>stk;
	bool vis[n+9];
	for(i=0;i<=n+1;i++)
	vis[i]=false;
	for(i=1;i<=n;i++)
	{
		if(!vis[i])
		dfs(i,vis,stk);
	}
	int topo[n+1],idx=0;
	while(!stk.empty())
	{
		topo[idx++]=stk.top();
		stk.pop();
	}
	bool ans=check(n,topo);
	if(ans)
	cout<<"YES\n";
	else
	cout<<"NO\n";
	return 0;
}