Problem p02892 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define sz(x) ((ll)x.size())
#define all(x) x.begin(), x.end()
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef long long ll;
typedef unsigned long long ul;

const int mn = 1e5 + 5;
const int mod = 1e9 + 7;

void bye() {
  puts("-1");
  exit(0);
}

int n;
char s[205][205];

stack<int> st;
int vis[205];
int pos[205];

void dfs(int x, int fa) {
  if (vis[x] == -1) {
    if ((sz(st) - pos[x]) % 2) {
      bye();
    }
    return;
  }
  vis[x] = -1;
  pos[x] = sz(st);
  st.push(x);
  for (int i = 1; i <= n; i++) {
    if (i != fa && s[x][i] == '1')
      dfs(i, x);
  }
  st.pop();
  vis[x] = 1;
}

int dist[205][205];

int main() {
  // cin.sync_with_stdio(0);
#ifdef LOCAL
  freopen("../1.txt", "r", stdin);
  // freopen("my.out", "w", stdout);
  size_t bg = clock();
#endif

  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%s", s[i] + 1);
  dfs(1, 0);
  memset(dist, 1, sizeof dist);
  for (int i = 1; i <= n; i++) {
    dist[i][i] = 0;
    for (int j = 1; j <= n; j++) {
      if (s[i][j] == '1')
        dist[i][j] = 1;
    }
  }
  for (int k = 1; k <= n; k++) {
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        dist[i][j] = min(dist[i][k] + dist[k][j], dist[i][j]);
      }
    }
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      if (dist[i][j] < 1e7)
        ans = max(dist[i][j], ans);
    }
  }
  printf("%d\n", ans + 1);

#ifdef LOCAL
  size_t ed = clock();
  printf("time: %f\n", (double)(ed - bg) / CLOCKS_PER_SEC);
#endif
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define sz(x) ((ll)x.size())
#define all(x) x.begin(), x.end()
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef long long ll;
typedef unsigned long long ul;

const int mn = 1e5 + 5;
const int mod = 1e9 + 7;

void bye() {
  puts("-1");
  exit(0);
}

int n;
char s[205][205];

stack<int> st;
int vis[205];
int pos[205];

void dfs(int x, int fa) {
  if (vis[x] == -1) {
    if ((sz(st) - pos[x]) % 2) {
      bye();
    }
    return;
  }
  vis[x] = -1;
  pos[x] = sz(st);
  st.push(x);
  for (int i = 1; i <= n; i++) {
    if (i != fa && s[x][i] == '1' && vis[i] != 1)
      dfs(i, x);
  }
  st.pop();
  vis[x] = 1;
}

int dist[205][205];

int main() {
  // cin.sync_with_stdio(0);
#ifdef LOCAL
  freopen("../1.txt", "r", stdin);
  // freopen("my.out", "w", stdout);
  size_t bg = clock();
#endif

  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%s", s[i] + 1);
  dfs(1, 0);
  memset(dist, 1, sizeof dist);
  for (int i = 1; i <= n; i++) {
    dist[i][i] = 0;
    for (int j = 1; j <= n; j++) {
      if (s[i][j] == '1')
        dist[i][j] = 1;
    }
  }
  for (int k = 1; k <= n; k++) {
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        dist[i][j] = min(dist[i][k] + dist[k][j], dist[i][j]);
      }
    }
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      if (dist[i][j] < 1e7)
        ans = max(dist[i][j], ans);
    }
  }
  printf("%d\n", ans + 1);

#ifdef LOCAL
  size_t ed = clock();
  printf("time: %f\n", (double)(ed - bg) / CLOCKS_PER_SEC);
#endif
  return 0;
}