Problem p02892 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define min(a, b) ((a) < (b) ? (a) : (b))
#define max(a, b) ((a) > (b) ? (a) : (b))
#define REP(i, n) for (ll i = 0; i < n; i++)
#define FOR(i, n1, n2) for (ll i = n1; i < n2; i++)
#define bFOR(i, n1, n2) for (ll i = n1; i >= n2; i--)
#define speed_up                                                               \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);

typedef long long int ll;
typedef pair<ll, ll> Pi;
typedef tuple<ll, ll, ll> Tu;
const int INF = (ll)(1LL << 30) - 1;
const double INFd = 100000000000.0;
const double PI = 3.14151926535;
const ll INFl = (ll)9223372036854775807 / 2;
const int MAX = 10000;
const ll MOD = (ll)1e9 + 7;
const ll tMOD = (ll)998244353;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
// int dx[4]={0,-1,0,1},dy[4]={-1,0,1,0};
int mdx[8] = {0, 1, 0, -1, 1, 1, -1, -1}, mdy[8] = {-1, 0, 1, 0, 1, -1, 1, -1};

template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}

int n;
int d[210][210];
int dd[210];
vector<int> v[210];
void warshall_floyd() {
  for (int k = 0; k < n; k++)
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
}
bool tree_make() {
  queue<int> q;
  q.push(0);
  dd[0] = -1;
  while (q.size()) {
    int t = q.front();
    q.pop();
    for (int i = 0; i < v[t].size(); i++) {
      if (dd[v[t][i]] == 0) {
        dd[v[t][i]] = dd[t] * -1;
        q.push(dd[v[t][i]]);
      } else {
        if (dd[t] == dd[v[t][i]])
          return false;
      }
    }
  }
  return true;
}

int main() {
  cin >> n;
  REP(i, n) {
    REP(j, n) {

      char s;
      cin >> s;
      if (i == j)
        continue;
      if (s == '1') {
        d[i][j] = 1;
        d[j][i] = 1;
        v[i].push_back(j);
        v[j].push_back(i);
      } else {
        d[i][j] = INF;
        d[j][i] = INF;
      }
    }
  }
  if (!tree_make()) {
    cout << -1 << endl;
    return 0;
  }
  warshall_floyd();
  int ans = 0;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) {
      if (d[i][j] != INF)
        ans = max(ans, d[i][j] + 1);
    }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define min(a, b) ((a) < (b) ? (a) : (b))
#define max(a, b) ((a) > (b) ? (a) : (b))
#define REP(i, n) for (ll i = 0; i < n; i++)
#define FOR(i, n1, n2) for (ll i = n1; i < n2; i++)
#define bFOR(i, n1, n2) for (ll i = n1; i >= n2; i--)
#define speed_up                                                               \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);

typedef long long int ll;
typedef pair<ll, ll> Pi;
typedef tuple<ll, ll, ll> Tu;
const int INF = (ll)(1LL << 30) - 1;
const double INFd = 100000000000.0;
const double PI = 3.14151926535;
const ll INFl = (ll)9223372036854775807 / 2;
const int MAX = 10000;
const ll MOD = (ll)1e9 + 7;
const ll tMOD = (ll)998244353;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
// int dx[4]={0,-1,0,1},dy[4]={-1,0,1,0};
int mdx[8] = {0, 1, 0, -1, 1, 1, -1, -1}, mdy[8] = {-1, 0, 1, 0, 1, -1, 1, -1};

template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}

int n;
int d[210][210];
int dd[210];
vector<int> v[210];
void warshall_floyd() {
  for (int k = 0; k < n; k++)
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
}
bool tree_make() {
  queue<int> q;
  q.push(0);
  dd[0] = -1;
  while (q.size()) {
    int t = q.front();
    q.pop();
    for (int i = 0; i < v[t].size(); i++) {
      if (dd[v[t][i]] == 0) {
        dd[v[t][i]] = dd[t] * (-1);
        q.push(v[t][i]);
      } else {
        if (dd[t] == dd[v[t][i]])
          return false;
      }
    }
  }
  return true;
}

int main() {
  cin >> n;
  REP(i, n) {
    REP(j, n) {

      char s;
      cin >> s;
      if (i == j)
        continue;
      if (s == '1') {
        d[i][j] = 1;
        d[j][i] = 1;
        v[i].push_back(j);
        v[j].push_back(i);
      } else {
        d[i][j] = INF;
        d[j][i] = INF;
      }
    }
  }
  if (!tree_make()) {
    cout << -1 << endl;
    return 0;
  }
  warshall_floyd();
  int ans = 0;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) {
      if (d[i][j] != INF)
        ans = max(ans, d[i][j] + 1);
    }
  cout << ans << endl;
  return 0;
}