Problem p02892 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ri register int
#define fi first
#define se second
#define pb push_back
using namespace std;
const int rlen = 1 << 18 | 1;
char buf[rlen], *ib = buf, *ob = buf;
#define gc()                                                                   \
  (((ib == ob) && (ob = (ib = buf) + fread(buf, 1, rlen, stdin)), ib == ob)    \
       ? -1                                                                    \
       : *ib++)
inline int read() {
  int ans = 0;
  char ch = gc();
  while (!isdigit(ch))
    ch = gc();
  while (isdigit(ch))
    ans = ((ans << 2) + ans << 1) + (ch ^ 48), ch = gc();
  return ans;
}
typedef pair<int, int> pii;
typedef long long ll;
typedef unsigned long long Ull;
inline int Read(char *s) {
  int top = 0;
  char ch = gc();
  while (!isdigit(ch))
    ch = gc();
  while (isdigit(ch))
    s[++top] = ch, ch = gc();
  return top;
}
char s[205];
int n, dep[205];
bool vs[205];
vector<int> e[205];
void check(int p) {
  vs[p] = 1;
  for (ri i = 0, v; i < e[p].size(); ++i) {
    if (vs[v = e[p][i]]) {
      if ((dep[v] + dep[p]) & 1)
        continue;
      puts("-1");
      exit(0);
    }
    dep[v] = dep[p] + 1;
    check(v);
  }
}
int main() {
#ifdef ldxcaicai
  freopen("lx.in", "r", stdin);
#endif
  n = read();
  for (ri i = 1; i <= n; ++i) {
    Read(s);
    for (ri j = 1; j <= n; ++j)
      if (s[j] == '1')
        e[i].pb(j);
  }
  check(1);
  int mx = 0;
  for (ri s = 1; s <= n; ++s) {
    memset(dep, 0, sizeof(dep));
    memset(vs, 0, sizeof(vs));
    queue<int> q;
    q.push(s), dep[s] = 1;
    while (q.size()) {
      int x = q.front();
      q.pop();
      vs[x] = 1;
      for (ri i = 0, v; i < e[x].size(); ++i) {
        if (vs[v = e[x][i]])
          continue;
        dep[v] = dep[x] + 1;
        q.push(v);
      }
    }
    for (ri i = 1; i <= n; ++i)
      mx = max(mx, dep[i]);
  }
  cout << mx;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ri register int
#define fi first
#define se second
#define pb push_back
using namespace std;
const int rlen = 1 << 18 | 1;
char buf[rlen], *ib = buf, *ob = buf;
#define gc()                                                                   \
  (((ib == ob) && (ob = (ib = buf) + fread(buf, 1, rlen, stdin)), ib == ob)    \
       ? -1                                                                    \
       : *ib++)
inline int read() {
  int ans = 0;
  char ch = gc();
  while (!isdigit(ch))
    ch = gc();
  while (isdigit(ch))
    ans = ((ans << 2) + ans << 1) + (ch ^ 48), ch = gc();
  return ans;
}
typedef pair<int, int> pii;
typedef long long ll;
typedef unsigned long long Ull;
inline int Read(char *s) {
  int top = 0;
  char ch = gc();
  while (!isdigit(ch))
    ch = gc();
  while (isdigit(ch))
    s[++top] = ch, ch = gc();
  return top;
}
char s[205];
int n, dep[205];
bool vs[205];
vector<int> e[205];
void check(int p) {
  vs[p] = 1;
  for (ri i = 0, v; i < e[p].size(); ++i) {
    if (vs[v = e[p][i]]) {
      if ((dep[v] + dep[p]) & 1)
        continue;
      puts("-1");
      exit(0);
    }
    dep[v] = dep[p] + 1;
    check(v);
  }
}
int main() {
#ifdef ldxcaicai
  freopen("lx.in", "r", stdin);
#endif
  n = read();
  for (ri i = 1; i <= n; ++i) {
    Read(s);
    for (ri j = 1; j <= n; ++j)
      if (s[j] == '1')
        e[i].pb(j);
  }
  check(1);
  int mx = 0;
  for (ri s = 1; s <= n; ++s) {
    memset(dep, 0, sizeof(dep));
    memset(vs, 0, sizeof(vs));
    queue<int> q;
    q.push(s), dep[s] = 1;
    while (q.size()) {
      int x = q.front();
      q.pop();
      vs[x] = 1;
      for (ri i = 0, v; i < e[x].size(); ++i) {
        if (vs[v = e[x][i]])
          continue;
        dep[v] = dep[x] + 1;
        vs[v] = 1;
        q.push(v);
      }
    }
    for (ri i = 1; i <= n; ++i)
      mx = max(mx, dep[i]);
  }
  cout << mx;
  return 0;
}