CCCPaste

Untitled

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

class Graph {
    int N;
    List> graph;
    public int[] inDegree, outDegree;

    public Graph(int N) {
        this.N = N;
        graph = new ArrayList<>(N);
        inDegree = new int[N];
        outDegree = new int[N];
        for(int i = 0; i < N; i++) {
            graph.add(new ArrayList<>());
            inDegree[i] = 0;
            outDegree[i] = 0;
        }
    }
    public void addEdge(int a, int b) {
        graph.get(a).add(b);
        outDegree[a]++;
        inDegree[b]++;
    }
    public void deleteNode(int node) {
        for(int i = 0; i < N; i++) {
            if(graph.get(i).contains(node)) {
                graph.get(i).remove(node);
            }
        }
        List ls = graph.get(node);
        graph.remove(ls);
    }
    public void printGraph() {
        for(List ls : graph) {
            for(int i = 0; i < ls.size(); i++) {
                System.out.println(ls.get(i));
            }
        }
    }


}
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        Graph g = new Graph(n);
        for(int i = 0; i < m; i++) {
            int a = sc.nextInt();
            int b = sc.nextInt();
            g.addEdge(a, b);
        }
        List toDelete = new ArrayList<>();
        for(int i = 0; i < n; i++) {
            if(g.outDegree[i] > g.inDegree[i]) {
                toDelete.add(i);
                System.out.println(i + ", ");
            }
        }

        for(int i = 0; i < toDelete.size(); i++) {
            g.deleteNode(toDelete.get(i));
        }
        g.printGraph();

    }
}/*
4
5
0 1
1 2
2 0
0 3
3 2
*/