-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path2608.cpp
More file actions
35 lines (34 loc) · 1.12 KB
/
2608.cpp
File metadata and controls
35 lines (34 loc) · 1.12 KB
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
class Solution {
public:
int findShortestCycle(int n, vector<vector<int>>& edges) {
vector<vector<int>> adjacency(n);
for (auto& edge : edges) {
adjacency[edge[0]].push_back(edge[1]);
adjacency[edge[1]].push_back(edge[0]);
}
int res = INT_MAX;
for (int root = 0; root < n; ++root) {
vector<int> parents(n, -1);
vector<int> dist(n, -1);
queue<int> q;
q.push(root);
dist[root] = 0;
while (!q.empty()) {
int node = q.front();
q.pop();
for (auto& neighbor : adjacency[node]) {
if (dist[neighbor] == -1) {
dist[neighbor] = dist[node] + 1;
parents[neighbor] = node;
q.push(neighbor);
}
else if (parents[node] != neighbor) {
res = min(res, dist[node] + dist[neighbor] + 1);
}
}
}
}
if (res == INT_MAX) return -1;
return res;
}
};