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. error connection reset by peer 104

    connection reset by peer的常见原因 1.服务器的并发连接数超过了其承载量,服务器会将其中一些连接关闭:2. errno = 104错误表明你在对一个对端socket已经关闭的的 ...

  2. es string 分词完整示例

    "products_name_en": {            "type":  "string", //字符串类型            ...

  3. Linux特殊权限及ACL权限

    一.SetUID与SGID 只能用于二进制程序,脚本不能设置 执行者需要有该二进制程序的x权限 执行具有SUID权限的二进制程序,那么执行者将具有该二进制程序所有者的权限. 举例来说,/etc/pas ...

  4. python基础:if判断与流程控制案例

    # 1.使用while循环输出1 2 3 4 5 6 8 9 10 count = 0 while count < 11: if count == 7: count += 1 continue ...

  5. 屏蔽恶意IP

    #!/bin/bash cat /var/log/secure | grep Failed | awk -F " " '{print $11}'| sort| uniq -c| a ...

  6. 如何决定使用 HashMap 还是 TreeMap?(未完成)

    如何决定使用 HashMap 还是 TreeMap?(未完成)

  7. marquee标签实现跑马灯效果--无缝滚动

    今天在做微信端的大转盘抽奖时,想把所有用户的抽奖记录做成无缝滚动的效果,无奈我的js功底太差,一时想不出实现的方法,便百度各种相似效果.但无意中发现了一个html标签——<marquee> ...

  8. macOS关闭修改扩展名的提示

    关闭 defaults write com.apple.finder FXEnableExtensionChangeWarning -bool false; killall Finder 开启 def ...

  9. 将网页上指定的表单的数据导入到excel中

    很多时候,我们想要将网页上显示的信息,导入到Excel中,但是很多时候无法下手.可是,这个时候,下面这个例子会帮你大忙了. 将html表单指定内容导出到EXCEL中. <!DOCTYPE HTM ...

  10. React组件式编程Demo-用户的增删改查

    1.项目结构 项目是基于webpack4, 参考 创建基本的webpack4.x项目 2.页面效果 初始化效果