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.

思路:

把课程序号做顶点,把给定的对作为边,就是找图里有没有环。

我自己代码:

bool canFinish(int numCourses, vector<pair<int, int>>& prerequisites) {
bool hasCircle = false; vector<vector<int>> edges(numCourses); //换一种表示图的方式 edges[0]表示顶点0对应的边 后面是所有它指向的顶点
for(int i = ; i < prerequisites.size(); ++i)
edges[prerequisites[i].first].push_back(prerequisites[i].second); bool * isusedv = (bool *)calloc(numCourses, sizeof(bool)); //存储顶点是否使用过
for(int i = ; i < prerequisites.size(); ++i)
{
hasCircle = findCircle(edges, isusedv, prerequisites[i].first);
if(hasCircle) break;
}
     free(isusedv);
return !hasCircle;
} bool findCircle(vector<vector<int>> &edges, bool * isusedv, int vid) //DFS
{
if(isusedv[vid])
return true; //找到了圈
isusedv[vid] = true; //标记该节点为用过
bool hasCircle = false;
for(int i = ; i < edges[vid].size(); ++i)
{
hasCircle |= findCircle(edges, isusedv, edges[vid][i]);
if(hasCircle) break; //一旦找到了圈就返回
}
isusedv[vid] = false;
return hasCircle;
}

大神的代码:

BFS拓扑排序:

一个简单的求拓扑排序的算法:首先要找到任意入度为0的一个顶点,删除它及所有相邻的边,再找入度为0的顶点,以此类推,直到删除所有顶点。顶点的删除顺序即为拓扑排序。

bool canFinish(int numCourses, vector<vector<int>>& prerequisites)
{
vector<unordered_set<int>> matrix(numCourses); // save this directed graph
for(int i = ; i < prerequisites.size(); ++ i)
matrix[prerequisites[i][]].insert(prerequisites[i][]); vector<int> d(numCourses, ); // in-degree
for(int i = ; i < numCourses; ++ i)
for(auto it = matrix[i].begin(); it != matrix[i].end(); ++ it)
++ d[*it]; for(int j = , i; j < numCourses; ++ j)
{
for(i = ; i < numCourses && d[i] != ; ++ i); // find a node whose in-degree is 0 if(i == numCourses) // if not find
return false; d[i] = -;
for(auto it = matrix[i].begin(); it != matrix[i].end(); ++ it)
-- d[*it];
} return true;
}

DFS找环

bool canFinish(int numCourses, vector<vector<int>>& prerequisites)
{
vector<unordered_set<int>> matrix(numCourses); // save this directed graph
for(int i = ; i < prerequisites.size(); ++ i)
matrix[prerequisites[i][]].insert(prerequisites[i][]); unordered_set<int> visited;
vector<bool> flag(numCourses, false);
for(int i = ; i < numCourses; ++ i)
if(!flag[i])
if(DFS(matrix, visited, i, flag))
return false;
return true;
}
bool DFS(vector<unordered_set<int>> &matrix, unordered_set<int> &visited, int b, vector<bool> &flag)
{
flag[b] = true;
visited.insert(b);
for(auto it = matrix[b].begin(); it != matrix[b].end(); ++ it)
if(visited.find(*it) != visited.end() || DFS(matrix, visited, *it, flag))
return true;
visited.erase(b);
return false;
}

【leetcode】Course Schedule(middle)☆的更多相关文章

  1. 【leetcode】Reverse Integer(middle)☆

    Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 总结:处理整数溢出 ...

  2. 【leetcode】Reorder List (middle)

    Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do thi ...

  3. 【leetcode】Word Break (middle)

    Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...

  4. 【leetcode】Rotate List(middle)

    Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...

  5. 【leetcode】Partition List(middle)

    Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...

  6. 【leetcode】Spiral Matrix(middle)

    Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...

  7. 【leetcode】Rotate Image(middle)

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

  8. 【leetcode】Next Permutation(middle)

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  9. 【leetcode】Reverse Bits(middle)

    Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...

随机推荐

  1. nginx TCP 代理& windows傻瓜式安装

    一.下载nginx Windows http://nginx.org/en/download.html 二.解压到目录 三.进入目录并start nginx.exe即可启动 cd d:/java/ng ...

  2. JS keycode 事件响应

    <script language="javascript"> function keyevent(){ if(event.keyCode==13) alert(&quo ...

  3. visual studio2010 “类视图”和“对象浏览器”图标

    “类视图”和“对象浏览器”显示一些图标,这些图标表示代码实体,例如命名空间.类.函数和变量. 下表以图文并茂的形式说明了这些图标. 图标 说明 图标 说明 namespace 方法或函数 类 运算符 ...

  4. 使用基于关系的选择器和伪类选择器创建纯CSS无JavaScript的鼠标移动到上面即可显示的下拉菜单

    html代码: <div class="menu-bar"> <ul> <li> <h3 class="text-warning ...

  5. JSON格式转换(javascript)

    使用ajax从后台抓取数据后,如果有多个值,可以使用json传值. ajax例子如下,在返回的类型里面,可以是文本型(text),JSON格式(json),超文本类型(html),XML文件类型(xm ...

  6. Java设计模式 之 命令模式

    1      从属模式分类 行为性模式 2      命令模式意图 命令模式可将动作的请求者和动作的执行者对象中解耦. 该模式将一个行为操作发起者的请求封装到对象中,该请求由另外一个对象执行. 将动作 ...

  7. HNU 12885 Bad Signal(模拟)

    题目链接:http://acm.hnu.cn/online/?action=problem&type=show&id=12885&courseid=274 解题报告:一共有n个 ...

  8. 在Linux上配置无线网络

    导读 iwconfig是Linux Wireless Extensions(LWE)的用户层配置工具之一.LWE是Linux下对无线网络配置的工具,包括内核的支持.用户层配置工具和驱动接口的支持三部分 ...

  9. ReLU 和sigmoid 函数对比以及droupout

    参考知乎的讨论:https://www.zhihu.com/question/29021768 1.计算简单,反向传播时涉及除法,sigmod求导要比Relu复杂: 2.对于深层网络,sigmod反向 ...

  10. Apache 无法启动

    本人是做前端开发的,对后台程序不太熟悉,也就以前学过一点.net.但现在都忘记的差不多了.最近在公司,经理给了我一个管理工具dedecms,我刚开始看的时候完全不懂这是什么东西,之前都没听说过(本人见 ...