-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path2360.cpp
42 lines (41 loc) · 1.24 KB
/
2360.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
class Solution {
public:
// {node, length}:
// node = -1: not detected
// node = -2: detected
pair<int, int> dfs(vector<int>& edges, vector<int>& visited, int node) {
if (visited[node] != -1) return {-1, -1};
visited[node] = 0;
int neighbor = edges[node];
if (neighbor == -1) {
visited[node] = 1;
return {-1, -1};
}
if (visited[neighbor] == 0) {
visited[node] = 1;
return {neighbor, 1};
}
auto [cycle, length] = dfs(edges, visited, neighbor);
visited[node] = 1;
if (cycle == -1) return {-1, -1};
if (cycle == node) return {-2, length + 1};
else {
if (cycle == -2) return {-2, length};
else return {cycle, length + 1};
}
}
int longestCycle(vector<int>& edges) {
int n = edges.size();
vector<int> visited(n, -1);
int maxLength = -1;
for (int i = 0; i < n; ++i) {
if (visited[i] == -1) {
auto [node, length] = dfs(edges, visited, i);
if (node != -1 && length > maxLength) {
maxLength = length;
}
}
}
return maxLength;
}
};