There are a total of n courses you have to take, labeled from 0
to n - 1
.
Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: [0,1]
Given the total number of courses and a list of prerequisite pairs, is it possible for you to finish all courses?
For example:
2, [[1,0]]
There are a total of 2 courses to take. To take course 1 you should have finished course 0. So it is possible.
2, [[1,0],[0,1]]
There are a total of 2 courses to take. To take course 1 you should have finished course 0, and to take course 0 you should also have finished course 1. So it is impossible.
典型的拓扑排序。原理也很简单,在一个有向图中,每次找到一个没有前驱节点的节点(也就是入度为0的节点),然后把它指向其他节点的边都去掉,重复这个过程(BFS),直到所有节点已被找到,或者没有符合条件的节点(如果图中有环存在)代码:
class Solution {
public:
bool canFinish(int numCourses, vector<pair<int,int> >&prerequisites){
vector<int>input_degree(numCourses,0);
multimap<int,int>st;
int i,j;
for(i=0;i<prerequisites.size();i++){
input_degree[prerequisites[i].first]++;
st.insert(make_pair(prerequisites[i].second,prerequisites[i].first));
}
for(i=0;i<numCourses;i++){
for(j=0;j<numCourses;j++)
if(input_degree[j]==0)break;
if(j==numCourses)return 0;
input_degree[j]--;
multimap<int,int>::iterator it=st.find(j);
int k=0;
int size=st.count(j);
while(k<size){
input_degree[it->second]--;
k++;
it++;
}
}
return 1;
}
};
210. Course Schedule II
There are a total of n courses you have to take, labeled from 0
to n - 1
.
Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: [0,1]
Given the total number of courses and a list of prerequisite pairs, return the ordering of courses you should take to finish all courses.
There may be multiple correct orders, you just need to return one of them. If it is impossible to finish all courses, return an empty array.
For example:
2, [[1,0]]
There are a total of 2 courses to take. To take course 1 you should have finished course 0. So the correct course order is [0,1]
4, [[1,0],[2,0],[3,1],[3,2]]
There are a total of 4 courses to take. To take course 3 you should have finished both courses 1 and 2. Both courses 1 and 2 should be taken after you finished course 0. So one correct course order is [0,1,2,3]
. Another correct ordering is[0,2,1,3]
.
class Solution {
public:
vector<int> findOrder(int numCourses, vector<pair<int,int> >&prerequisites){
vector<int>res;
vector<int> res1;
if(numCourses==0)return res;
vector<int>input_degree(numCourses,0);
multimap<int,int>st;
int i,j;
for(i=0;i<prerequisites.size();i++)
{
input_degree[prerequisites[i].first]++;
st.insert(pair<int,int>(prerequisites[i].second,prerequisites[i].first));
}
for(i=0;i<numCourses;i++){
for(j=0;j<numCourses;j++)
if(input_degree[j]==0)break;
if(j==numCourses)break;
res.push_back(j);
input_degree[j]--;
multimap<int,int>::iterator it=st.find(j);
int size=st.count(j);int k=0;
while(k<size){input_degree[it->second]--;k++;it++;}
}
return res.size()==numCourses?res:res1;
}
};