-
Notifications
You must be signed in to change notification settings - Fork 495
/
(LEETCODE)Longest Cycle in a Graph.java
83 lines (76 loc) · 2.13 KB
/
(LEETCODE)Longest Cycle in a Graph.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
class Solution {
public int longestCycle(int[] edges) {
ArrayList<ArrayList<Integer>> adj= new ArrayList<>();
ArrayList<ArrayList<Integer> > ans = new ArrayList<ArrayList<Integer> >();
for(int i=0;i<edges.length;i++)
{
adj.add(new ArrayList<>());
}
for(int i=0;i<edges.length;i++)
{
if(edges[i]!=-1)
adj.get(i).add(edges[i]);
}
int k=kosaraju(edges.length,adj,ans);
if(ans.size()!=0)
Collections.sort(ans,(a,b)->b.size()-a.size());
if(ans.size()==0)
{
return -1;
}
return ans.get(0).size();
}
public int kosaraju(int n, ArrayList<ArrayList<Integer>> adj, ArrayList<ArrayList<Integer> > ans)
{
//code here
int vis[] = new int[n];
Stack<Integer> st = new Stack<Integer>();
for(int i = 0;i<n;i++) {
if(vis[i] == 0) {
dfs(i, st, adj, vis);
}
}
ArrayList<ArrayList<Integer> > transpose = new ArrayList<ArrayList<Integer> >();
// ArrayList<ArrayList<Integer> > ans = new ArrayList<ArrayList<Integer> >();
for (int i = 0; i < n; i++)
transpose.add(new ArrayList<Integer>());
for(int i = 0;i<n;i++) {
vis[i] = 0;
for(Integer it: adj.get(i)) {
transpose.get(it).add(i);
}
}
int cnt = 0;
while(st.size() > 0) {
int node = st.peek();
st.pop();
if(vis[node] == 0) {
cnt++;
ArrayList<Integer> p= new ArrayList<>();
revDfs(node, transpose, vis,p);
if(p.size()>1)
ans.add(new ArrayList<>(p));
}
}
return cnt;
}
private void dfs(int node, Stack<Integer> st, ArrayList<ArrayList<Integer>> adj, int vis[]) {
vis[node] = 1;
for(Integer it : adj.get(node)) {
if(vis[it] == 0) {
dfs(it, st, adj, vis);
}
}
st.push(node);
}
private void revDfs(int node, ArrayList<ArrayList<Integer>> transpose, int vis[],ArrayList<Integer> curr) {
vis[node] = 1;
// System.out.print(node + " ");
curr.add(node);
for(Integer it : transpose.get(node)) {
if(vis[it] == 0) {
revDfs(it, transpose, vis,curr);
}
}
}
}