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. SAP CRM和C4C的内容管理(Content Management)

    SAP CRM内容管理 SAP CRM使用Attachments这个UI给用户提供内容管理的功能.通过新建按钮可以上传本地文档到CRM系统: 该内容管理支持简单的版本管理功能,用户可点击Check O ...

  2. 3.web开发入门知识

    /*web入门*/ /*互联网上常用的协议以及它的端口*/ http 80 http://localhost/    相当于    http://localhost:80/         http协 ...

  3. 【uoj#46】 [清华集训2014] 玄学

      题目传送门:uoj46   题意简述:要求在序列上维护一个操作间支持结合律的区间操作,查询连续一段时间内的操作对单点的作用效果,\(n \leq 10^5,m \leq 6 \times 10^5 ...

  4. Jmeter中间件处理-ActiveMQ

    消息队列是目前的主流中间件,我们在日常测试过程中,无论是接口还是压力测试,都会遇到需要处理这些中间件数据的情况.本文以Activemq的Topic为例,说明如何基于Jmeter实现消息队列数据的发送和 ...

  5. PostScript

    https://baike.baidu.com/item/PostScript/2192822?fr=aladdin PostScript是一种编程语言,最适用于列印图像和文字(无论是在纸.胶片或非物 ...

  6. 学习elasticsearch(一)linux环境搭建(3)——head插件安装

    对于5.x的es,head插件不支持 ./elasticearch-plugin install [plugin_name]方式安装. 进入正文 1.首先确保你的机器安装了python,如果没有,请看 ...

  7. (五)zabbix微信报警

    1.注册微信企业号 1)注册微信企业号 https://work.weixin.qq.com 2)通讯录添加用户 3)记住部门id 4)创建应用 5)点击刚创建的应用,记住Agentld和secret ...

  8. 如何创建vue项目

    Vue项目环境搭建 """ node ~~ python:node是用c++编写用来运行js代码的 npm(cnpm) ~~ pip:npm是一个终端应用商城,可以换国内 ...

  9. C语言位运算题解

    #include <stdio.h> #include <stdlib.h> #include <string.h> //#define NONBLANK 1 ma ...

  10. git log/show/HEAD step(2)

    git log can see all commit log #git logcommit 2737cfa37f81810072f074dcf19964be0a5eea2e (HEAD -> m ...