Problem p02892 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

using ll = long long;
using P = pair<int, int>;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;

template <class T> inline bool chmax(T &x, T y) {
  if (x < y) {
    x = y;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &x, T y) {
  if (x > y) {
    x = y;
    return true;
  }
  return false;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int N;
  cin >> N;
  vector<vector<int>> G(N, vector<int>(N));
  for (int i = 0; i < N; ++i) {
    string S;
    cin >> S;
    for (int j = 0; j < N; ++j) {
      G[i][j] = S[j] - '0';
    }
  }
  vector<int> color(N, -1);
  auto is_bipartite = [&](auto &&self, int u = 0, int c = 0) -> bool {
    color[u] = c;
    for (int v = 0; v < N; ++v) {
      if (!G[u][v])
        continue;
      if (color[v] == -1 && !self(self, v, c ^ 1))
        return false;
      else if (color[v] == c)
        return false;
    }
    return true;
  };
  if (!is_bipartite(is_bipartite)) {
    cout << -1 << endl;
    return 0;
  }
  int ans = -1;
  for (int u = 0; u < N; ++u) {
    for (int v = 0; v < N; ++v)
      color[v] = -1;
    bool ok = true;
    int max_number = 1;
    queue<P> que;
    que.emplace(u, 1);
    while (!que.empty()) {
      int cur = que.front().first, c = que.front().second;
      que.pop();
      color[cur] = c;
      chmax(max_number, c);
      for (int nxt = 0; nxt < N; ++nxt) {
        if (!G[cur][nxt])
          continue;
        if (color[nxt] == -1)
          que.emplace(nxt, c + 1);
        else if (color[nxt] != c - 1 && color[nxt] != c + 1) {
          ok = false;
          break;
        }
      }
      if (!ok)
        break;
    }
    if (ok)
      chmax(ans, max_number);
  }
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

using ll = long long;
using P = pair<int, int>;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;

template <class T> inline bool chmax(T &x, T y) {
  if (x < y) {
    x = y;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &x, T y) {
  if (x > y) {
    x = y;
    return true;
  }
  return false;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int N;
  cin >> N;
  vector<vector<int>> G(N, vector<int>(N));
  for (int i = 0; i < N; ++i) {
    string S;
    cin >> S;
    for (int j = 0; j < N; ++j) {
      G[i][j] = S[j] - '0';
    }
  }
  vector<int> color(N, -1);
  auto is_bipartite = [&](auto &&self, int u = 0, int c = 0) -> bool {
    color[u] = c;
    for (int v = 0; v < N; ++v) {
      if (!G[u][v])
        continue;
      if (color[v] == -1 && !self(self, v, c ^ 1))
        return false;
      else if (color[v] == c)
        return false;
    }
    return true;
  };
  if (!is_bipartite(is_bipartite)) {
    cout << -1 << endl;
    return 0;
  }
  int ans = -1;
  for (int u = 0; u < N; ++u) {
    for (int v = 0; v < N; ++v)
      color[v] = -1;
    bool ok = true;
    int max_number = 1;
    queue<P> que;
    que.emplace(u, 1);
    while (!que.empty()) {
      int cur = que.front().first, c = que.front().second;
      que.pop();
      if (color[cur] != -1)
        continue;
      color[cur] = c;
      chmax(max_number, c);
      for (int nxt = 0; nxt < N; ++nxt) {
        if (!G[cur][nxt])
          continue;
        if (color[nxt] == -1)
          que.emplace(nxt, c + 1);
        else if (color[nxt] != c - 1 && color[nxt] != c + 1) {
          ok = false;
          break;
        }
      }
      if (!ok)
        break;
    }
    if (ok)
      chmax(ans, max_number);
  }
  cout << ans << endl;
}