• LeetCode 210


    一、问题描述

    Description:

    There are a total of n courses you have to take, labeled from 0 to n1.

    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, return the ordering of courses you should take to finish all courses.

    There may be multiple correct orders, you just need to return one of them. If it is impossible to finish all courses, return an empty array.

    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 the correct course order is [0,1]

    4, [[1,0],[2,0],[3,1],[3,2]]

    There are a total of 4 courses to take. To take course 3 you should have finished both courses 1 and 2. Both courses 1 and 2 should be taken after you finished course 0. So one correct course order is [0,1,2,3]. Another correct ordering is[0,2,1,3].


    二、解题报告

    本题是《LeetCode 207 - Course Schedule》的进阶,第 207 题是判断是否可以拓扑排序,而本题是要返回拓扑排序序列,若不能拓扑排序(即存在环),则返回空序列。

    把每个课程看做一个顶点,课程之间的依赖关系看做一条有向边,就构成了一个有向图。而该有向图能不能拓扑排序,取决于有向图中是否存在环。具体见《拓扑排序的介绍及C++实现》。

    代码在《LeetCode 207 - Course Schedule》稍作修改即可:

    /************************图声明************************/
    class Graph
    {
        int V;             // 顶点个数
        list<int> *adj;    // 邻接表
        queue<int> q;      // 维护一个入度为0的顶点的集合
        int* indegree;     // 记录每个顶点的入度
    public:
        vector<int> vec;                // 存放拓扑排序的结果 
        Graph(int V);                   // 构造函数
        ~Graph();                       // 析构函数
        void addEdge(int v, int w);     // 添加边
        bool topological_sort();        // 拓扑排序
    };
    
    /************************图定义************************/
    Graph::Graph(int V)
    {
        this->V = V;
        adj = new list<int>[V];
    
        indegree = new int[V];  // 入度全部初始化为0
        for(int i=0; i<V; ++i)
            indegree[i] = 0;
    }
    
    Graph::~Graph()
    {
        delete [] adj;
        delete [] indegree;
    }
    
    void Graph::addEdge(int v, int w)
    {
        adj[v].push_back(w); 
        ++indegree[w];
    }
    
    bool Graph::topological_sort()
    {
        for(int i=0; i<V; ++i)
            if(indegree[i] == 0)
                q.push(i);         // 将所有入度为0的顶点入队
    
        int count = 0;             // 计数,记录当前已经输出的顶点数 
        while(!q.empty())
        {
            int v = q.front();      // 从队列中取出一个顶点
            q.pop();
    
            vec.push_back(v);        // 输出该顶点
            ++count;
            // 将所有v指向的顶点的入度减1,并将入度减为0的顶点入栈
            list<int>::iterator beg = adj[v].begin();
            for( ; beg!=adj[v].end(); ++beg)
                if(!(--indegree[*beg]))
                    q.push(*beg);   // 若入度为0,则入栈
        }
    
        if(count < V)
            return false;           // 没有输出全部顶点,有向图中有回路
        else
            return true;            // 拓扑排序成功
    }
    
    
    class Solution {
    public:
        vector<int> findOrder(int numCourses, vector<pair<int, int>>& prerequisites) {
    
            Graph g(numCourses);      // 创建图
            for(int i=0; i<prerequisites.size(); ++i)
                g.addEdge(prerequisites[i].second, prerequisites[i].first);
    
            if(g.topological_sort())  // 存在拓扑排序
                return g.vec;
            else
                return vector<int>(); // 不存在拓扑排序
        }
    };





    LeetCode答案源代码:https://github.com/SongLee24/LeetCode


  • 相关阅读:
    VTK 空间几何变换(Transform),平移、旋转和缩放
    VTK 对象绘制 3D坐标轴(vtkCubeAxesActor)
    VTK 颜色映射之颜色插值
    VTK 模型的颜色映射
    VTK 如何不使用qvtkwidget,而直接把vtk出口嵌入qt窗口中
    VTK vtkRenderWindow 与vtkRenderWindowInteractor详解
    Qt 将界面程序封装成动态库DLL
    Qt 界面做成dll以便调用
    Qt 将带界面的程序封装成dll
    Qt 开发的界面程序封装成DLL,在VC中成功调用
  • 原文地址:https://www.cnblogs.com/songlee/p/5738059.html
Copyright © 2020-2023  润新知