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. 例如, 给定二叉搜 ...
随机推荐
- Fillder抓包配置
Faillder设置,完成以下设置后重启Fillder Fillder工具配置 设置端口 端口设置 (根据公司限制使用范围内的端口) 设置是否远程连接 勾选Decrypt HTTPS traffic ...
- Hive语法及其进阶(一)
1.Hive完整建表 1 CREATE [EXTERNAL] TABLE [IF NOT EXISTS] table_name( 2 [(col_name data_type [COMMENT col ...
- Win10下部署VS+Qt+OpenCV+darknet(YOLO)集成环境
VS+Qt集成环境 下载VS与Qt并安装. VS:官网链接 Qt:下载链接 将Qt的bin目录加入PATH路径 bin目录举例:D:\development\Qt\5.12.0\msvc2017_64 ...
- 获取classpath(src/main/resources)的绝对路径
先贴上代码 private static String basePath = Thread.currentThread().getContextClassLoader().getResource(&q ...
- 前端开发3年了,竟然不知道什么是 Vue 脚手架?(下)
上一篇文章<前端开发3年了,竟然不知道什么是 Vue 脚手架?(上)>介绍了什么是脚手架,以及Vue-cli 2.x如何创建项目,创建的项目结构.这篇文章介绍 Vue-cli 3.x 如何 ...
- 如何用redis统计海量UV?
前言 我们先思考一个常见的业务问题:如果你负责开发维护一个大型的网站,有一天老板找产品经理要网站每个网页每天的 UV 数据,然后让你来开发这个统计模块,你会如何实现? 统计uv的常用方法以及优缺点 其 ...
- Python爬取 | 唯美女生图片
这里只是代码展示,且复制后不能直接运行,需要配置一些设置才行,具体请查看下方链接介绍: Python爬取 | 唯美女生图片 from selenium import webdriver from fa ...
- QQ三国 秘制机簧去哪打?打的太慢?
我在完成这个任务时卡了很久,因为打的效率极低,因此最后我是如何完成的. 1. 先说打谁吧,刚开始我打机簧蜘蛛,就没打出来过,,后来换了机簧车,掉率就上升了,建议打机簧车. 2. 如果你一直打不出来,建 ...
- NOIP模拟77
前言 感觉最近太飘了,这次考试是挺好的一次打击(好像也不算是). 犯了一个智障错误(双向边一倍数组 100pts->30pts)别的就.. T1 最大或 解题思路 一开始我以为是一个找规律,然而 ...
- allure报告中allure.title 如何去掉后方的参数化显示
1.解决方法如下 listener.py 文件位置:Lib\site-packages\allure_pytest\listener.py (第三方包所在的LIb目录) 将下图中红色部分test_re ...