Problem p02892 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <ctime>
#include <deque>
#include <float.h>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;

unsigned euclidean_gcd(unsigned a, unsigned b) {
  if (a < b)
    return euclidean_gcd(b, a);
  unsigned r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}

ll ll_gcd(ll a, ll b) {
  if (a < b)
    return ll_gcd(b, a);
  ll r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}

class UnionFind {
public:
  vector<ll> par;
  vector<ll> siz;
  UnionFind(ll sz_) : par(sz_), siz(sz_, 1LL) {
    for (ll i = 0; i < sz_; ++i)
      par[i] = i;
  }
  void init(ll sz_) {
    par.resize(sz_);
    siz.assign(sz_, 1LL);
    for (ll i = 0; i < sz_; ++i)
      par[i] = i;
  }
  ll root(ll x) {
    while (par[x] != x) {
      x = par[x] = par[par[x]];
    }
    return x;
  }
  bool merge(ll x, ll y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (siz[x] < siz[y])
      swap(x, y);
    siz[x] += siz[y];
    par[y] = x;
    return true;
  }

  bool issame(ll x, ll y) { return root(x) == root(y); }

  ll size(ll x) { return siz[root(x)]; }
};

long long modpow(long long a, long long n, long long mod) {
  long long res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

long long modinv(long long a, long long mod) { return modpow(a, mod - 2, mod); }

int main() {
  ll n;
  cin >> n;
  vector<vector<int>> z(n);
  UnionFind u(n * 2);
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    for (int j = 0; j < n; j++) {
      if (s[j] == '1') {
        z[i].push_back(j);
        u.merge(i, j + n);
      }
    }
  }
  int ansi = 0;
  for (int i = 0; i < n; i++) {
    if (u.issame(i, i + n))
      ansi = -1;
  }
  if (ansi == -1)
    cout << ansi << endl;
  else {
    int anss = 0;
    for (int i = 0; i < n; i++) {
      vector<bool> seen(n, false);
      vector<int> ans(n);
      queue<int> q;
      q.push(i);
      while (!q.empty()) {
        int h = q.front();
        seen[h] = true;
        for (int i = 0; i < z[h].size(); i++) {
          if (!seen[z[h][i]]) {
            q.push(z[h][i]);
            ans[z[h][i]] = ans[h] + 1;
          }
        }
        q.pop();
      }
      for (int j = 0; j < n; j++) {
        anss = max(anss, ans[j] + 1);
      }
    }
    cout << anss << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <ctime>
#include <deque>
#include <float.h>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;

unsigned euclidean_gcd(unsigned a, unsigned b) {
  if (a < b)
    return euclidean_gcd(b, a);
  unsigned r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}

ll ll_gcd(ll a, ll b) {
  if (a < b)
    return ll_gcd(b, a);
  ll r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}

class UnionFind {
public:
  vector<ll> par;
  vector<ll> siz;
  UnionFind(ll sz_) : par(sz_), siz(sz_, 1LL) {
    for (ll i = 0; i < sz_; ++i)
      par[i] = i;
  }
  void init(ll sz_) {
    par.resize(sz_);
    siz.assign(sz_, 1LL);
    for (ll i = 0; i < sz_; ++i)
      par[i] = i;
  }
  ll root(ll x) {
    while (par[x] != x) {
      x = par[x] = par[par[x]];
    }
    return x;
  }
  bool merge(ll x, ll y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (siz[x] < siz[y])
      swap(x, y);
    siz[x] += siz[y];
    par[y] = x;
    return true;
  }

  bool issame(ll x, ll y) { return root(x) == root(y); }

  ll size(ll x) { return siz[root(x)]; }
};

long long modpow(long long a, long long n, long long mod) {
  long long res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

long long modinv(long long a, long long mod) { return modpow(a, mod - 2, mod); }

int main() {
  ll n;
  cin >> n;
  vector<vector<int>> z(n);
  UnionFind u(n * 2);
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    for (int j = 0; j < n; j++) {
      if (s[j] == '1') {
        z[i].push_back(j);
        u.merge(i, j + n);
      }
    }
  }
  int ansi = 0;
  for (int i = 0; i < n; i++) {
    if (u.issame(i, i + n))
      ansi = -1;
  }
  if (ansi == -1)
    cout << ansi << endl;
  else {
    int anss = 0;
    for (int i = 0; i < n; i++) {
      vector<bool> seen(n, false);
      vector<int> ans(n);
      queue<int> q;
      q.push(i);
      while (!q.empty()) {
        int h = q.front();
        seen[h] = true;
        for (int i = 0; i < z[h].size(); i++) {
          if (!seen[z[h][i]]) {
            seen[z[h][i]] = true;
            q.push(z[h][i]);
            ans[z[h][i]] = ans[h] + 1;
          }
        }
        q.pop();
      }
      for (int j = 0; j < n; j++) {
        anss = max(anss, ans[j] + 1);
      }
    }
    cout << anss << endl;
  }
}