Problem p02902 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define mk make_pair
#define fs first
#define sc second
using namespace std;
typedef long long ll;
typedef long double ld;
vector<int> a[1111], b[1111];
bool vis[1111], vis1[1111];
vector<int> v;
void dfs(int x) {
  if (vis[x])
    return;
  vis[x] = 1;
  for (int i = 0; i < a[x].size(); ++i) {
    dfs(a[x][i]);
  }
}
int tmp2;
int dfs1(int x) {
  if (vis[x])
    return 0;
  if (x == tmp2) {
    v.push_back(x);
    return 1;
  }
  vis[x] = 1;
  int tmp;
  for (int i = 0; i < a[x].size(); ++i) {
    tmp = dfs1(a[x][i]);
    if (tmp) {
      v.push_back(x);
      return 1;
    }
  }
  return 0;
}
int main() {
  int n, m;
  while (cin >> n >> m) {
    int x, y;
    for (int i = 0; i < m; ++i) {
      scanf("%d%d", &x, &y);
      a[x].push_back(y);
      b[y].push_back(x);
    }
    int tmp1 = -1;
    for (int i = 1; i <= n; ++i) {
      for (int j = 1; j <= n; ++j)
        vis[j] = 0;
      dfs(i);
      for (int j = 0; j < b[i].size(); ++j) {
        if (vis[b[i][j]]) {
          tmp1 = i;
          tmp2 = b[i][j];
          memset(vis, 0, sizeof vis);
          dfs1(i);
          break;
        }
      }
      if (tmp1 != -1)
        break;
    }
    if (tmp1 == -1) {
      cout << -1 << endl;
      continue;
    }
    reverse(v.begin(), v.end());
    memset(vis, 0, sizeof vis);
    for (int i = 0; i < v.size(); ++i) {
      vis[v[i]] = 1;
    }
    for (int k = 0; k < v.size(); ++k) {
      int i = v[k];
      for (int j = 0; j < a[i].size(); ++j) {
        if (vis[a[i][j]]) {
          while (k + 1 < v.size()) {
            if (v[k + 1] == a[i][j])
              break;
            vis[v[k + 1]] = 0;
            v.erase(v.begin() + k + 1);
          }
        }
      }
      vis[i] = 0;
    }
    //        memset(vis, 0, sizeof vis);
    for (int i = 0; i < v.size(); ++i) {
      vis[v[i]] = 1;
    }
    int mx = 0;
    for (int k = 0; k < v.size(); ++k) {
      int i = v[k];
      for (int j = 0; j < b[i].size(); ++j) {
        if (vis[b[i][j]]) {
          while (v.back() != b[i][j]) {
            v.pop_back();
          }
          mx = max(mx, k);
        }
      }
      vis[i] = 0;
    }
    cout << v.size() - mx << endl;
    for (int i = mx; i < v.size(); ++i) {
      cout << v[i] << endl;
    }
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define mk make_pair
#define fs first
#define sc second
using namespace std;
typedef long long ll;
typedef long double ld;
vector<int> a[1111], b[1111];
bool vis[1111], vis1[1111];
vector<int> v;
void dfs(int x) {
  if (vis[x])
    return;
  vis[x] = 1;
  for (int i = 0; i < a[x].size(); ++i) {
    dfs(a[x][i]);
  }
}
int tmp2;
int dfs1(int x) {
  if (vis[x])
    return 0;
  if (x == tmp2) {
    v.push_back(x);
    return 1;
  }
  vis[x] = 1;
  int tmp;
  for (int i = 0; i < a[x].size(); ++i) {
    tmp = dfs1(a[x][i]);
    if (tmp) {
      v.push_back(x);
      return 1;
    }
  }
  return 0;
}
int main() {
  int n, m;
  while (cin >> n >> m) {
    int x, y;
    for (int i = 0; i < m; ++i) {
      scanf("%d%d", &x, &y);
      a[x].push_back(y);
      b[y].push_back(x);
    }
    int tmp1 = -1;
    for (int i = 1; i <= n; ++i) {
      for (int j = 1; j <= n; ++j)
        vis[j] = 0;
      dfs(i);
      for (int j = 0; j < b[i].size(); ++j) {
        if (vis[b[i][j]]) {
          tmp1 = i;
          tmp2 = b[i][j];
          memset(vis, 0, sizeof vis);
          dfs1(i);
          break;
        }
      }
      if (tmp1 != -1)
        break;
    }
    if (tmp1 == -1) {
      cout << -1 << endl;
      continue;
    }
    reverse(v.begin(), v.end());
    memset(vis, 0, sizeof vis);
    for (int i = 0; i < v.size(); ++i) {
      vis[v[i]] = 1;
    }
    for (int k = 0; k < v.size(); ++k) {
      int i = v[k];
      for (int j = 0; j < a[i].size(); ++j) {
        if (vis[a[i][j]]) {
          while (k + 1 < v.size()) {
            if (v[k + 1] == a[i][j])
              break;
            vis[v[k + 1]] = 0;
            v.erase(v.begin() + k + 1);
          }
        }
      }
      vis[i] = 0;
    }
    //        memset(vis, 0, sizeof vis);
    for (int i = 0; i < v.size(); ++i) {
      vis[v[i]] = 1;
    }
    int mx = 0;
    for (int k = 0; k < v.size(); ++k) {
      int i = v[k];
      for (int j = 0; j < b[i].size(); ++j) {
        if (vis[b[i][j]]) {
          while (v.back() != b[i][j]) {
            vis[v.back()] = 0;
            v.pop_back();
          }
          mx = max(mx, k);
        }
      }
      vis[i] = 0;
    }
    cout << v.size() - mx << endl;
    for (int i = mx; i < v.size(); ++i) {
      cout << v[i] << endl;
    }
  }
  return 0;
}