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. 如何在oracle中导入导出dmp数据库文件

    Oracle数据导入导出imp/exp就相当于oracle数据还原与备份.exp命令可以把数据从远程数据库服务器导出到本地的dmp文件,imp命令可以把dmp文件从本地导入到远处的数据库服务器中.利用 ...

  2. 记ubuntu下安装Anaconda

    晚上尝试在ubuntu 16.04版本下安装python的Anaconda3发行版. 从清华源下载的Anaconda3-Linux 64位版本安装包,然后顺利的下一步,下一步.....一切顺利!结果到 ...

  3. P1681 最大正方形 Iand II

    题目描述 在一个n*m的只包含0和1的矩阵里找出一个不包含0的最大正方形,输出边长. 输入输出格式 输入格式: 输入文件第一行为两个整数n,m(1<=n,m<=100),接下来n行,每行m ...

  4. Linux Mint下的conky配置

    最近闲来无事,想把自己的Linux Mint弄的再炫酷点,在桌面上显示一些信息,因为我已经装了Cairo-dock,现在就差这个了,下面简单说下整个流程,首先你得安装conky, sudo apt-g ...

  5. springboot 学习笔记(九)

    springboot整合activemq,实现broker集群部署(cluster) 1.为实现jms高并发操作,需要对activemq进行集群部署,broker cluster就是activemq自 ...

  6. echarts的title和legend重合解决(各种小细节)

    一:关于title与legend重叠 1.重合样子 2.解决办法: legend:{ show: true, top:"6%",//与上方的距离 可百分比% 可像素px }, 3. ...

  7. addin修改启动路径

  8. jquery的trigger和triggerHandler区别

    网上关于这个问题都是抄来抄去的,都没怎么说清楚.所以自己做了个测试,供大家参考指教.首先先看API怎么说的 为了检验一下,编写了一个简单的测试代码,如下: <html lang="en ...

  9. 远程登录事件ID

    4672.4624 删除本机记录 HKEY_CURRENT_USER \ Software\Microsoft  \ Terminal ServerClientDefault: 删除“此电脑\文档”下 ...

  10. spring-autowire机制

    在xml配置文件中,autowire有5种类型,可以在<bean/>元素中使用autowire属性指定 模式                        说明 no            ...