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对每一个分枝路径深入到不能再深入为止,其应用于树/图的遍历.嵌套关系处理.回溯等,可以 ...
随机推荐
- 3.IOC的配置与应用(annotation的方式)
自动装载 @Autowired public class UserService { private UserDAO userDAO; public UserDAO getUserDAO() { re ...
- C# Winfrom 自定义控件添加图标
Winfrom自定义控件添加自定义图标实现方式: 1.新建UserControl——略 2.寻找合适的图标文件——将文件和控件放置同一目录下(相同目录.自定义控件类名.图标文件名相同) 注:如果路径不 ...
- 02_ Flume的安装部署及其简单使用
一.Flume的安装部署: Flume的安装非常简单,只需要解压即可,当然,前提是已有hadoop环境 安装包的下载地址为:http://www-us.apache.org/dist/flume/1. ...
- vscode 右键打开
Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\*\shell\VSCode] @="Open with Code" ...
- 4.caffe:train_val.prototxt、 solver.prototxt 、 deploy.prototxt( 创建模型与编写配置文件)
一,train_val.prototxt name: "CIFAR10_quick" layer { name: "cifar" type: "Dat ...
- 牛客第十场 F.Popping Balloons
第一维直接遍历 第二维用线段树维护每个最左端可以得到的贡献 在线段树上每次删除一个点会影响到 X X-R X-2*R 3个值 最多操作1e5次 复杂度 6*n*logn(删了还要加回来 #i ...
- setTimeout设置为0 为啥不能立马执行
setTimeout(function(){}, timer) 是指延时执行.第一个参数是回调函数,第二个参数是指延时多久执行回调函数. setTimeout(function(){console.l ...
- BZOJ 2631 tree / Luogu P1501 [国家集训队]Tree II (LCT,多重标记)
题意 一棵树,有删边加边,有一条链加/乘一个数,有询问一条链的和 分析 LCT,像线段树一样维护两个标记(再加上翻转标记就是三个),维护size,就行了 CODE #include<bits/s ...
- BZOJ 4802: 欧拉函数 (Pollard-Rho)
开始一直T,原来是没有srand- CODE #include<bits/stdc++.h> using namespace std; typedef long long LL; vect ...
- jQuery+masonry实现瀑布流
增加jQuery组件 <script src="//cdn.bootcss.com/jquery/2.2.1/jquery.min.js "></script&g ...