博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
802. Find Eventual Safe States--Medium
阅读量:4683 次
发布时间:2019-06-09

本文共 1916 字,大约阅读时间需要 6 分钟。

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 j such 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.

1.思考

  • 首先想到方法就是DPS,然后对每个节点深度优先搜索直到找到回路,找到就返回true,否则返回false;
  • 一开始是使用vector<int> cyc,然后通过push_back和find来工作的,但是这个方法时间复杂度太高;后来进行优化才想到直接用vector<int> cyc(len, 0),找到回路赋值为1,没找到赋值为0,所以这个方法时间复杂度更低;
  • 在调试过程中,提交之后一直都是Time Exceeded。其实刚开始的思路就是正确的,但是没有很优化,所以才导致的超时。后来考虑到其实不能每个节点都访问才知道它是否有回路,在dps每一个节点的时候,中间节点的回路信息都是可以知道的(在有回路的情况下是知道的,没有回路的情况还要到时候轮到该节点时进行遍历);
  • 本来设置了visit用来记录是否访问,之后发现初始化cyc为1,就可以减少一个变量的使用,还更加简洁明了。

2.实现

  • RunTime: 172ms (56.97%)
  • Memory: 32.2MB (91.80%)
class Solution {public:    vector
eventualSafeNodes(vector
>& graph) { int len = graph.size(); vector
res, cyc(len, 0); for(int i=0; i
>& graph, vector
& cyc){ if(cyc[src]==1)//src is in a cycle return true; else if(cyc[src]==-1)//src isn't in a cycle return false; cyc[src] = 1;//Default which means this node has been visited for(auto dst:graph[src]){ if(dfs(dst, graph, cyc)){//dst is in a cycle cyc[dst] = 1; return true; } } cyc[src] = -1;//dst isn't in a cycle return false; } };

转载于:https://www.cnblogs.com/xuyy-isee/p/10547736.html

你可能感兴趣的文章
iOS IM开发的一些开源、框架和教程等资料
查看>>
2015年创业中遇到的技术问题:51-60
查看>>
FansUnion:共同写博客计划终究还是“流产”了
查看>>
中国象棋程序的设计与实现(六)--N皇后问题的算法设计与实现(源码+注释+截图)...
查看>>
mobiscroll 日期问题
查看>>
<jsp:include>和<%@include%>的区别
查看>>
poj 1691 搜索
查看>>
win7/win8下vmware/VirtualBox虚拟网卡显示未识别网络的解决
查看>>
PCA vs Linear Regression 可视化理解
查看>>
python 二维字典
查看>>
编译原理实验一
查看>>
Git for Android Studio 学习笔记
查看>>
pip 警告!The default format will switch to columns in the future
查看>>
Arrays类学习笔记
查看>>
graphite积累(二)
查看>>
[转]JAVA 反射及使用
查看>>
实验吧之【天下武功唯快不破】
查看>>
c# 生成各种标准条码实例
查看>>
MUTABLE和IMMUTABLE集合
查看>>
忘记MySQL root密码重置MySQL root密码
查看>>