Leetcode 课程表 C++ 图的深度搜索和广度搜索练习
广度搜索(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++ 图的深度搜索和广度搜索练习的更多相关文章
- 重新整理数据结构与算法(c#)—— 图的深度遍历和广度遍历[十一]
参考网址:https://www.cnblogs.com/aoximin/p/13162635.html 前言 简介图: 在数据的逻辑结构D=(KR)中,如果K中结点对于关系R的前趋和后继的个数不加限 ...
- 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,标题翻译为:<用 ...
- SDUT 2107 图的深度遍历
图的深度遍历 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 请定一个无向图,顶点编号从0到 ...
- 数据结构之 图论---图的深度遍历( 输出dfs的先后遍历序列 )
图的深度遍历 Time Limit: 1000MS Memory limit: 65536K 题目描述 请定一个无向图,顶点编号从0到n-1,用深度优先搜索(DFS),遍历并输出.遍历时,先遍历节点编 ...
- 数据结构实验之图论二:图的深度遍历(SDUT 2107)(简单DFS)
题解:图的深度遍历就是顺着一个最初的结点开始,把与它相邻的结点都找到,也就是一直往下搜索直到尽头,然后在顺次找其他的结点. #include <bits/stdc++.h> using n ...
- SDUT-2107_图的深度遍历
数据结构实验之图论二:图的深度遍历 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 请定一个无向图,顶点编号从0到n-1 ...
- LeetCode:二叉搜索树中的搜索【700】
LeetCode:二叉搜索树中的搜索[700] 题目描述 给定二叉搜索树(BST)的根节点和一个值. 你需要在BST中找到节点值等于给定值的节点. 返回以该节点为根的子树. 如果节点不存在,则返回 N ...
- 图的深度遍历(C语言)邻接矩阵表示
知识讲解: 图的遍历分为两种,深度遍历与广度遍历.这里讨论深度遍历. 以上图为例讨论图(图片来自<算法笔记>)的深度遍历: 设图形的顶点数为n. 先从顶点v0开始,用一个数组vis[n]来 ...
- Java实现 LeetCode 700 二叉搜索树中的搜索(遍历树)
700. 二叉搜索树中的搜索 给定二叉搜索树(BST)的根节点和一个值. 你需要在BST中找到节点值等于给定值的节点. 返回以该节点为根的子树. 如果节点不存在,则返回 NULL. 例如, 给定二叉搜 ...
随机推荐
- python日志loguru
文档:https://loguru.readthedocs.io/en/stable/overview.html#installation pip install loguru 使用 基本使用 ##终 ...
- 记一次某网站生产环境CPU忽高忽低故障解决过程
感谢 感谢[一级码农] 的帮助,之前也读了大佬的好多文章,一直在学习中,也没有实际操作过. 这次的过程也是在大佬的指点下完成的. 现象描述 从周六上午开始,陆续收到服务器CPU高的报警短信,到下午已经 ...
- HTML 网页开发、CSS 基础语法——五. 编辑器
- 鸿蒙内核源码分析(中断概念篇) | 海公公的日常工作 | 百篇博客分析OpenHarmony源码 | v43.02
百篇博客系列篇.本篇为: v43.xx 鸿蒙内核源码分析(中断概念篇) | 海公公的日常工作 | 51.c.h .o 硬件架构相关篇为: v22.xx 鸿蒙内核源码分析(汇编基础篇) | CPU在哪里 ...
- VUE自学日志02-应用与组件实例
准备好了吗? 我们刚才简单介绍了 Vue 核心最基本的功能--本教程的其余部分将更加详细地涵盖这些功能以及其它高阶功能,所以请务必读完整个教程! 应用 & 组件实例 创建一个应用实例创建一个应 ...
- The type name or alias SqlServer could not be resolved.Please check your configuration
The type name or alias SqlServer could not be resolved.Please check your configuration file.... 检查一下 ...
- .NET Core 基于Quartz的UI可视化操作组件 GZY.Quartz.MUI 简介
前言 最近在用Quartz做定时任务.虽然很方便,但是Quartz自己貌似是没有UI界面的..感觉操作起来 就很难受.. 查了一下,貌似有个UI组件 不过看了一下文档..直接给我劝退了..太麻烦了 我 ...
- 洛谷4475 巧克力王国(KD-Tree + 维护子树和)
(嘤嘤嘤 又是一个自闭了一晚上的题) qwq果然不是平面上的点的问题,也可以直接用KDTree打暴力 我们对于巧克力直接建kdtree 维护一个\(mx[i],mn[i]\) 但是有一个非常不友好的事 ...
- 用C++实现的数独解题程序 SudokuSolver 2.4 及实例分析
SudokuSolver 2.4 程序实现 本次版本实现了 用C++实现的数独解题程序 SudokuSolver 2.3 及实例分析 里发现的第三个不完全收缩 grp 算法 thirdGreenWor ...
- noj加1乘2平方
广度优先搜索典例 00 题目 描述: 最简单的队列的使用#include <iostream>#include <queue>using namespace std;queue ...