{"id":"95756e10-a295-4e40-ae3f-f6669eb537df","name":"Pepcoding Course Schedule","description":"Pepcoding offers total of n courses labelled from 0 to n-1.\r\n\r\nSome courses may have prerequisites. you have been given m pairs ai,bi. where 1 pair means you must take the course bi before the course ai.\r\n\r\nGiven the total number of courses numCourses and a list of the prerequisite pairs, return the ordering of courses you should take to finish all courses. If it is impossible to finish all courses print -1.","inputFormat":"first line contains 2 numbers n,m wher n represents number of course and m is number of pairs representing prerequisite. Then m lines follow, each line has 2 space separated integers ai , bi.","outputFormat":"Print the ordering of courses you should take to finish all courses.Print the ordering of courses you should take to finish all courses.","constraints":"1 &lt;= numCourses &lt;= 2000\r\n0 &lt;= ai, bi &lt; numCourses\r\nai != bi\r\nAll the pairs ai,bi are distinct.","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\nusing namespace std;\n\n\nvoid findOrder(int numCourses,vector<vector<int>>& graph, vector<int> & ans){\n\t\t\n}\n\nint main(){\n\n\tint n,m;\n\tcin>>n>>m;\n\n\n\tvector<vector<int>> graph(n);\n\n\tfor(int i=0;i<m;i++){\n\t\tint u,v;\n\t\tcin>>u>>v;\n\t\tgraph[v].push_back(u);\n\t}\n\n\tvector<int>ans(n);\n\tfindOrder(n,graph,ans);\n\n\n\n}"},"java":{"code":"import java.io.BufferedReader;\nimport java.io.IOException;\nimport java.io.InputStreamReader;\nimport java.util.ArrayList;\nimport java.util.Arrays;\nimport java.util.Collections;\nimport java.util.LinkedList;\nimport java.util.PriorityQueue;\nimport java.util.Queue;\n\npublic class Main {\n\n\tpublic static void main(String[] args) throws NumberFormatException, IOException {\n\t\tBufferedReader br = new BufferedReader(new InputStreamReader(System.in));\n\t\tString[] st = br.readLine().split(\" \");\n\t\tint n = Integer.parseInt(st[0]);\n\t\tint m = Integer.parseInt(st[1]);\n\n\t\tint[][] prerequisites = new int[m][2];\n\t\tfor (int i = 0; i < m; i++) {\n\t\t\tst = br.readLine().split(\" \");\n\t\t\tprerequisites[i][0] = Integer.parseInt(st[0]);\n\t\t\tprerequisites[i][1] = Integer.parseInt(st[1]);\n\t\t}\n\t\tArrayList<ArrayList<Integer>> graph = new ArrayList<>();\n\t\tfor (int i = 0; i < n; i++) {\n\t\t\tgraph.add(new ArrayList<>());\n\t\t}\n\n\t\tfor (int i = 0; i < prerequisites.length; i++) {\n\t\t\tint u = prerequisites[i][0];\n\t\t\tint v = prerequisites[i][1];\n\n\t\t\tgraph.get(v).add(u);\n\t\t}\n\n\t\tint[] ans = findOrder(n, graph);\n\n\t\tfor (int val : ans) {\n\t\t\tSystem.out.print(val + \" \");\n\t\t}\n\t}\n\n\tpublic static int[] findOrder(int numCourses, ArrayList<ArrayList<Integer>> graph) {\n \n \n\t}\n}"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"4 4\r\n1 0\r\n2 0\r\n3 1\r\n3 2","sampleOutput":"0 1 2 3 ","questionVideo":"","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"7e07fddf-83bd-421e-848f-118f1f29541c","name":"Graphs For Intermediate","slug":"graphs-for-intermediate-493","type":0},{"id":"84faff6c-d8e4-4491-ac85-0ad587da2ee3","name":"Pepcoding Course Schedule","slug":"pepcoding-course-schedule","type":1}],"next":{"id":"bb9cf72f-36e0-4023-a288-fff7058b8172","name":"Pepcoding Course Schedule MCQ","type":0,"slug":"pepcoding-course-schedule-mcq"},"prev":{"id":"0fc5fb50-4256-45f5-a368-9e126bef16b0","name":"Minimum Number of Swaps Required to Sort an Array","type":3,"slug":"minimum-number-of-swaps-required-to-sort-an-array"}}}

Pepcoding Course Schedule

Pepcoding offers total of n courses labelled from 0 to n-1. Some courses may have prerequisites. you have been given m pairs ai,bi. where 1 pair means you must take the course bi before the course ai. Given the total number of courses numCourses and a list of the prerequisite pairs, return the ordering of courses you should take to finish all courses. If it is impossible to finish all courses print -1.

{"id":"95756e10-a295-4e40-ae3f-f6669eb537df","name":"Pepcoding Course Schedule","description":"Pepcoding offers total of n courses labelled from 0 to n-1.\r\n\r\nSome courses may have prerequisites. you have been given m pairs ai,bi. where 1 pair means you must take the course bi before the course ai.\r\n\r\nGiven the total number of courses numCourses and a list of the prerequisite pairs, return the ordering of courses you should take to finish all courses. If it is impossible to finish all courses print -1.","inputFormat":"first line contains 2 numbers n,m wher n represents number of course and m is number of pairs representing prerequisite. Then m lines follow, each line has 2 space separated integers ai , bi.","outputFormat":"Print the ordering of courses you should take to finish all courses.Print the ordering of courses you should take to finish all courses.","constraints":"1 &lt;= numCourses &lt;= 2000\r\n0 &lt;= ai, bi &lt; numCourses\r\nai != bi\r\nAll the pairs ai,bi are distinct.","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\nusing namespace std;\n\n\nvoid findOrder(int numCourses,vector<vector<int>>& graph, vector<int> & ans){\n\t\t\n}\n\nint main(){\n\n\tint n,m;\n\tcin>>n>>m;\n\n\n\tvector<vector<int>> graph(n);\n\n\tfor(int i=0;i<m;i++){\n\t\tint u,v;\n\t\tcin>>u>>v;\n\t\tgraph[v].push_back(u);\n\t}\n\n\tvector<int>ans(n);\n\tfindOrder(n,graph,ans);\n\n\n\n}"},"java":{"code":"import java.io.BufferedReader;\nimport java.io.IOException;\nimport java.io.InputStreamReader;\nimport java.util.ArrayList;\nimport java.util.Arrays;\nimport java.util.Collections;\nimport java.util.LinkedList;\nimport java.util.PriorityQueue;\nimport java.util.Queue;\n\npublic class Main {\n\n\tpublic static void main(String[] args) throws NumberFormatException, IOException {\n\t\tBufferedReader br = new BufferedReader(new InputStreamReader(System.in));\n\t\tString[] st = br.readLine().split(\" \");\n\t\tint n = Integer.parseInt(st[0]);\n\t\tint m = Integer.parseInt(st[1]);\n\n\t\tint[][] prerequisites = new int[m][2];\n\t\tfor (int i = 0; i < m; i++) {\n\t\t\tst = br.readLine().split(\" \");\n\t\t\tprerequisites[i][0] = Integer.parseInt(st[0]);\n\t\t\tprerequisites[i][1] = Integer.parseInt(st[1]);\n\t\t}\n\t\tArrayList<ArrayList<Integer>> graph = new ArrayList<>();\n\t\tfor (int i = 0; i < n; i++) {\n\t\t\tgraph.add(new ArrayList<>());\n\t\t}\n\n\t\tfor (int i = 0; i < prerequisites.length; i++) {\n\t\t\tint u = prerequisites[i][0];\n\t\t\tint v = prerequisites[i][1];\n\n\t\t\tgraph.get(v).add(u);\n\t\t}\n\n\t\tint[] ans = findOrder(n, graph);\n\n\t\tfor (int val : ans) {\n\t\t\tSystem.out.print(val + \" \");\n\t\t}\n\t}\n\n\tpublic static int[] findOrder(int numCourses, ArrayList<ArrayList<Integer>> graph) {\n \n \n\t}\n}"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"4 4\r\n1 0\r\n2 0\r\n3 1\r\n3 2","sampleOutput":"0 1 2 3 ","questionVideo":"","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"7e07fddf-83bd-421e-848f-118f1f29541c","name":"Graphs For Intermediate","slug":"graphs-for-intermediate-493","type":0},{"id":"84faff6c-d8e4-4491-ac85-0ad587da2ee3","name":"Pepcoding Course Schedule","slug":"pepcoding-course-schedule","type":1}],"next":{"id":"bb9cf72f-36e0-4023-a288-fff7058b8172","name":"Pepcoding Course Schedule MCQ","type":0,"slug":"pepcoding-course-schedule-mcq"},"prev":{"id":"0fc5fb50-4256-45f5-a368-9e126bef16b0","name":"Minimum Number of Swaps Required to Sort an Array","type":3,"slug":"minimum-number-of-swaps-required-to-sort-an-array"}}}
plane

Editor


Loading...

Pepcoding Course Schedule

easy

Pepcoding offers total of n courses labelled from 0 to n-1. Some courses may have prerequisites. you have been given m pairs ai,bi. where 1 pair means you must take the course bi before the course ai. Given the total number of courses numCourses and a list of the prerequisite pairs, return the ordering of courses you should take to finish all courses. If it is impossible to finish all courses print -1.

Constraints

1 <= numCourses <= 2000 0 <= ai, bi < numCourses ai != bi All the pairs ai,bi are distinct.

Format

Input

first line contains 2 numbers n,m wher n represents number of course and m is number of pairs representing prerequisite. Then m lines follow, each line has 2 space separated integers ai , bi.

Output

Print the ordering of courses you should take to finish all courses.Print the ordering of courses you should take to finish all courses.

Example

Sample Input

4 4 1 0 2 0 3 1 3 2

Sample Output

0 1 2 3

Discussions

Show Discussion

Related Resources

related resources

Turning Off Zen Mode