【拓扑】207. Course Schedule && 210. Course Schedule II

207. Course Schedule【拓扑】

There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1. You are given an array prerequisites where prerequisites[i] = [ai, bi] indicates that you must take course bi first if you want to take course ai.

For example, the pair [0, 1], indicates that to take course 0 you have to first take course 1.
Return true if you can finish all courses. Otherwise, return false.

Example 1:

Input: numCourses = 2, prerequisites = [[1,0]]
Output: true
Explanation: There are a total of 2 courses to take.
To take course 1 you should have finished course 0. So it is possible.
Example 2:

Input: numCourses = 2, prerequisites = [[1,0],[0,1]]
Output: false
Explanation: 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.

Constraints:

1 <= numCourses <= 105
0 <= prerequisites.length <= 5000
prerequisites[i].length == 2
0 <= ai, bi < numCourses
All the pairs prerequisites[i] are unique.

class Solution {
    public boolean canFinish(int numCourses, int[][] prerequisites) {
        
        int n=prerequisites.length;
        if(n==0)return true;
       
        int[] inDeg=new int[numCourses];
        PriorityQueue<Integer> myQue=new PriorityQueue<>((n1,n2)->inDeg[n1]-inDeg[n2]);
        ArrayList<Integer>[] myList=new ArrayList[numCourses];

        for(int i=0;i<numCourses;i++){
            myList[i]=new ArrayList<>();
        }



        for(int i=0;i<n;i++){
            int a=prerequisites[i][0];
            int b=prerequisites[i][1];
            inDeg[a]++;
            //inDeg[b]++;
            myList[b].add(a);
        }

        for(int i=0;i<numCourses;i++){
            myQue.add(i);
        }

        while(myQue.size()>0){
            int x=myQue.poll();
            //System.out.println(x);
            if(inDeg[x]!=0)return false;
            for(int y:myList[x]){
                myQue.remove(y);
                inDeg[y]--;
                myQue.add(y);
            }
        }

        return true;
    }
}

写复杂了,没必要用优先队列

BFS

class Solution {
    public boolean canFinish(int numCourses, int[][] prerequisites) {
        
        int n=prerequisites.length;
        if(n==0)return true;
       
        int[] inDeg=new int[numCourses];
        ArrayList<Integer>[] myList=new ArrayList[numCourses];
        LinkedList<Integer> myQue=new LinkedList<>();

        for(int i=0;i<numCourses;i++){
            myList[i]=new ArrayList<>();
        }


        for(int i=0;i<n;i++){
            int a=prerequisites[i][0];
            int b=prerequisites[i][1];
            inDeg[a]++;
            //inDeg[b]++;
            myList[b].add(a);
        }

        for(int i=0;i<numCourses;i++){
            if(inDeg[i]==0)myQue.add(i);
        }

        int num=0;
        while(myQue.size()>0){
            int x=myQue.remove();
            num++;
            for(int y:myList[x]){
                inDeg[y]--;
                if(inDeg[y]==0)myQue.add(y);
            }
        }

        return num==numCourses;
    }
}

210. Course Schedule II

There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1. You are given an array prerequisites where prerequisites[i] = [ai, bi] indicates that you must take course bi first if you want to take course ai.

For example, the pair [0, 1], indicates that to take course 0 you have to first take course 1.
Return the ordering of courses you should take to finish all courses. If there are many valid answers, return any of them. If it is impossible to finish all courses, return an empty array.

Example 1:

Input: numCourses = 2, prerequisites = [[1,0]]
Output: [0,1]
Explanation: 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].
Example 2:

Input: numCourses = 4, prerequisites = [[1,0],[2,0],[3,1],[3,2]]
Output: [0,2,1,3]
Explanation: 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].
Example 3:

Input: numCourses = 1, prerequisites = []
Output: [0]

Constraints:

1 <= numCourses <= 2000
0 <= prerequisites.length <= numCourses * (numCourses - 1)
prerequisites[i].length == 2
0 <= ai, bi < numCourses
ai != bi
All the pairs [ai, bi] are distinct.

