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. 说说移动前端中 viewport (视口)

    转载网络资源的文章:来源不详~~ 移动前端中常说的 viewport (视口)就是浏览器显示页面内容的屏幕区域.其中涉及几个重要概念是 dip ( device-independent pixel 设 ...

  2. VBA 每日文件按日期打包存放

    每天省出1小时,换个好心情 1.判断是否存在tempfolder (过渡文件夹) 2.不存在 则在目标目录下新建文件夹tempfolder 在网上看到一种更简单的方法 if dir("e:\ ...

  3. Java并发包源码学习之AQS框架(一)概述

    AQS其实就是java.util.concurrent.locks.AbstractQueuedSynchronizer这个类. 阅读Java的并发包源码你会发现这个类是整个java.util.con ...

  4. SRM 510 2 250TheAlmostLuckyNumbersDivTwo(数位dp)

    SRM 510 2 250TheAlmostLuckyNumbersDivTwo Problem Statement John and Brus believe that the digits 4 a ...

  5. Jquery中的事件和动画

    在学习Jquery中的过程中我们绝大部分都用到了事件的操作,也可以说事件是Jquery中必不可少的一部分,我们常见的一些事件有单击事件,鼠标事件,键盘事件等等.在Jquery中的学习中为了能使让页面以 ...

  6. H5图像遮罩-遁地龙卷风

    (-1)写在前面 这个idea不是我的,向这位前辈致敬.我用的是chrome49.用到的图片资源在我的百度云盘里http://yun.baidu.com/share/link?shareid=1970 ...

  7. 备份Oracle数据库的脚本

    @echo off goto bakoracle :bakoracle echo. echo ★☆★  自动备份Oracle数据库   ★☆★ echo. set backpath=E:\Oracle ...

  8. .assetbundle 和.unity3d 好处

    .assetbundle 资源文件 .unity3D  场景文件 xml.json 静态存储和 还原 AssetBuddle 优点:减小压缩包.资源更新.分开安装包和数据包.AssetBuddle加密 ...

  9. Android学习笔记(二十一)——实战:程序数据共享

    //此系列博文是<第一行Android代码>的学习笔记,如有错漏,欢迎指正! 我们继续在Database项目的基础上继续开发,通过内容提供器来给它加入外部访问接口.首先将 MyDataba ...

  10. 一颗躁动的心---下决心从SLAM开始,不钻研嵌入式底层了

    在写这个随笔时,北京的外面正在下2016的第一场雪.夜深人尽之时总会考虑一下自己的未来在何方. 长这么大了,我发现我这人始终不能坚定不移的朝着一个方向努力,总是朝三暮四,对学习更是朝令夕改,这造成了我 ...