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. 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的更多相关文章
- 【LeetCode】802. Find Eventual Safe States 解题报告(Python)
[LeetCode]802. Find Eventual Safe States 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemi ...
- 802. Find Eventual Safe States
https://leetcode.com/problems/find-eventual-safe-states/description/ class Solution { public: vector ...
- LeetCode 802. Find Eventual Safe States
原题链接在这里:https://leetcode.com/problems/find-eventual-safe-states/ 题目: In a directed graph, we start a ...
- [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. ...
- 【leetcode】802. Find Eventual Safe States
题目如下: 解题思路:本题大多数人采用DFS的方法,这里我用的是另一种方法.我的思路是建立一次初始值为空的safe数组,然后遍历graph,找到graph[i]中所有元素都在safe中的元素,把i加入 ...
- [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. ...
- [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. ...
- 算法与数据结构基础 - 图(Graph)
图基础 图(Graph)应用广泛,程序中可用邻接表和邻接矩阵表示图.依据不同维度,图可以分为有向图/无向图.有权图/无权图.连通图/非连通图.循环图/非循环图,有向图中的顶点具有入度/出度的概念. 面 ...
- 算法与数据结构基础 - 深度优先搜索(DFS)
DFS基础 深度优先搜索(Depth First Search)是一种搜索思路,相比广度优先搜索(BFS),DFS对每一个分枝路径深入到不能再深入为止,其应用于树/图的遍历.嵌套关系处理.回溯等,可以 ...
随机推荐
- 5.Servlet 对象(request-response)
/*ServletResponse*/ /*responese常见应用*/ 1.向客户端输出中文数据 (分别以OutputStream 和 PrintWriter输出) 2.文件下载和中文文件的下载 ...
- redis----Not only Sql 理论
数据存储的瓶颈:(mysql ==>500万数据就已经很慢了) 1 数据量的总大小,一个机器放不下时 2 数据 的索引,一个机器的内存放不下时 3 访问量(读写混合),一个实例不能承受 Redi ...
- 【坑】Java中遍历递归删除List元素
运行环境 idea 2017.1.1 需求背景 需要做一个后台,可以编辑资源列表用于权限管理 资源列表中可以有父子关系,假设根节点为0,以下以(父节点id,子节点id)表示 当编辑某个资源时,需要带出 ...
- 【坑】maven编码配置
错误环境: maven 3.5.0 idea 2017.1.1 错误原因: 由于没有设置统一编码,导致与其他同事开发过程中出现乱码问题 解决方案: 在maven的 pom配置中properties节点 ...
- Computer Vision_2_Active Shape Models:Active Shape Models-Their Training and Application——1995
此为计算机视觉部分,主要侧重在底层特征提取,视频分析,跟踪,目标检测和识别方面等方面. 1. Active Appearance Models 活动表观模型和活动轮廓模型基本思想来源 Snake,现在 ...
- Django的配置模板路径
Django的配置模板路径 找到settings.py 配置静态目录: 注:创建静态文件名就用static 不要用别的. 两个函数. return redirect ('http//:www.b ...
- 2.03_Python网络爬虫Http和Https
一:HTTP和HTTPS HTTP协议(HyperText Transfer Protocol,超文本传输协议):是一种发布和接收 HTML页面的方法,以明文的形式传输,效率高,但是不安全 HTTPS ...
- BAT脚本批量调用Sql执行文件 (SqlServer 数据库)
@echo off & setlocal EnableDelayedExpansion set num=0set INSTANCE_HOSTNAME= 地址set INSTANCE_PORT= ...
- 微信小程序实现连接蓝牙设备跑步APP
背景 微信小程序兴起,有变成超级APP的趋势,通过微信提供的小程序api,可以通过微信调用到手机原生的支持. 目标 通过微信小程序实现来实现跑步类App的功能. 需求分析 跑步类App需要的两个核心的 ...
- vue-重要方法使用
标签属性router-link-exact-active: 当页面正在方位是触发router-link-exact-active 标签router-link to= 使用a标签页面就会跳转,就不是单页 ...