Problem p03166 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <chrono>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

#define pii pair<int, int>
#define vi vector<int>
#define ll long long
#define pb push_back
#define mp make_pair
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL)
#define repf(i, st, end, inc) for (int i = st; i < end; i += inc)
#define repb(i, st, end, dec) for (int i = st; i >= end; i -= dec)

#define MOD 1000000007

#define trace1(a) cout << a << endl;
#define trace2(a, b) cout << a << " | " << b << endl;
#define trace3(a, b, c) cout << a << " | " << b << " | " << c << endl;
#define trace4(a, b, c, d)                                                     \
  cout << a << " | " << b << " | " << c << " | " << d << endl;

using namespace std;
using namespace std::chrono;

void dfs(int st, vector<vector<int>> &g, vector<int> &visited,
         vector<int> &topo_sort) {
  visited[st] = 1;
  for (int i = 0; i < g[st].size(); i++) {
    if (visited[i] == g[st][i])
      dfs(g[st][i], g, visited, topo_sort);
  }
  topo_sort.pb(st);
}

int main() {
  fast;
#ifdef LOCAL_PROJECT
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  auto start = high_resolution_clock::now();

  int n, m;
  cin >> n >> m;
  vector<vector<int>> g(n);
  vector<int> in_deg(n, 0);
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    g[x - 1].pb(y - 1);
    in_deg[y - 1]++;
  }
  vector<int> topo_sort;
  vector<int> visited(n, 0);

  for (int i = 0; i < n; i++) {
    if (in_deg[i] == 0)
      dfs(i, g, visited, topo_sort);
  }

  reverse(topo_sort.begin(), topo_sort.end());

  vector<int> dp(n, 0);

  for (int i = 0; i < n; i++) {
    int node = topo_sort[i];
    for (int j = 0; j < g[node].size(); j++) {
      dp[g[node][j]] = max(dp[g[node][j]], 1 + dp[node]);
    }
  }

  int ans = -1;
  for (int i = 0; i < n; i++)
    ans = max(ans, dp[i]);

  cout << ans << "\n";

  auto stop = high_resolution_clock::now();
  auto duration = duration_cast<microseconds>(stop - start);
  // cout << endl;
  // cout << fixed << setprecision(9) << double(duration.count()) / 1000000 <<
  // endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <chrono>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

#define pii pair<int, int>
#define vi vector<int>
#define ll long long
#define pb push_back
#define mp make_pair
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL)
#define repf(i, st, end, inc) for (int i = st; i < end; i += inc)
#define repb(i, st, end, dec) for (int i = st; i >= end; i -= dec)

#define MOD 1000000007

#define trace1(a) cout << a << endl;
#define trace2(a, b) cout << a << " | " << b << endl;
#define trace3(a, b, c) cout << a << " | " << b << " | " << c << endl;
#define trace4(a, b, c, d)                                                     \
  cout << a << " | " << b << " | " << c << " | " << d << endl;

using namespace std;
using namespace std::chrono;

void dfs(int st, vector<vector<int>> &g, vector<int> &visited,
         vector<int> &topo_sort) {
  visited[st] = 1;
  for (int i = 0; i < g[st].size(); i++) {
    if (visited[g[st][i]] == 0)
      dfs(g[st][i], g, visited, topo_sort);
  }
  topo_sort.pb(st);
}

int main() {
  fast;
#ifdef LOCAL_PROJECT
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  auto start = high_resolution_clock::now();

  int n, m;
  cin >> n >> m;
  vector<vector<int>> g(n);
  vector<int> in_deg(n, 0);
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    g[x - 1].pb(y - 1);
    in_deg[y - 1]++;
  }
  vector<int> topo_sort;
  vector<int> visited(n, 0);

  for (int i = 0; i < n; i++) {
    if (in_deg[i] == 0)
      dfs(i, g, visited, topo_sort);
  }

  reverse(topo_sort.begin(), topo_sort.end());

  vector<int> dp(n, 0);

  for (int i = 0; i < n; i++) {
    int node = topo_sort[i];
    for (int j = 0; j < g[node].size(); j++) {
      dp[g[node][j]] = max(dp[g[node][j]], 1 + dp[node]);
    }
  }

  int ans = -1;
  for (int i = 0; i < n; i++)
    ans = max(ans, dp[i]);

  cout << ans << "\n";

  auto stop = high_resolution_clock::now();
  auto duration = duration_cast<microseconds>(stop - start);
  // cout << endl;
  // cout << fixed << setprecision(9) << double(duration.count()) / 1000000 <<
  // endl;

  return 0;
}