In a directed graph, we start at some node and every turn, walk along a directed edge of the graph.  If we reach a node that is terminal (that is, it has no outgoing directed edges), we stop.

Now, say our starting node is eventually safe if and only if we must eventually walk to a terminal node.  More specifically, there exists a natural number K so that for any choice of where to walk, we must have stopped at a terminal node in less than K steps.

Which nodes are eventually safe?  Return them as an array in sorted order.

The directed graph has N nodes with labels 0, 1, ..., N-1, where N is the length of graph.  The graph is given in the following form: graph[i] is a list of labels jsuch that (i, j) is a directed edge of the graph.

Example:
Input: graph = [[1,2],[2,3],[5],[0],[5],[],[]]
Output: [2,4,5,6]
Here is a diagram of the above graph.

Runtime: 268 ms, faster than 12.50% of C++ online submissions for Find Eventual Safe States.

slow

class Solution {
public:
vector<int> eventualSafeNodes(vector<vector<int>>& graph) {
vector<int> indegree(graph.size(),);
vector<int> outdegree(graph.size(), );
unordered_map<int,vector<int>> parent;
for(int i=; i<graph.size(); i++){
for(int j=; j<graph[i].size(); j++){
indegree[graph[i][j]]++;
outdegree[i]++;
parent[graph[i][j]].push_back(i);
}
}
queue<int> q;
unordered_map<int,bool> used;
for(int i=; i<graph.size(); i++) used[i] = false;
while(true) {
for(int i=; i<outdegree.size(); i++) {
if(outdegree[i] == && !used[i]) {
q.push(i);
}
}
if(q.empty()) break;
while(!q.empty()) {
int tmp = q.front(); q.pop();
used[tmp] = true;
for(int x : parent[tmp]) {
outdegree[x]--;
}
}
}
vector<int> ret;
for(int i=; i<outdegree.size(); i++){
if(outdegree[i] == ) ret.push_back(i);
}
return ret;
}
};

Runtime: 140 ms, faster than 100.00% of C++ online submissions for Find Eventual Safe States.

class Solution {

public:
vector<int> eventualSafeNodes(vector<vector<int>>& graph) {
vector<int> color(graph.size(),);
vector<int> ret;
for(int i=; i<graph.size(); i++){
if(dfs(graph, i, color)) ret.push_back(i);
}
return ret;
} bool dfs(vector<vector<int>>& graph, int s, vector<int>& color) {
if(color[s] > ) return color[s] == ;
color[s] = ;
for(int& x : graph[s]) {
if(!dfs(graph, x, color)) return false;
}
color[s] = ;
return true;
}
};

LC 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. 802. Find Eventual Safe States

    https://leetcode.com/problems/find-eventual-safe-states/description/ class Solution { public: vector ...

  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. 15.Filter(过滤器)

    1.管理所有WEB资源:(Jsp, Servlet, 静态图片文件或静态 html 文件等)文件等进行拦截,从而实现一些特殊的功能 2.Filter接口中有一个doFilter方法,当我们编写好Fil ...

  2. Python下载安装及验证

      目录: 一.Python介绍 二.python安装及验证 一.Python介绍 Python是著名的“龟叔”Guido van Rossum在1989年圣诞节期间,为了打发无聊的圣诞节而编写的一个 ...

  3. composer安装学习

    Packagist 镜像 网站地址 http://www.phpcomposer.com/ 请各位使用本镜像的同学注意: 本镜像已经依照 composer 官方的数据源安全策略完全升级并支持 http ...

  4. Redis汇总

    开源项目 https://www.cnblogs.com/yswenli/p/9460527.html

  5. namespace" 或The content of element type "mapper" must match "EMPTY"

    必须为元素类型 "mapper" 声明属性 "namespace" 或The content of element type "mapper" ...

  6. pandas库的一些操作

    1.pd.value_count():带入数值可以计算出value有多少的类别 #得到类别的降序 tips['day'].value_counts(sort=True,ascending=True) ...

  7. Spark学习(4)----ScalaTest

    一.例子: 1.一个简单例子:https://www.jianshu.com/p/ceabf3437dd7 2.Funsuite例子:https://www.programcreek.com/scal ...

  8. 构建官方example

    开发工具的选择: 构建官方example:

  9. python_三元运算符

    三元运算又称三目运算,是对简单的条件语句的简写 简单条件语句: if 条件成立: val = 1 else: val = 2 改成三元运算: val = 1 if 条件成立 else 2 举例: a ...

  10. 服务器上的UID按钮

    定位用的,比如你机柜上有很多台机器,你在前面按下UID灯,机器后面也有一个UID灯会亮起来,这样当你到后面去的时候你就知道刚才在前面看的是哪一台,另外,有人通过ILO远程端口连接到你的服务器的时候,U ...