https://leetcode.com/problems/find-eventual-safe-states/description/

class Solution {
public:
vector<bool> visited;
vector<int> mem; // -1 unknown, 0 unsafe, 1 safe.
int n;
vector<int> eventualSafeNodes(vector<vector<int>>& graph) {
n = graph.size();
mem = vector<int>(n, -1);
visited = vector<bool>(n, false);
for (int i = 0; i < n; i++)
dfs(graph, i); vector<int> res;
for (int i = 0; i < n; i++)
if (mem[i] == 1)
res.push_back(i);
return res;
}
bool dfs(vector<vector<int>>& graph, int i) {
// check if i is evetually safe
if (mem[i] != -1) return mem[i] == 1; bool res = true;
if (visited[i]) {  // A loop
res = false;
}
else {
visited[i] = true;
for (auto j : graph[i]) {
if (!dfs(graph, j)) {
res = false;
break;
}
}
// visited[i] = false; // This line is optional, since we've cached the result for i in mem.
}
mem[i] = res;
return res;
}
};

  

802. Find Eventual Safe States的更多相关文章

  1. 【LeetCode】802. Find Eventual Safe States 解题报告(Python)

    [LeetCode]802. Find Eventual Safe States 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemi ...

  2. LC 802. Find Eventual Safe States

    In a directed graph, we start at some node and every turn, walk along a directed edge of the graph.  ...

  3. LeetCode 802. Find Eventual Safe States

    原题链接在这里:https://leetcode.com/problems/find-eventual-safe-states/ 题目: In a directed graph, we start a ...

  4. [LeetCode] 802. Find Eventual Safe States 找到最终的安全状态

    In a directed graph, we start at some node and every turn, walk along a directed edge of the graph.  ...

  5. 【leetcode】802. Find Eventual Safe States

    题目如下: 解题思路:本题大多数人采用DFS的方法,这里我用的是另一种方法.我的思路是建立一次初始值为空的safe数组,然后遍历graph,找到graph[i]中所有元素都在safe中的元素,把i加入 ...

  6. [Swift]LeetCode802. 找到最终的安全状态 | Find Eventual Safe States

    In a directed graph, we start at some node and every turn, walk along a directed edge of the graph.  ...

  7. [LeetCode] Find Eventual Safe States 找到最终的安全状态

    In a directed graph, we start at some node and every turn, walk along a directed edge of the graph.  ...

  8. 算法与数据结构基础 - 图(Graph)

    图基础 图(Graph)应用广泛,程序中可用邻接表和邻接矩阵表示图.依据不同维度,图可以分为有向图/无向图.有权图/无权图.连通图/非连通图.循环图/非循环图,有向图中的顶点具有入度/出度的概念. 面 ...

  9. 算法与数据结构基础 - 深度优先搜索(DFS)

    DFS基础 深度优先搜索(Depth First Search)是一种搜索思路,相比广度优先搜索(BFS),DFS对每一个分枝路径深入到不能再深入为止,其应用于树/图的遍历.嵌套关系处理.回溯等,可以 ...

随机推荐

  1. 【tf.keras】tf.keras加载AlexNet预训练模型

    目录 从 PyTorch 中导出模型参数 第 0 步:配置环境 第 1 步:安装 MMdnn 第 2 步:得到 PyTorch 保存完整结构和参数的模型(pth 文件) 第 3 步:导出 PyTorc ...

  2. EF增删查改加执行存储过程和sql语句,多种方法汇总

    ActionUrl c = new ActionUrl() { ActionName="test", RequestUrl="/123/123", SubTim ...

  3. 基于python3.6的如何爬取百度地图

    先前参考了其他的代码,大多数是python2.7写的,而3.6用的类库以及规则有了很大的变动,所以自己写了一个这样的代码,供给大家参考. def get_station(i): station=[] ...

  4. Git 团队常用命令操作指南

    命令如下: git clone -b <branch name> [remote repository address] 主要就是在clone的时候,后面添加branch的信息. 报错: ...

  5. Mantis-1.3.3 (Ubuntu 16.04)

    平台: Ubuntu 类型: 虚拟机镜像 软件包: mantis-1.3.3 bug tracking commercial devops mantis open-source project man ...

  6. Android商城开发系列(十三)—— 首页热卖商品布局实现

    热卖商品布局效果如下图: 这个布局跟我们上节做的推荐是一样的,也是用LinearLayout和GridView去实现的,新建一个hot_item.xml,代码如下所示: <?xml versio ...

  7. pta 编程题7 List Leaves

    其它pta数据结构编程题请参见:pta 这次的编程作业要求从上到下,从左到右输出一棵树的叶子节点,即树的层序遍历,用队列的方式来实现. 注意enqueue和dequeue函数参数为Queue & ...

  8. IOS Modal(切换另外控件器方式)

    ● 除了push之外,还有另外一种控制器的切换方式,那就是Modal ● 任何控制器都能通过Modal的形式展示出来 ● Modal的默认效果:新控制器从屏幕的最底部往上钻,直到盖住之前的控制器为止 ...

  9. 如何解决EXCEL中的科学计数法

    EXCEL虽然能够有效的处理数据,尤其是数字的计算.但是,在单元格中输入数字的时候,很多时候都会受到科学计算法的困扰. 当单元格中输入的数字,超过11位时,就会自动变成科学计数法.无论您怎么调整列的宽 ...

  10. 2017.12.24 Java序列化你不知道的事(二)

    1 序列化允许重构 序列化允许一定数量的类变种,甚至重构之后也是如此,ObjectInputStream 仍可以很好地将其读出来. Java Object Serialization 规范可以自动管理 ...