-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShortestCycle.cpp
66 lines (66 loc) · 1.37 KB
/
ShortestCycle.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
63
64
65
66
#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
int M = 22;
vector<int> gr[N];
void bfs(int src, int n, int &ans)
{
vector<int> dis(n + 1, INT_MAX);
queue<int> q;
q.push(src);
dis[src] = 0;
while (!q.empty())
{
int cur = q.front();
q.pop();
for (auto nbr : gr[src])
{
if (dis[nbr] == INT_MAX)
{
dis[nbr] = dis[cur] + 1;
q.push(nbr);
}
else if (dis[nbr] >= dis[cur])
{
// back edge is encountered
// cout << ans << " ";
ans = min(ans, dis[nbr] + dis[cur] + 1);
// cout << dis[cur] << " ";
// cout << dis[nbr] << endl;
}
}
}
}
void solve()
{
int n, m;
cin >> n >> m;
for (int i = 0; i < m; i++)
{
int x, y;
cin >> x >> y;
gr[x].push_back(y);
gr[y].push_back(x);
}
int ans = n + 1;
for (int i = 1; i <= n; i++)
{
bfs(i, n, ans);
}
if (ans == n + 1)
{
cout << "No cycle is present in the graph" << endl;
}
else
{
cout << "shortest cycle detected is of length " << ans << endl;
}
}
int main(int argc, char const *argv[])
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}