class Solution {
    public int[] findOrder(int numCourses, int[][] prerequisites) {
        List<List<Integer>> edge=new ArrayList<>();
        LinkedList<Integer> myQue=new LinkedList<>();
        int[] inDeg=new int[numCourses];

        for(int i=0;i<numCourses;i++){
            edge.add(new ArrayList<>());
        }
        for(int[] p:prerequisites){
            int a=p[0];
            int b=p[1];
            edge.get(b).add(a);
            inDeg[a]++;
        }


        for(int i=0;i<numCourses;i++){
            if(inDeg[i]==0)myQue.add(i);
        }
        ArrayList<Integer> ans=new ArrayList<>();
        while(myQue.size()>0){
            //System.out.println(myQue.size());
            int x=myQue.poll();
            ans.add(x);
            for(int y:edge.get(x)){
                inDeg[y]--;
                if(inDeg[y]==0)myQue.add(y);
            }
        }

        if(ans.size()==numCourses)return ans.stream().mapToInt(Integer::valueOf).toArray();
        else return new int[0];

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 等保三级拓扑图 .vsd 是指符合等保三级安全要求的网络拓扑图文件,通常以 Microsoft Visio 软件的 .vsd 格式保存。 等保三级是我国《信息安全等级保护管理办法》规定的信息安全等级划分标准之一,意在对重要信息系统进行安全保护。拓扑图是描述网络结构和设备之间连接关系的图形表示,通过拓扑图可以了解网络的布局和设备的部署情况。 下载等保三级拓扑图 .vsd 文件,可能有以下几个目的: 1. 了解网络结构:拓扑图可以清晰展示网络的布局和连接关系,下载该文件可以更好地了解网络的结构和组成部分。这对于网络管理员来说非常重要,可以帮助他们精确规划网络的建设和优化。 2. 安全审计与评估:等保三级拓扑图是符合等保三级安全要求的网络结构描述,下载该文件可以用于安全审计和评估。通过分析拓扑图中的设备和连接信息,可以对网络的安全性进行评估,识别潜在的安全风险和漏洞,采取相应的措施进行修复和加固。 3. 案例学习与参考:等保三级拓扑图通常是经过专业安全团队设计和优化的,下载该文件可以作为学习和参考的资料。可以从中学习到一些较好的网络拓扑设计思路和安全措施,对自己的网络安全工作有所借鉴和启发。 总之,下载等保三级拓扑图 .vsd 文件可以帮助我们了解网络结构、进行安全审计与评估以及学习参考。但需要注意的是,下载该文件需要获得相关授权或拥有必要的权限,以确保合法合规。 ### 回答2: 等保三级拓扑图是一种网络安全架构图,用于表示企业或组织的网络体系结构和各个网络安全控制层级之间的关系。这种拓扑图通常以.vsds文件格式进行存储和传输。 .vsds扩展名是Visio绘图软件的文件格式。要下载等保三级拓扑图.vsds文件,首先确保您有合适的绘图软件(如Visio)来打开和查看该文件。接下来,可以通过以下步骤下载所需的.vsds文件: 1. 打开网络浏览器,并进入适当的网站或资源库。 2. 在网站的搜索框中输入“等保三级拓扑图.vsds下载”。 3. 点击搜索按钮,系统将为您显示相关的搜索结果。 4. 筛选出最合适的搜索结果,找到可信赖的文件来源。 5. 点击下载链接,等待下载过程完成。 6. 打开下载后的.vsds文件,使用Visio软件或其他兼容的绘图工具查看和编辑拓扑图。 请注意,.vsds文件可能会很大,因此下载所需的时间可能会根据您的网络速度和所在地区的网络环境而有所差异。另外,确保下载文件的来源可信,并采取适当的网络安全措施,以确保您的计算机和数据的安全。 总之,通过访问适当的网站或资源库,搜索和下载等保三级拓扑图.vsds文件,并使用适当的绘图工具打开和查看该文件,您就可以获取所需的拓扑图。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值