For this Question: Given a directed graph, detect the presence of a cycle in the graph.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.The method should return 1 if there is a cycle else it should return 0.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 4
0 1
1 2
2 3
0 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<ArrayList<Integer>> adj;


    static void graph(int v){
        adj =new ArrayList<>();
        for(int i=0;i<=v;i++) adj.add(new ArrayList<Integer>());
    }


    static void addedge(int u, int v){
        adj.get(u).add(v);
    }

    static boolean dfs(int i, boolean [] vis, int parent[]){
        vis[i] = true; parent[i] = 1;
        for(Integer it: adj.get(i)){
            if(vis[it]==false) {
                if(dfs(it, vis, parent)==true) return true;
            }
            else if(parent[it]==1) return true;
        }
        parent[i] = 0;
        return false;
    }

    static boolean helper(int n){
        boolean vis[] = new boolean [n+1];
        int parent[] = new int[n+1];

        for(int i=0;i<=n;i++){
            if(vis[i]==false){

                if(dfs(i, vis, parent)) return true;
            }
        }

        return false;
    }

    public static void main (String[] args) throws Exception{
       BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String t[] = br.readLine().split(" ");
        int n = Integer.parseInt(t[0]);
        graph(n);
        int e = Integer.parseInt(t[1]);
        for(int i=0;i<e;i++) {
            String num[] = br.readLine().split(" ");
            int u = Integer.parseInt(num[0]);
            int v = Integer.parseInt(num[1]);
            addedge(u, v);
        }
        if(helper(n)) System.out.println("Yes");
        else System.out.println("No");
    }
}, In this Programming Language: Java, Now tell me if this Code is compilable or not?