广度搜索(degree)

struct GraphNode{
int label;
vector<GraphNode*> neighbours;
GraphNode(int x):label(x){};
};
class Solution {
public:
bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
vector<GraphNode*> graph;
vector<int> degree;
for(int i = 0 ; i < numCourses;i++){
graph.push_back(new GraphNode(i));
degree.push_back(0);
}
queue<GraphNode*> Q;
for(int i = 0 ;i < prerequisites.size();i++){
GraphNode* first = graph[prerequisites[i][0]];
GraphNode* second = graph[prerequisites[i][1]];
second->neighbours.push_back(first);
degree[prerequisites[i][0]]++;
}
for(int i = 0 ;i < numCourses;i++){
if(degree[i] == 0) Q.push(graph[i]);
}
while(!Q.empty()){
for(int i = 0 ; i < Q.front()->neighbours.size();i++){
degree[Q.front()->neighbours[i]->label]--;
if(degree[Q.front()->neighbours[i]->label] == 0){
Q.push(Q.front()->neighbours[i]);
}
}
Q.pop();
}
for(int i = 0 ; i < numCourses;i++){
delete graph[i];
}
for(int i = 0 ;i < numCourses;i++){
if(degree[i]) return false;
}
return true;
}
};

深度搜索

struct GraphNode{
int label;
vector<GraphNode*> neighbours;
GraphNode(int x):label(x){};
};
class Solution {
public:
bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
vector<GraphNode*> graph;
vector<int> visit;
for(int i = 0 ;i<numCourses;i++){
graph.push_back(new GraphNode(i));
visit.push_back(-1);
}
for(int i = 0; i < prerequisites.size();i++){
graph[prerequisites[i][1]]->neighbours.push_back( graph[prerequisites[i][0]]);
}
for (int i = 0 ; i < numCourses;i++){
if(visit[i] == -1 && DFS_graph(graph[i],visit) == false) return false ;
}
for(int i = 0 ;i < numCourses;i++){
delete graph[i];
}
return true; }
bool DFS_graph(GraphNode* node,vector<int>& visit){
visit[node->label] = 0;
for (int i = 0 ; i < node->neighbours.size();i++){
if(visit[node->neighbours[i]->label] == -1){
if(DFS_graph(node->neighbours[i],visit) == false){
return false;
}
}
else if(visit[node->neighbours[i]->label] == 0) return false;
}
visit[node->label] = 1;
return true;
}
};

Leetcode 课程表 C++ 图的深度搜索和广度搜索练习的更多相关文章

  1. 重新整理数据结构与算法(c#)—— 图的深度遍历和广度遍历[十一]

    参考网址:https://www.cnblogs.com/aoximin/p/13162635.html 前言 简介图: 在数据的逻辑结构D=(KR)中,如果K中结点对于关系R的前趋和后继的个数不加限 ...

  2. AlphaGo论文的译文,用深度神经网络和树搜索征服围棋:Mastering the game of Go with deep neural networks and tree search

    转载请声明 http://blog.csdn.net/u013390476/article/details/50925347 前言: 围棋的英文是 the game of Go,标题翻译为:<用 ...

  3. SDUT 2107 图的深度遍历

    图的深度遍历 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 请定一个无向图,顶点编号从0到 ...

  4. 数据结构之 图论---图的深度遍历( 输出dfs的先后遍历序列 )

    图的深度遍历 Time Limit: 1000MS Memory limit: 65536K 题目描述 请定一个无向图,顶点编号从0到n-1,用深度优先搜索(DFS),遍历并输出.遍历时,先遍历节点编 ...

  5. 数据结构实验之图论二:图的深度遍历(SDUT 2107)(简单DFS)

    题解:图的深度遍历就是顺着一个最初的结点开始,把与它相邻的结点都找到,也就是一直往下搜索直到尽头,然后在顺次找其他的结点. #include <bits/stdc++.h> using n ...

  6. SDUT-2107_图的深度遍历

    数据结构实验之图论二:图的深度遍历 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 请定一个无向图,顶点编号从0到n-1 ...

  7. LeetCode:二叉搜索树中的搜索【700】

    LeetCode:二叉搜索树中的搜索[700] 题目描述 给定二叉搜索树(BST)的根节点和一个值. 你需要在BST中找到节点值等于给定值的节点. 返回以该节点为根的子树. 如果节点不存在,则返回 N ...

  8. 图的深度遍历(C语言)邻接矩阵表示

    知识讲解: 图的遍历分为两种,深度遍历与广度遍历.这里讨论深度遍历. 以上图为例讨论图(图片来自<算法笔记>)的深度遍历: 设图形的顶点数为n. 先从顶点v0开始,用一个数组vis[n]来 ...

  9. Java实现 LeetCode 700 二叉搜索树中的搜索(遍历树)

    700. 二叉搜索树中的搜索 给定二叉搜索树(BST)的根节点和一个值. 你需要在BST中找到节点值等于给定值的节点. 返回以该节点为根的子树. 如果节点不存在,则返回 NULL. 例如, 给定二叉搜 ...

随机推荐

  1. JMeter脚本开发

    什么是jmeter脚本 用户操作系统的动作流程 用户操作系统的请求 类似演戏的剧本 怎么快速开发漂亮的jmeter脚本 准确 快速 漂亮,脚本逻辑清晰,维护性高 脚本开发方案 代理 http代理服务器 ...

  2. @RestController的用法

    我一直都不太理解RESTFUL风格但是先记住一些基本用法在深入吧 ** * * 在服务端应用程序状态和功能可以分成各种资源,每一个资源都使用URL 得到一个唯一的地址,所有资源都共享统一的 * 接口, ...

  3. 『Python』多进程

    Python中的多线程无法利用多核优势,如果想要充分地使用多核CPU的资源(os.cpu_count()查看),在Python中大部分情况需要使用多进程.Python提供了multiprocessin ...

  4. Jmeter压测学习1—第一个项目:登录

    我用的是我们公司的测试环境练习的 网址:http://****:9000/login.html  账号是admin 密码:123456 一.打开Jmeter 找到安装目录:bin->Jmeter ...

  5. disruptor笔记之六:常见场景

    欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...

  6. Java秘诀!Java赋值运算符介绍

    运算符丰富是 Java 语言的主要特点之一,它提供的运算符数量之多,在高级语言中是少见的. Java 语言中的运算符除了具有优先级之外,还有结合性的特点.当一个表达式中出现多种运算符时,执行的先后顺序 ...

  7. Upload-labs通关指南(下) 11-20

    承接上篇,这次我们继续做下半部分. 有些题目有其他做法是针对于windows系统特性的,并不能在linux上奏效,因此不在考虑范围之内. Pass-11 制作图片马直接上传 copy a.jpg /a ...

  8. 利用 CSS Overview 面板重构优化你的网站

    本文将向大家介绍 Chrome 87 开始支持的 CSS Overview Panel,并且介绍如何更好地利用这个面板.通过 CSS Overview Panel,可能可以帮助我们: 更准确(高保真) ...

  9. python filter lambda 的使用

    lambda 匿名函数的使用 >>> a=lambda x : x in "1234567890.," >>> a("asd" ...

  10. 使用vue-cli+webpack搭建vue开发环境

    在这里我真的很开心,好久没有用过博客,今天突然看到了我的博客有不少人看过,虽然没有留下脚印,但是还是激起了我重新拿起博客的信心,感谢大家. 在这里我们需要首先下载node,因为我们要用到npm包下载, ...