For this Question: There is a directed graph of N nodes and M edges with each node labeled from 0 to N - 1.
A node is a terminal node if there are no outgoing edges. A node is a safe node if every possible path starting from that node leads to a terminal node. Find the number of safe nodes.First line contains two integers N and M denoting the number of nodes and number of edges.
Then M lines follow each containing two integers u and v denoting there is an edge from node u to node v.

Constraints
1 <= N <= 10^4
1<= M <= 10^4
0 <= u,v < N
The graph may contain self- loops.Print the number of safe nodes.Sample Input 1:
7 7
0 1
0 2
1 2
1 3
2 5
3 0
4 5
Output 
4
Explanation:
Nodes 5 and 6 are terminal nodes as there are no outgoing edges from either of them.
Every path starting at nodes 2, 4, 5, and 6 all lead to either node 5 or 6.

Sample Input 2:
5 10
0 1
0 2
0 3
0 4
1 1
1 2
2 3
2 4
3 0
3 4
Output 
1
Explanation:
Only node 4 is a terminal node, and every path starting at node 4 leads to node 4., I have written this Solution Code: import java.io.*;
import java.util.*;

class Main{
    static int eventualSafeNodes(ArrayList<ArrayList<Integer>> graph) {
        //count outgoing edges
        int nr=graph.size();
        int[] outdegree = new int[nr];
        //graph
        Map<Integer, Set<Integer>> map = new HashMap();
        Queue<Integer> queue = new LinkedList();
        List<Integer> res = new LinkedList();
        
        for (int i = 0; i < nr; i++) {
            
            ArrayList<Integer> edges = graph.get(i);
            // make graph
            for (int edge : edges) {
                if (!map.containsKey(edge))
                    map.put(edge, new HashSet());
                
                //map in reverse way
                // 1->0, 2->0, 2-1, 3->1.....
                map.get(edge).add(i);
            }
    
            //no. of outgoing edges
            outdegree[i] = edges.size();
            //safe states
            if (outdegree[i] == 0)
                queue.offer(i);
        }
        
        // System.out.println(map);
        
        while (!queue.isEmpty()) {
            int node = queue.poll();
            
            //edges to node
            if (map.containsKey(node)) {
                
                Set<Integer> release = map.get(node);
                for (int n : release) {
                    //decrease outdegre of edges which can reach node
                    //as we remove node
                    outdegree[n]--;
                    //if some state becomes safe add it to the queue
                    if (outdegree[n] == 0)
                        queue.offer(n);
                }
            }
        }
        
        // we have to return in ascending order
        // so we add safe states at the end
        for (int i = 0; i < outdegree.length; i++) {
            if (outdegree[i] == 0)
                res.add(i);
        }
        return res.size();
    
    }
	public static void main (String[] args)throws IOException {
        BufferedReader br = new BufferedReader (new InputStreamReader(System.in));
        String line = br.readLine();
    	String[] strs = line.trim().split("\\s+");
        int n=Integer.parseInt(strs[0]);
        int m=Integer.parseInt(strs[1]);
        ArrayList<ArrayList<Integer> > g = new ArrayList<ArrayList<Integer> >(n);
        for(int i=0;i<n;i++){
             ArrayList<Integer> x = new ArrayList<Integer>();
             g.add(x);
        }
        for(int i=0;i<m;i++){
            line = br.readLine();
    	    strs = line.trim().split("\\s+");
    	    int x=Integer.parseInt(strs[0]);
    	    int y=Integer.parseInt(strs[1]);
    	    g.get(x).add(y);
        }
        System.out.print(eventualSafeNodes(g));
	}	
}, In this Programming Language: Java, Now tell me if this Code is compilable or not?