Problem p02892 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
using lint = long long int;
long long int INF = 1001001001001001LL;
int inf = 1000000007;
long long int MOD = 1000000007LL;
double PI = 3.1415926535897932;

template <typename T1, typename T2> inline void chmin(T1 &a, const T2 &b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, const T2 &b) {
  if (a < b)
    a = b;
}

#define ALL(a) a.begin(), a.end()
#define RALL(a) a.rbegin(), a.rend()
/* do your best */

int solve(int startNode, vector<vector<int>> &g) {
  // startNode から幅優先探索をして，どれだけ進めるかを返す．
  // ダメなら -1 をかえす

  int n = g.size();

  bool ok = true;
  int ret = 0;

  queue<tuple<int, int, int>> que; // cur, par, cost
  vector<int> used(n, -1);
  que.push({startNode, -1, 1});

  while (!que.empty()) {
    int cur, par, cost;
    tie(cur, par, cost) = que.front();
    que.pop();
    used[cur] = cost;
    ret = max(ret, cost);
    for (auto nxt : g[cur]) {
      if (used[nxt] != -1 and cost - 1 != used[nxt]) {
        ok = false;
        break;
      }

      if (!ok)
        break;

      if (used[nxt] == -1)
        que.push({nxt, cur, cost + 1});
    }
  }

  if (!ok)
    return -1;
  else
    return ret;
}

int main() {

  // 幅優先探索をするが，途中で探索済みに遷移しようとしたら out
  // これをすべての頂点に対してやる

  int n;
  cin >> n;
  vector<vector<int>> g(n);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      char c;
      cin >> c;
      if (c == '1') {
        g[i].push_back(j);
      }
    }
  }

  int ans = -1;

  for (int i = 0; i < n; i++) {
    ans = max(ans, solve(i, g));
  }

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
using lint = long long int;
long long int INF = 1001001001001001LL;
int inf = 1000000007;
long long int MOD = 1000000007LL;
double PI = 3.1415926535897932;

template <typename T1, typename T2> inline void chmin(T1 &a, const T2 &b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, const T2 &b) {
  if (a < b)
    a = b;
}

#define ALL(a) a.begin(), a.end()
#define RALL(a) a.rbegin(), a.rend()
/* do your best */

int solve(int startNode, vector<vector<int>> &g) {
  // startNode から幅優先探索をして，どれだけ進めるかを返す．
  // ダメなら -1 をかえす

  int n = g.size();

  bool ok = true;
  int ret = 0;

  queue<tuple<int, int, int>> que; // cur, par, cost
  vector<int> used(n, -1);
  que.push({startNode, -1, 1});

  while (!que.empty()) {
    int cur, par, cost;
    tie(cur, par, cost) = que.front();
    que.pop();
    if (used[cur] != -1) {
      continue;
    }
    used[cur] = cost;
    ret = max(ret, cost);
    for (auto nxt : g[cur]) {
      if (used[nxt] != -1 and cost - 1 != used[nxt]) {
        ok = false;
        break;
      }

      if (!ok)
        break;

      if (used[nxt] == -1)
        que.push({nxt, cur, cost + 1});
    }
  }

  if (!ok)
    return -1;
  else
    return ret;
}

int main() {

  // 幅優先探索をするが，途中で探索済みに遷移しようとしたら out
  // これをすべての頂点に対してやる

  int n;
  cin >> n;
  vector<vector<int>> g(n);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      char c;
      cin >> c;
      if (c == '1') {
        g[i].push_back(j);
      }
    }
  }

  int ans = -1;

  for (int i = 0; i < n; i++) {
    ans = max(ans, solve(i, g));
  }

  cout << ans << endl;

  return 0;
}