-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathLongest Cycle in a Graph.cpp
62 lines (42 loc) · 1.5 KB
/
Longest Cycle in a Graph.cpp
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
/*
Solution by Rahul Surana
***********************************************************
You are given a directed graph of n nodes numbered from 0 to n - 1,
where each node has at most one outgoing edge.
The graph is represented with a given 0-indexed array edges of size n,
indicating that there is a directed edge from node i to node edges[i].
If there is no outgoing edge from node i, then edges[i] == -1.
Return the length of the longest cycle in the graph. If no cycle exists, return -1.
A cycle is a path that starts and ends at the same node.
***********************************************************
*/
#include<bits/stdc++.h>
class Solution {
public:
vector<bool> v;
int ans = -1;
void df(int &i, vector<int>& edges,unordered_map<int,int> &dist){
if(i == -1 || v[i]) {
return;
}
v[i] = true;
if(edges[i] != -1 && !v[edges[i]]){
dist[edges[i]] = dist[i]+1;
df(edges[i],edges,dist);
}
else if(edges[i] != -1 && dist.count(edges[i])){
ans = max(ans, dist[i] - dist[edges[i]]+1);
}
}
int longestCycle(vector<int>& edges) {
v.resize(edges.size(),false);
for(int i = 0; i < edges.size(); i++){
if(!v[i]){
unordered_map<int,int> dist;
dist[i] = 1;
df(i, edges, dist);
}
}
return ans;
}
};