Problem p03166 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfenv>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
// #include <bits/stdc++.h>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
long long MOD = 1000000000 + 7; // 924844033 1000000000 + 9;
constexpr long long INF = numeric_limits<LL>::max();
const double PI = acos(-1);
#define fir first
#define sec second
#define thi third
#define debug(x) cerr << #x << ": " << x << '\n'
typedef pair<LL, LL> Pll;
typedef pair<double, double> Dll;
typedef pair<LL, pair<LL, LL>> Ppll;
typedef pair<LL, pair<LL, bitset<100001>>> Pbll;
typedef pair<LL, pair<LL, vector<LL>>> Pvll;
typedef pair<LL, LL> Vec2;
struct Tll {
  LL first, second, third;
};
struct Fll {
  LL first, second, third, fourth;
};
typedef pair<LL, Tll> Ptll;
#define rep(i, rept) for (LL i = 0; i < rept; i++)
#define Rrep(i, mf) for (LL i = mf - 1; i >= 0; i--)
LL h, w, n, m, k, t, s, p, q, last, first, cnt, sum, ans, dp[20100], a[220000],
    b[220000];
string str, ss;
bool f[220000];
char c[4000][4000];
int di[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
struct Edge {
  LL to, cost;
};
struct edge {
  LL from, to, cost;
};
vector<vector<Edge>> vec, rvec;
vector<edge> ed;
vector<LL> v;
map<string, LL> ma;
set<LL> st;

void YN(bool f) {
  if (f)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}
void yn(bool f) {
  if (f)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}
vector<LL> tsort(vector<vector<Edge>> vec) {
  enum { YET, VISITED, DONE };
  vector<LL> res, flg(n, YET);
  static const function<bool(int)> dfs = [&](int v) {
    flg[v] = VISITED;
    for (auto &e : vec[v]) {
      int w = e.to;
      if (flg[w] != DONE && (flg[w] == VISITED || !dfs(w)))
        return false;
    }
    flg[v] = DONE;
    res.push_back(v);
    return true;
  };
  for (int i = 0; i < n; ++i)
    if (flg[i] == YET && !dfs(i))
      return {};
  reverse(res.begin(), res.end());
  return res;
}
int main() {
  cin >> n >> m;
  vec.resize(n);
  rep(i, m) {
    LL x, y;
    cin >> x >> y;
    x--, y--;
    vec[x].push_back(Edge{y, 1});
  }
  vector<LL> x = tsort(vec);
  rep(i, n) {
    ans = max(ans, dp[x[i]]);
    rep(j, vec[x[i]].size()) {
      LL v = vec[x[i]][j].to;
      dp[v] = max(dp[x[i]] + 1, dp[v]);
    }
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfenv>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
// #include <bits/stdc++.h>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
long long MOD = 1000000000 + 7; // 924844033 1000000000 + 9;
constexpr long long INF = numeric_limits<LL>::max();
const double PI = acos(-1);
#define fir first
#define sec second
#define thi third
#define debug(x) cerr << #x << ": " << x << '\n'
typedef pair<LL, LL> Pll;
typedef pair<double, double> Dll;
typedef pair<LL, pair<LL, LL>> Ppll;
typedef pair<LL, pair<LL, bitset<100001>>> Pbll;
typedef pair<LL, pair<LL, vector<LL>>> Pvll;
typedef pair<LL, LL> Vec2;
struct Tll {
  LL first, second, third;
};
struct Fll {
  LL first, second, third, fourth;
};
typedef pair<LL, Tll> Ptll;
#define rep(i, rept) for (LL i = 0; i < rept; i++)
#define Rrep(i, mf) for (LL i = mf - 1; i >= 0; i--)
LL h, w, n, m, k, t, s, p, q, last, first, cnt, sum, ans, dp[201000], a[220000],
    b[220000];
string str, ss;
bool f[220000];
char c[4000][4000];
int di[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
struct Edge {
  LL to, cost;
};
struct edge {
  LL from, to, cost;
};
vector<vector<Edge>> vec, rvec;
vector<edge> ed;
vector<LL> v;
map<string, LL> ma;
set<LL> st;

void YN(bool f) {
  if (f)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}
void yn(bool f) {
  if (f)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}
vector<LL> tsort(vector<vector<Edge>> vec) {
  enum { YET, VISITED, DONE };
  vector<LL> res, flg(n, YET);
  static const function<bool(int)> dfs = [&](int v) {
    flg[v] = VISITED;
    for (auto &e : vec[v]) {
      int w = e.to;
      if (flg[w] != DONE && (flg[w] == VISITED || !dfs(w)))
        return false;
    }
    flg[v] = DONE;
    res.push_back(v);
    return true;
  };
  for (int i = 0; i < n; ++i)
    if (flg[i] == YET && !dfs(i))
      return {};
  reverse(res.begin(), res.end());
  return res;
}
int main() {
  cin >> n >> m;
  vec.resize(n);
  rep(i, m) {
    LL x, y;
    cin >> x >> y;
    x--, y--;
    vec[x].push_back(Edge{y, 1});
  }
  vector<LL> x = tsort(vec);
  rep(i, n) {
    ans = max(ans, dp[x[i]]);
    rep(j, vec[x[i]].size()) {
      LL v = vec[x[i]][j].to;
      dp[v] = max(dp[x[i]] + 1, dp[v]);
    }
  }
  cout << ans << endl;
  return 0;
}