Skip to content

[LeetCode] 802. Find Eventual Safe States #70

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
frdmu opened this issue Aug 5, 2021 · 0 comments
Open

[LeetCode] 802. Find Eventual Safe States #70

frdmu opened this issue Aug 5, 2021 · 0 comments

Comments

@frdmu
Copy link
Owner

frdmu commented Aug 5, 2021

We start at some node in a directed graph, and every turn, we walk along a directed edge of the graph. If we reach a terminal node (that is, it has no outgoing directed edges), we stop.

We define a starting node to be safe if we must eventually walk to a terminal node. More specifically, there is a natural number k, so that we must have stopped at a terminal node in less than k steps for any choice of where to walk.

Return an array containing all the safe nodes of the graph. The answer should be sorted in ascending order.

The directed graph has n nodes with labels from 0 to 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, going from node i to node j.

 

Example 1:

picture1

Input: graph = [[1,2],[2,3],[5],[0],[5],[],[]]
Output: [2,4,5,6]
Explanation: The given graph is shown above.

Example 2:

Input: graph = [[1,2,3,4],[1,2],[3,4],[0,4],[]]
Output: [4]

Constraints:

  • n == graph.length
  • 1 <= n <= 104
  • 0 <= graph[i].length <= n
  • graph[i] is sorted in a strictly increasing order.
  • The graph may contain self-loops.
  • The number of edges in the graph will be in the range [1, 4 * 104].

解法:
拓扑排序用来判断环的存在。
它寻找的是入度为 0 的节点以及仅由入度为 0 的节点所指向的节点
而本题是找到图中出度为 0 的节点,以及仅指向出度为 0 节点的节点
刚好是相反的情况,所以,我们将题目给定的有向图变为反图(也即有向边的起点、终点互换),那么所有安全点便可以通过拓扑排序来求解了。代码如下:

class Solution {
public:
    vector<int> eventualSafeNodes(vector<vector<int>>& graph) {
        int n = graph.size();
        vector<vector<int>> rev(n);
        vector<int> inDegree(n, 0);
        vector<int> res;

        for (int u = 0; u < n; u++) {
            for (auto v: graph[u]) {
                rev[v].push_back(u);
            }
            inDegree[u] = graph[u].size();
        } 
        queue<int> q;
        for (int i = 0; i < n; i++) {
            if (inDegree[i] == 0) {
                q.push(i);
            }
        } 
        while (!q.empty()) {
            int u = q.front();
            q.pop();
            for (auto v: rev[u]) {
                inDegree[v]--;
                if (inDegree[v] == 0)
                    q.push(v);
            }
        }
        for (int i = 0; i < n; i++) {
            if (inDegree[i] == 0) 
                res.push_back(i);
        }

        return res;
    }
};

Refer:
【GTAlgorithm】5分钟干掉拓扑排序

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant