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 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.

解析

这是一个典型的拓扑排序,判断是否存在环,如果0->1(学1前要学0),并且1->0,则输出false

public static boolean canFinish(int numCourses, int[][] prerequisites) {
        boolean canFinish = true;
        int[] indegree = new int[numCourses];//用来存放第i个结点的入度
        int[][] matrix = new int[numCourses][numCourses];//matrix[i][j],i->j,i是j的先行课

        Stack<Integer> stack = new Stack<Integer>();
        // 初始化邻接表
        for (int i = 0; i < prerequisites.length; i++) {
            if (matrix[prerequisites[i][1]][prerequisites[i][0]] == 1)
                continue;
            indegree[prerequisites[i][0]]++;
            matrix[prerequisites[i][1]][prerequisites[i][0]] = 1;
        }

        //将入度为0的结点入栈
        for(int i=0;i<numCourses;i++){
            if(indegree[i]==0)
                stack.push(i);
        }
        //将入度为0的结点出栈,且将以此节点为条件的结点的入度-1,并将此时为0的结点入栈,直到栈为空
        while(stack.size()>0){
            int node = stack.pop();
            for (int k = 0; k < numCourses; k++) {
                if (matrix[node][k] == 1) {
                    indegree[k]--;
                    matrix[node][k] = 0;
                    if(indegree[k]==0){
                        stack.push(k);
                    }
                }
            }
        }

        //统计是否含有入读大于0的结点
        for (int i = 0; i < numCourses; i++) {
            if (indegree[i] > 0) {
                canFinish = false;
                break;
            }
        }

        return canFinish;
    }
时间: 2024-07-30 03:32:12

Course Schedule 题解的相关文章

poj 1325 Machine Schedule 题解

Machine Schedule Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14479   Accepted: 6172 Description As we all know, machine scheduling is a very classical problem in computer science and has been studied for a very long history. Scheduli

LeetCode编程训练 - 拓扑排序(Topological Sort)

拓扑排序基础 拓扑排序用于解决有向无环图(DAG,Directed Acyclic Graph)按依赖关系排线性序列问题,直白地说解决这样的问题:有一组数据,其中一些数据依赖其他,问能否按依赖关系排序(被依赖的排在前面),或给出排序结果. 最常用解决拓扑排序问题的方法是Kahn算法,步骤可以概括为: 1. 根据依赖关系,构建邻接矩阵或邻接表.入度数组 2. 取入度为0的数据(即不依赖其他数据的数据),根据邻接矩阵/邻接表依次减小依赖其的数据的入度 3. 判断减小后是否有新的入度为0的数据,继续进

2014 百度之星题解 1002 - Disk Schedule

Problem Description 有很多从磁盘读取数据的需求,包括顺序读取.随机读取.为了提高效率,需要人为安排磁盘读取.然而,在现实中,这种做法很复杂.我们考虑一个相对简单的场景. 磁盘有许多轨道,每个轨道有许多扇区,用于存储数据.当我们想在特定扇区来读取数据时,磁头需要跳转到特定的轨道.具体扇区进行读取操作.为了简单,我们假设磁头可以在某个轨道顺时针或逆时针匀速旋转,旋转一周的时间是360个单位时间.磁头也可以随意移动到某个轨道进行读取,每跳转到一个相邻轨道的时间为400个单位时间,跳

题解报告:hdu 4907 Task schedule

Problem Description 有一台机器,并且给你这台机器的工作表,工作表上有n个任务,机器在ti时间执行第i个任务,1秒即可完成1个任务.有m个询问,每个询问有一个数字q,表示如果在q时间有一个工作表之外的任务请求,请计算何时这个任务才能被执行.机器总是按照工作表执行,当机器空闲时立即执行工作表之外的任务请求. Input 输入的第一行包含一个整数T, 表示一共有T组测试数据.对于每组测试数据:第一行是两个数字n, m,表示工作表里面有n个任务, 有m个询问:第二行是n个不同的数字t

POJ1325 Machine Schedule 【二分图最小顶点覆盖】

Machine Schedule Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11958   Accepted: 5094 Description As we all know, machine scheduling is a very classical problem in computer science and has been studied for a very long history. Scheduli

hdu 3572 Task Schedule

Task Schedule Problem Description Our geometry princess XMM has stoped her study in computational geometry to concentrate on her newly opened factory. Her factory has introduced M new machines in order to process the coming N tasks. For the i-th task

Task Schedule(Hdu3572网络流)

跟着风神学图论 Task Schedule(Hdu3572网络流) 题意: 有一个工厂最近新近了一批机器人,这些机器人被用来解决若干个任务.每个任务都有完成所需的时间pi和规定了必须在si天或者si天之后才能处理这个任务,且需要在ei天之内完成. 工厂规定每个机器人每天只能处理一个任务,且每个任务最多只能由一个机器处理. 问这些机器人能否在顺利完成这些任务. 题解: 网络流建模. 对于每一个任务来说,它必须要在si到ei天之间被处理. 并且要在pi天以内完成. 所以我们可以把每一天当成点来考虑,

2014百度之星资格赛题解

比赛链接:点击打开链接 ,,杭电把比赛关了代码都找不到了.. 无责任民科还是mark一下好了.. HDU 4823 Energy Conversion 把式子变换一下发现是一个等比数列,高速幂就可以. #include<stdio.h> #include<iostream> #include<string.h> #include<math.h> using namespace std; #define ll __int64 #define inf 10000

zoj 3538 Arrange the Schedule(矩阵快速幂)

Arrange the Schedule Time Limit: 1 Second      Memory Limit: 65536 KB In Summer 2011, the ZJU-ICPC Team has a n-days training schedule. ZJU-ICPC Team has been divided into 4 Group: Akiba, BiliBili, CIA, Double(Group A, B, C, D). There is a group in c