207. Course Schedule

本文介绍了一种通过拓扑排序来判断课程安排是否可行的方法。针对给出的课程总数及课程间的先修关系,利用拓扑排序原理检测是否存在环路,进而确定能否完成所有课程的学习。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

Note:
The input prerequisites is a graph represented by a list of edges, not adjacency matrices. Read more about how a graph is represented.

click to show more hints.

Hints:
  1. This problem is equivalent to finding if a cycle exists in a directed graph. If a cycle exists, no topological ordering exists and therefore it will be impossible to take all courses.
  2. Topological Sort via DFS - A great video tutorial (21 minutes) on Coursera explaining the basic concepts of Topological Sort.
  3. Topological sort could also be done via BFS.
题意:给出功课数,以及功课之间的相互制约的先后顺序,判断能否排出有序的上课课表。

思路:拓扑排序。也是有效的检测图中是否有环的方法。

class Solution {
public:
	bool canFinish(int numCourses, vector<pair<int, int>>& prerequisites) {
		vector<vector<int>> graph(numCourses);
		vector<int> indegree(numCourses, 0);
		for (auto edge : prerequisites){
			graph[edge.second].push_back(edge.first);
			indegree[edge.first]++;
		}
		queue<int> Q;
		for (int i = 0; i < numCourses; i++){
			if (indegree[i] == 0)
				Q.push(i);
		}
		int count = 0;
		while (!Q.empty()){
			int u = Q.front();
			Q.pop();
			count++;
			for (auto v : graph[u]){
				--indegree[v];
				if (indegree[v] == 0)
					Q.push(v);
			}
		}
		return count == numCourses;
	}
};

二刷:

第二道是要求输出路径的,根据第二道的代码,简单修改,就是判断结果路径的结点数是否等于课程数,如果不等,就返回false,等于就返回true。

class Solution {
public:
    bool canFinish(int numCourses, vector<pair<int, int>>& prerequisites) {
		vector<int> result;
		vector<int> pushDegree(numCourses, 0);//入度表
		vector<vector<int>> requisties(numCourses);//制约表
		queue<int> myqueue;//0入度队列
		for (auto iter : prerequisites){
			pushDegree[iter.first]++;
			requisties[iter.second].push_back(iter.first);
		}

		for (int i = 0; i < numCourses; i++){
			if (pushDegree[i] == 0)
				myqueue.push(i);
		}
		while (!myqueue.empty()){
			int cur = myqueue.front();
			myqueue.pop();
			result.push_back(cur);
			for (int i = 0; i < requisties[cur].size(); i++){
				pushDegree[requisties[cur][i]]--;
				if (pushDegree[requisties[cur][i]] == 0)
					myqueue.push(requisties[cur][i]);
			}
		}
		if (result.size() != numCourses)
			return false;
		return true;
	}
};












评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值