Leetcode 207. 课程表

class Solution {
public:
    struct GraphNode
    {
        int lable;
        vector<GraphNode*> neighbors;
        GraphNode(int x):lable(x){}
    };

    bool DFS_graph(GraphNode* node, vector<int>& visit)
    {
        //正在访问
        visit[node->lable] = 0;
        for(int i=0; i<node->neighbors.size(); ++i)
        {
            if(visit[node->neighbors[i]->lable] == -1)
            {
                //在访问的里面已经返回false就直接返回false了
                if(DFS_graph(node->neighbors[i], visit) == 0)
                {
                    return false;
                }
            }

            // 第一个遇到有环的
            if(visit[node->neighbors[i]->lable] == 0)
            {
                return false;
            }
        }
        //完成了以后
        visit[node->lable] = 1;
        return true;
    }

    bool canFinish(int numCourses, vector<pair<int, int>>& prerequisites) {
        vector<GraphNode*> graph;
        vector<int> visit;//节点状态,-1表示没有访问,0表示正在访问,1表示已经访问
        bool flag = true;
        for(int i=0; i<numCourses; ++i)
        {
            graph.push_back(new GraphNode(i));
        }
        visit.resize(numCourses, -1);

        for(int i=0; i<prerequisites.size(); ++i)
        {
            GraphNode* begin = graph[prerequisites[i].second];
            GraphNode* end = graph[prerequisites[i].first];
            begin->neighbors.push_back(end);
        }

        for(int i=0; i<graph.size(); ++i)
        {
            if(visit[i] == -1 && !DFS_graph(graph[i], visit))
            {
                flag = false;
                break;
            }
        }

        for(int i=0; i<numCourses; ++i)
        {
            delete graph[i];
        }
        return flag;
    }
};
 bool canFinish(int numCourses, vector<pair<int, int>>& prerequisites) {
        vector<GraphNode*> graph;
        vector<int> degree;

        for(int i=0; i<numCourses; ++i)
        {
            graph.push_back(new GraphNode(i));
        }
        degree.resize(numCourses, 0);

        for(int i=0; i<prerequisites.size(); ++i)
        {
            GraphNode* begin = graph[prerequisites[i].second];
            GraphNode* end = graph[prerequisites[i].first];
            begin->neighbors.push_back(end);
            ++degree[prerequisites[i].first];
        }
        queue<GraphNode*> queue;
        for(int i=0; i<numCourses; ++i)
        {
            if(degree[i] == 0)
            {
                queue.push(graph[i]);
            }
        }

        while(!queue.empty())
        {
            auto *node = queue.front();
            queue.pop();
            for(int i=0; i<node->neighbors.size(); ++i)
            {
                --degree[node->neighbors[i]->lable];
                if(degree[node->neighbors[i]->lable] == 0)
                {
                    queue.push(node->neighbors[i]);
                }
            }
        }
        for(int i=0; i<numCourses; ++i )
            delete graph[i];

        for(int i=0; i<degree.size(); ++i)
        {
            if(degree[i])
                return false;
        }
        return true;
    }
};

原文地址:https://www.cnblogs.com/randyniu/p/9350307.html

时间: 2024-08-30 11:11:00

Leetcode 207. 课程表的相关文章

[LeetCode] 207. 课程表(拓扑排序,BFS)

题目 现在你总共有 n 门课需要选,记为?0?到?n-1. 在选修某些课程之前需要一些先修课程.?例如,想要学习课程 0 ,你需要先完成课程 1 ,我们用一个匹配来表示他们: [0,1] 给定课程总量以及它们的先决条件,判断是否可能完成所有课程的学习? 示例 1: 输入: 2, [[1,0]] 输出: true 解释:?总共有 2 门课程.学习课程 1 之前,你需要完成课程 0.所以这是可能的. 示例 2: 输入: 2, [[1,0],[0,1]] 输出: false 解释:?总共有 2 门课程

Leetcode 207 课程表 (拓扑排序,判断有向图环)

定义一个队列Q,把入度为0的结点入队 若Q不为空,则取队首结点,删去所有从该点出发的边,并把这些边所到达结点的入度减一,若某个节点入度减为0,则将它入队 反复进行如上操作,直到队列为空.(当总的入队次数大于节点数时,跳出循环) 如果这时入过队的节点数恰好等于节点总数,则为有向无环图.否则有环. class Solution { public: static const int maxn = 10000; vector<int> gra[maxn]; int co = 0; int count[

LeetCode:课程表II【210】

LeetCode:课程表II[210] 题目描述 现在你总共有 n 门课需要选,记为 0 到 n-1. 在选修某些课程之前需要一些先修课程. 例如,想要学习课程 0 ,你需要先完成课程 1 ,我们用一个匹配来表示他们: [0,1] 给定课程总量以及它们的先决条件,返回你为了学完所有课程所安排的学习顺序. 可能会有多个正确的顺序,你只要返回一种就可以了.如果不可能完成所有课程,返回一个空数组. 示例 1: 输入: 2, [[1,0]] 输出: [0,1] 解释: 总共有 2 门课程.要学习课程 1

leetcode 207. Course Schedule

https://leetcode.com/problems/course-schedule/#/description 图的算法题 题目就是求一堆课程有先修和后修的区别,这些课程能否构成拓扑排序而不矛盾: 应该算是我做的第一个关于图的算法题,一开始毫无思路,看了网上的sloutions,发现这道题还是比较简单的,可以用bfs和dfs都可以做.感觉还是比较典型的关于图算法例题. http://www.cnblogs.com/zmyvszk/p/5573234.html 这个题解讲的还是比较明白的,

Leetcode 630.课程表III

课程表III 这里有 n 门不同的在线课程,他们按从 1 到 n 编号.每一门课程有一定的持续上课时间(课程时间)t 以及关闭时间第 d 天.一门课要持续学习 t 天直到第 d天时要完成,你将会从第 1 天开始. 给出 n 个在线课程用 (t, d) 对表示.你的任务是找出最多可以修几门课. 示例: 输入: [[100, 200], [200, 1300], [1000, 1250], [2000, 3200]] 输出: 3 解释: 这里一共有 4 门课程, 但是你最多可以修 3 门: 首先,

leetcode 207. Course Schedule 课程计划 ---------- java

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 l

LeetCode 207. Course Schedule(拓扑排序)

题目 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

Java for LeetCode 207 Course Schedule 【Unsolved】

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 l

LeetCode 207. Course Schedule(拓扑排序-求有向图中是否存在环)

求有向图中是否有环. 法一:拓扑排序 用一个队列维护所有入度为0的节点,每次弹出一个节点v,查看从v可达的所有节点u; 将u的入读减一,若u的入度此时为0, 则将u加入队列. 在队列为空时,检查所有节点的入度,若所有节点入度都为0, 则存在这样的一个拓扑排序 -- 有向图中不存在环. 代码: class Solution { public: bool canFinish(int numCourses, vector<pair<int, int>>& prerequisite