Problem p00442 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
typedef pair<int, P> P1;

#define fr first
#define sc second
#define mp make_pair
#define pb push_back
#define rep(i, x) for (int i = 0; i < x; i++)
#define rep1(i, x) for (int i = 1; i <= x; i++)
#define rrep(i, x) for (int i = x - 1; i >= 0; i--)
#define rrep1(i, x) for (int i = x; i > 0; i--)
#define sor(v) sort(v.begin(), v.end())
#define rev(s) reverse(s.begin(), s.end())
#define lb(vec, a) lower_bound(vec.begin(), vec.end(), a)
#define ub(vec, a) upper_bound(vec.begin(), vec.end(), a)
#define uniq(vec) vec.erase(unique(vec.begin(), vec.end()), vec.end())
#define min_3(a, b, c) min(a, min(b, c))
#define max_3(a, b, c) max(a, max(b, c))
#define mp1(a, b, c) P1(a, P(b, c))
#define pque(a) priority_queue<a>
#define rpque(a) priority_queue<a, vector<a>, greater<a>>

const int INF = 1000000000;
const int dir_4[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
const int dir_8[8][2] = {{1, 0},  {1, 1},   {0, 1},  {-1, 1},
                         {-1, 0}, {-1, -1}, {0, -1}, {1, -1}};
const int kaijou[10] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880};

vector<int> G[5002], rG[5002];
int dp[5002];
bool used[5002];
int t = 0;

void dfs(int v) {
  int p = 0;
  rep(i, rG[v].size()) {
    if (!used[rG[v][i]]) {
      dfs(rG[v][i]);
    }
    p = max(p, dp[rG[v][i]]);
  }
  if (!used[v]) {
    used[v] = true;
    printf("%d\\n", v);
  }
  dp[v] = p + 1;
  t = max(t, dp[v]);
  rep(i, G[v].size()) {
    if (!used[G[v][i]]) {
      dfs(G[v][i]);
    }
  }
}

int main() {
  int n, m;
  scanf("%d%d", &n, &m);

  rep(i, m) {
    int x, y;
    scanf("%d%d", &x, &y);
    G[x].pb(y);
    rG[y].pb(x);
  }

  rep(i, 5002) {
    used[i] = false;
    dp[i] = 0;
  }

  rep1(i, n) {
    if (!used[i]) {
      dfs(i);
    }
  }

  if (t == n)
    puts("0");
  else
    puts("1");
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int n, m, w, l, ans = 0;
vector<int> a[5012], wor, nex;
int b[5012] = {0}, c[5012] = {0};

bool f(int x, int y) {
  if (c[x] > c[y])
    return false;
  return true;
}

int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) {
    scanf("%d%d", &w, &l);
    a[w].push_back(l);
    b[l]++;
  }
  for (int i = 1; i <= n; i++)
    if (b[i] == 0)
      nex.push_back(i);
  while (!nex.empty()) {
    wor = nex;
    nex.clear();
    int s = wor.size();
    for (int i = 0; i < s; i++) {
      int ss = a[wor[i]].size();
      for (int j = 0; j < ss; j++) {
        b[a[wor[i]][j]]--;
        if (b[a[wor[i]][j]] == 0) {
          c[a[wor[i]][j]] = c[wor[i]] + 1;
          nex.push_back(a[wor[i]][j]);
        }
      }
    }
  }
  wor.clear();
  for (int i = 1; i <= n; i++)
    wor.push_back(i);
  sort(wor.begin(), wor.end(), f);
  for (int i = 0; i < n - 1; i++) {
    printf("%d\\n", wor[i]);
    if (c[wor[i]] == c[wor[i + 1]])
      ans = 1;
  }
  printf("%d\\n%d\\n", wor[n - 1], ans);
  return 0;
}