【LeetCode 207】Course Schedule
2015-06-16 14:46
337 查看
There are a total of n courses you have to take, labeled from
Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair:
Given the total number of courses and a list of prerequisite pairs, is it possible for you to finish all courses?
For example:
There are a total of 2 courses to take. To take course 1 you should have finished course 0. So it is possible.
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.
思路:
此题可抽象为有向图中的环检测问题。常规解法为构建邻接矩阵或邻接链表后通过拓扑排序检测有向图中是否存在环路来解决。若存在环路说明这个小朋友悲剧了,即将面临辍学的危机,否则,小朋友就可以任性的选课直到毕业(然而事实上即使能顺利毕业也并没有什么*用)。
在这里使用了两个set容器数组来构建图(考虑到也许会有重复输入等情况,还有就是懒 - -!),随后进行拓扑排序求得结果。
0to
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.
思路:
此题可抽象为有向图中的环检测问题。常规解法为构建邻接矩阵或邻接链表后通过拓扑排序检测有向图中是否存在环路来解决。若存在环路说明这个小朋友悲剧了,即将面临辍学的危机,否则,小朋友就可以任性的选课直到毕业(然而事实上即使能顺利毕业也并没有什么*用)。
在这里使用了两个set容器数组来构建图(考虑到也许会有重复输入等情况,还有就是懒 - -!),随后进行拓扑排序求得结果。
class Solution { public: bool canFinish(int numCourses, vector<pair<int, int> >& prerequisites) { const int n = numCourses; int cnt = 0; set<int> sets , grap ; queue<int> Queue;//队列,拓扑排序必备 //从给定的输入中构建图,sets[i]表示学习课程i后才能继续学习的后置课程的集合 //grap[i]表示学习课程i所必须的前置课程的集合(入度) vector<pair<int, int> >::iterator it = prerequisites.begin(); for(; it != prerequisites.end(); it++) { sets[(*it).second].insert((*it).first); grap[(*it).first].insert((*it).second); } //将不需要前置课程的课程(入度为0的课程)压入队列 for(int i = 0; i < n; i++) if(grap[i].size() == 0) Queue.push(i); while(!Queue.empty()) { //取出队首的课程 int cur = Queue.front(); Queue.pop(); //遍历当前课程(cur)的后置课程集合 //在每一个后置课程(*it)的前置课程的集合(grap[*it])中去掉当前课程 //若去掉当前课程后该课程的前置课程集合为空,则将其压入队列 set<int>::iterator it = sets[cur].begin(); for(; it != sets[cur].end(); it++) { grap[*it].erase(cur); if(grap[*it].size() == 0) Queue.push(*it); } cnt++;//统计共拓扑出了多少课程 } //若拓扑出的课程数等于总课程数,说明无环,return true,反之,return false return cnt == n ? 1 : 0; } };
相关文章推荐
- ExtJs中多个form情况下指定某个form使能
- Http中的Get与Post
- 自己的五子棋版本1.1
- Mac OS X Yosemite安装Hadoop 2.6记录
- Android文件图片上传的详细讲解(四)---服务端编码
- IOS开发之深拷贝与浅拷贝(mutableCopy与Copy)详解
- 解决两个div之间出现空白条问题
- malloc()与 alloc()区别
- 天气数据API接口记录
- NV12转化为BMP函数
- 图像目标区域质心计算
- java.lang.IllegalAccessError: tried to access method oracle.jdbc.pool.OracleDataSource.getPassword()
- 乱七八糟的笔记
- 2015.06.16,学习,学习笔记-《通过翻译学英语》学习笔记(1)
- php 对中文字符串的处理- 随机取出指定个数的汉字
- mybatis+spring+c3p0+maven+ehcache
- android 色卡参数
- xmpp-iOS-注册和登录
- Android文件图片上传的详细讲解(三)---模式回调类
- oracle standby数据库改变standby_archive_dest路径