Problem p02892 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <assert.h>
#include <climits>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <vector>

using namespace std;

template <typename T> inline bool sign(T A) { return (A > 0) - (A < 0); }
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define SREP(i, s, n)                                                          \
  for (int i = s; (n - i) * sign(n - s) > 0; i += sign(n - s))
#define all(x) (x).begin(), (x).end()
typedef long long ll;
typedef unsigned long long ull;

template <typename T> T gcd(T a, T b) {
  if (a < b)
    gcd(b, a);
  if (b == 1)
    return 1;
  T r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}

bool _less(pair<int, int> a, pair<int, int> b) { return a.second < b.second; }

template <template <class, class, class...> class C, typename K, typename V,
          typename... Args>
V map_get(const C<K, V, Args...> &m, K const &key, const V &defval) {
  typename C<K, V, Args...>::const_iterator it = m.find(key);
  if (it == m.end())
    return defval;
  return it->second;
}

#define MOD (1'000'000'000 + 7
#define INF (ll(1e18))

int main(void) {
  ll N;
  cin >> N;
  vector<string> S(N);
  REP(i, N) { cin >> S[i]; }
  ll maxk = -1;
  REP(i, N) {
    queue<pair<ll, ll>> q;
    vector<ll> v(N, -1);
    ll k = -1;
    q.push(make_pair(i, 1));
    while (q.size()) {
      auto j = q.front();
      q.pop();
      if (v[j.first] >= 0 && v[j.first] != j.second) {
        k = -1;
        break;
      }
      v[j.first] = j.second;
      k = j.second;
      REP(l, N) {
        if (S[j.first][l] == '1') {
          if (v[l] < 0) {
            q.push(make_pair(l, k + 1));
          } else if (v[l] != k - 1 && v[l] != k + 1) {
            k = -1;
            break;
          }
        }
      }
      if (k < 0)
        break;
    }
    maxk = max(maxk, k);
  }
  cout << maxk << endl;
}


Fixed submission:
#include <algorithm>
#include <assert.h>
#include <climits>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <vector>

using namespace std;

template <typename T> inline bool sign(T A) { return (A > 0) - (A < 0); }
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define SREP(i, s, n)                                                          \
  for (int i = s; (n - i) * sign(n - s) > 0; i += sign(n - s))
#define all(x) (x).begin(), (x).end()
typedef long long ll;
typedef unsigned long long ull;

template <typename T> T gcd(T a, T b) {
  if (a < b)
    gcd(b, a);
  if (b == 1)
    return 1;
  T r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}

bool _less(pair<int, int> a, pair<int, int> b) { return a.second < b.second; }

template <template <class, class, class...> class C, typename K, typename V,
          typename... Args>
V map_get(const C<K, V, Args...> &m, K const &key, const V &defval) {
  typename C<K, V, Args...>::const_iterator it = m.find(key);
  if (it == m.end())
    return defval;
  return it->second;
}

#define MOD (1'000'000'000 + 7
#define INF (ll(1e18))

int main(void) {
  ll N;
  cin >> N;
  vector<string> S(N);
  REP(i, N) { cin >> S[i]; }
  ll maxk = -1;
  REP(i, N) {
    queue<pair<ll, ll>> q;
    vector<ll> v(N, -1);
    ll k = -1;
    q.push(make_pair(i, 1));
    while (q.size()) {
      auto j = q.front();
      q.pop();
      if (v[j.first] >= 0 && v[j.first] != j.second) {
        k = -1;
        break;
      } else if (v[j.first] == j.second) {
        continue;
      }
      v[j.first] = j.second;
      k = j.second;
      REP(l, N) {
        if (S[j.first][l] == '1') {
          if (v[l] < 0) {
            q.push(make_pair(l, k + 1));
          } else if (v[l] != k - 1 && v[l] != k + 1) {
            k = -1;
            break;
          }
        }
      }
      if (k < 0)
        break;
    }
    maxk = max(maxk, k);
  }
  cout << maxk << endl;
}