Problem p02892 (C++)

Buggy submission (status: Runtime Error):
/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author parsa bahrami
 */

#pragma GCC optimize("O2")

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef long long int ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pld;
typedef pair<string, string> pss;

template <class T>
using Tree =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define sz(x) (ll) x.size()
#define jjoin(x)                                                               \
  for (auto i : x)                                                             \
    cout << i << endl;
#define all(x) (x).begin(), (x).end()
#define F first
#define S second
#define Mp make_pair
#define sep ' '
#define error(x) cerr << #x << " = " << x << endl
#define fast_io                                                                \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define set_random                                                             \
  mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
#define kill(x) return cout << x << endl, 0;
#define Hkill(x)                                                               \
  cout << x << endl;                                                           \
  exit(0);
#define endl '\n'

ll poww(ll a, ll b, ll md) {
  return (!b ? 1
             : (b & 1 ? a * poww(a * a % md, b / 2, md) % md
                      : poww(a * a % md, b / 2, md) % md));
}

const ll MAXN = 2e5 + 10;
const ll MAXA = 101;
const ll INF = 8e18;
const ll MOD = 998244353; // 1e9 + 7
const ld PI = 4 * atan((ld)1);
bool adj[MAXA][MAXA];
ll T[MAXN], H[MAXN], n;

void dfs(ll v) {
  for (ll u = 1; u <= n; u++) {
    if (adj[u][v] && T[u] == INF) {
      T[u] = T[v] ^ 1;
      dfs(u);
    } else if (T[u] == T[v] && adj[u][v]) {
      // cout << T[u] << sep << 'u' << sep << T[v] << sep << 'v' << endl;
      Hkill(-1);
    }
  }
}

ll bfs(ll v) {
  fill(H, H + MAXN, 0);
  queue<ll> Q;
  Q.push(v);
  H[v] = 1;
  while (sz(Q)) {
    ll u = Q.front();
    Q.pop();
    for (ll ver = 1; ver <= n; ver++) {
      if (H[ver] == 0 && adj[ver][u]) {
        H[ver] = H[u] + 1;
        Q.push(ver);
      }
    }
  }
  return *max_element(H, H + MAXN);
}

int main() {
  fast_io;
  set_random;
  cin >> n;
  for (ll i = 1; i <= n; i++) {
    for (ll j = 1; j <= n; j++) {
      char c;
      cin >> c;
      adj[i][j] = c - '0';
    }
  }
  fill(T, T + MAXN, INF);
  T[1] = 1;
  dfs(1);
  ll ans = 0;
  for (ll i = 1; i <= n; i++) {
    ans = max(ans, bfs(i));
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author parsa bahrami
 */

#pragma GCC optimize("O2")

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef long long int ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pld;
typedef pair<string, string> pss;

template <class T>
using Tree =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define sz(x) (ll) x.size()
#define jjoin(x)                                                               \
  for (auto i : x)                                                             \
    cout << i << endl;
#define all(x) (x).begin(), (x).end()
#define F first
#define S second
#define Mp make_pair
#define sep ' '
#define error(x) cerr << #x << " = " << x << endl
#define fast_io                                                                \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define set_random                                                             \
  mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
#define kill(x) return cout << x << endl, 0;
#define Hkill(x)                                                               \
  cout << x << endl;                                                           \
  exit(0);
#define endl '\n'

ll poww(ll a, ll b, ll md) {
  return (!b ? 1
             : (b & 1 ? a * poww(a * a % md, b / 2, md) % md
                      : poww(a * a % md, b / 2, md) % md));
}

const ll MAXN = 2e5 + 10;
const ll MAXA = 210;
const ll INF = 8e18;
const ll MOD = 998244353; // 1e9 + 7
const ld PI = 4 * atan((ld)1);
bool adj[MAXA][MAXA];
ll T[MAXN], H[MAXN], n;

void dfs(ll v) {
  for (ll u = 1; u <= n; u++) {
    if (adj[u][v] && T[u] == INF) {
      T[u] = T[v] ^ 1;
      dfs(u);
    } else if (T[u] == T[v] && adj[u][v]) {
      // cout << T[u] << sep << 'u' << sep << T[v] << sep << 'v' << endl;
      Hkill(-1);
    }
  }
}

ll bfs(ll v) {
  fill(H, H + MAXN, 0);
  queue<ll> Q;
  Q.push(v);
  H[v] = 1;
  while (sz(Q)) {
    ll u = Q.front();
    Q.pop();
    for (ll ver = 1; ver <= n; ver++) {
      if (H[ver] == 0 && adj[ver][u]) {
        H[ver] = H[u] + 1;
        Q.push(ver);
      }
    }
  }
  return *max_element(H, H + MAXN);
}

int main() {
  fast_io;
  set_random;
  cin >> n;
  for (ll i = 1; i <= n; i++) {
    for (ll j = 1; j <= n; j++) {
      char c;
      cin >> c;
      adj[i][j] = c - '0';
    }
  }
  fill(T, T + MAXN, INF);
  T[1] = 1;
  dfs(1);
  ll ans = 0;
  for (ll i = 1; i <= n; i++) {
    ans = max(ans, bfs(i));
  }
  cout << ans << endl;
  return 0;
}