For this Question: Given an undirected graph, the task is to detect if there is a cycle in the undirected graph or not.The first line of input contains two integers N and M which denotes the no of vertices and no of edges in the graph respectively. 
Next M lines contain space-separated integers u and v denoting that there is a directed edge from u to v.

Constraints:
1 <= N, M <= 1000
0 <= u, v <=  N-1, u != v
There are no self loops or multiple edges.
Print 'Yes' if there is a cycle in the graph otherwise print 'No'Sample Input 1:
4 5
0 1
1 2
2 3
3 0
0 2

Sample Output 1:
Yes

Explanation:
There is a cycle with nodes 0, 1, 2, 3

Sample Input 2:
4 3
0 1
1 2
2 3

Sample Output 2:
No

Explanation:
There is no cycle in this graph, I have written this Solution Code: import java.io.*;
import java.util.*;



class Main {
	static ArrayList<Integer> ar[];
	static boolean st[];
		static boolean status=false;
	public static void main (String[] args) throws Exception {


	BufferedReader rd=new BufferedReader(new InputStreamReader(System.in));
	try{
	String sa[]=rd.readLine().split(" ");
	int n=Integer.parseInt(sa[0]);
	int e=Integer.parseInt(sa[1]);
	st=new boolean[n+1];

	ar=new ArrayList[n+1];
	for(int i=0;i<n;i++)
	ar[i]=new ArrayList<Integer>();
	for(int i=0;i<e;i++)
	{
		sa=rd.readLine().split(" ");
		int u=Integer.parseInt(sa[0]);;
		int v=Integer.parseInt(sa[1]);;
		ar[u].add(v);
		ar[v].add(u);
	}
	dfs(0);
	if(status)
	System.out.println("Yes");
	else
	System.out.println("No");
	}
	catch(Exception e)
	{
	if(status)
	System.out.println("Yes");
	else
	System.out.println("Yes");
	}
	}

static int p=-1;
	static void dfs(int k)
	{



	   try{
	   Stack <Integer> st1=new Stack<>();
	   int l=0;
	   st1.add(k);
       while(!st1.empty())
	   {
		   if(!st1.empty())
		    l=st1.pop();

		   if(st[l])
		   {
			   status=true;
			   return;
		   }
		   st[l]=true;
		   for(int i=0;i<ar[l].size();i++)
		   {
			   if(!st[ar[l].get(i)])
			   st1.push(ar[l].get(i));
		   }
	   }
	   }
	   catch(Exception e)
	   {

	   }
	}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not?