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. Eclipse中使用tomcat 8服务器初级教程

    Eclipse中使用tomcat容器时,经常遇到的问题是启动不成功,输入localhost:8080报404,本文就是教大家破解这个问题.(不过这是很初级的问题了,大牛勿喷) 步骤 1 Window- ...

  2. HDOJ 4768 Flyer

    二分.... Flyer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  3. 自己总结的USB数据结构及其描述符

    背景: USB理论知识光看着空想总觉着丢三落四,好像哪里没法理解到位,自己做个总结. 正文: 1. USB通信的最基本单位是“包”.如果把“包”肢解的话,可以分为各种“域”(7类,即一串二进制数.每类 ...

  4. Linux 修改主机名 和 ip 映射关系

    1. 修改主机名 vim /etc/sysconfig/network NETWORKING=yes HOSTNAME=hadoop 2. 修改主机名和IP的映射关系 vim /etc/hosts 1 ...

  5. 中国天气预报数据API收集

      {"weatherinfo":{"city":"北京","cityid":"101010100" ...

  6. CentOS-6.5-saltstack-安装

    官方网站:https://www.saltstack.com/ 官方文档   https://docs.saltstack.cn/contents.html GitHub:  https://gith ...

  7. EF初接触01

    自动属性:{get;set} 隐式类型 var, dynamic var:  隐式的类型推断出来,在编译阶段把Var换成对应的实际的类型 所以只应用在编译之间, 在运行阶段是和实际类型意义的 dyna ...

  8. DateEdit和TimeEdit用法

    DateEdit 控件默认情况下,显示的只有日期,没有时间.下面介绍2中日期和时间同时显示的方法: 1.Properties.VistaDisplayMode 为true, 2.Properties. ...

  9. JNI_Android项目中调用.so动态库实现详解

    转自:http://www.yxkfw.com/?p=7223 1. 在Eclipse中创建项目:TestJNI 2. 新创建一个class:TestJNI.java package com.wwj. ...

  10. 17.2---#字棋(CC150)

    牛客网的在线题.思路,比较简单.就是判断一下是否有连起来的1. public static boolean checkWon(int[][] board){ boolean res = false; ...