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从网络摄像头获取rstp视频流并截取图片保存
import cv2 def get_img_from_camera_net(folder_path): cap = cv2.VideoCapture("rtsp://admin:a ...
- 关于java中BigDecimal的简介
关于java中BigDecimal的简介 1.BigDecimal属于大数据,精度极高,不属于基本数据类型,属于java对象(引用数据类型), 这是sun提供的一个类,专门用在财务软件中. 2.注意: ...
- vue 移动端项目切换页面,页面置顶
之前项目是pc端是使用router的方式实现置顶的 //main.js router.afterEach((to, from, next) => { window.scrollTo(0, 0) ...
- Spring MVC面试复习整理
Spring MVC Spring MVC 是Spring Framework 提供的 web 组件 它的实现基于 MVC 的设计模式:Model(模型层).View(视图层).Controller( ...
- mysql8 主从搭建
主:192.168.10.2 从:192.168.10.3 主:1.登录mysql,授权账号,让从数据库可以进行复制. mysql CREATE USER 'repl'@'192.168.10.3' ...
- tomcat unkonwhost
服务器能ping通域名,tomcat死活不行,重启tomcat解决
- CF1092F Tree with Maximum Cost(dfs+dp)
果然我已经菜到被\(div3\)的题虐哭了 qwq 首先看到这个题,一个比较显然的想法就是先从1号点开始\(dfs\)一遍,然后通过一些奇怪的方式,再\(dfs\)一遍得到其他点的贡献. 那么具体应该 ...
- javaweb 入门
java web 我们首先来看一下两种网络服务的常用架构. C/S([Client/Server])架构 B/S架构 (Browser/Server) (这是重点) 程序完全部署在服务器上,用户通过浏 ...
- Java 创建PDF打印小册子
概述 PDF打印小册子是指将PDF格式文档在打印成刊物前需要提前进行的页面排版,以便在打印后装订成册.下面以Java代码展示如何来实现.这里调用Free Spire.PDF for Java中的Pdf ...
- LeetCode:动态规划
动态规划 动态规划永远的神 这部分主要是学习了 labuladong 公众号中对于动态规划的讲解 刷了些 leetcode 题,在此做一些记录,不然没几天就忘光光了 题目 这部分内容直接上题目了,解题 ...