-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFind_Eventual_Safe_Nodes.java
47 lines (47 loc) · 1.11 KB
/
Find_Eventual_Safe_Nodes.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
class Solution {
private boolean dfscheck(int node,int vis[],int pathvis[],int check[],int graph[][])
{
vis[node]=1;
pathvis[node]=1;
check[node]=0;
for(int it:graph[node])
{
if (vis[it]==0)
{
if (dfscheck(it,vis,pathvis,check,graph)==true)
{
return true;
}
}
if(pathvis[it]==1)
{
return true;
}
}
check[node]=1;
pathvis[node]=0;
return false;
}
public List<Integer> eventualSafeNodes(int[][] graph) {
int v=graph.length;
int vis[]=new int[v];
int pathvis[]=new int[v];
int check[]=new int[v];
for(int i=0;i<v;i++)
{
if(vis[i]==0)
{
dfscheck(i,vis,pathvis,check,graph);
}
}
List<Integer> ans = new ArrayList<>();
for(int i=0;i<v;i++)
{
if (check[i]==1)
{
ans.add(i);
}
}
return ans;
}
}