category-wise-problems

contains category wise problems(data structures, competitive) of popular platforms.

View the Project on GitHub mayankdutta/category-wise-problems

Graph girth, Medium, modified BFS

Approach
Code sample ```cpp int n, m; cin >> n >> m; vector<vector> graph(n); for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; a--, b--; graph[a].push_back(b); graph[b].push_back(a); } int ans = INT_MAX; for (int i = 0; i < n; i++) { vector dist(n, -1); queue qu; qu.push(i); dist[i] = 0; while (!qu.empty()) { auto u = qu.front(); qu.pop(); for (const auto &v : graph[u]) { if (dist[v] == -1) { dist[v] = dist[u] + 1; qu.push(v); } else if (dist[v] >= dist[u]) { ans = min(ans, dist[u] + dist[v] + 1); } } } } cout << (ans == INT_MAX ? -1 : ans) << '\n'; ``` </details>