Problem p03166 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long int
#define pb push_back
const int M = 1e9 + 7;
using namespace std;
int N = 1000000 + 1;
void dfs(vector<int> *g, int st, bool *visited, vector<int> *ts) {
  visited[st] = true;
  for (int i = 0; i < g[st].size(); i++) {
    if (visited[g[st][i]] == false) {
      dfs(g, g[st][i], visited, ts);
    }
  }
  ts->pb(st);
}
int logestPath(vector<int> *g, vector<int> *ts, int n, int m) {
  int *dp = new int[n + 1];
  for (int i = 0; i <= n; i++) {
    dp[i] = 0;
  }
  int tss = ts->size();
  dp[ts->at(tss - 1)] = 0;
  //	for(int i=0;i<tss;i++)
  //	{
  //		cout<<ts->at(i)<<" ";
  //	}
  //	cout<<endl;
  for (int i = tss - 1; i >= 0; i--) {
    int curVertex = ts->at(i);
    // cout<<curVertex<<" ";
    for (int j = 0; j < g[curVertex].size(); j++) {
      dp[curVertex] = max(dp[curVertex], 1 + dp[g[curVertex][j]]);
    }
    // cout<<curVertex<<" "<<dp[curVertex]<<" ";
  }
  // cout<<endl;
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    ans = max(ans, dp[ts->at(i)]);
  }
  return ans;
}
int main() {

  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  vector<int> *g = new vector<int>[n + 1];
  for (int i = 1; i <= m; i++) {
    int u, v;
    cin >> u >> v;
    g[u].pb(v);
  }
  bool *visited = new bool[n + 1]();
  // memset(visited,0,n+1);
  vector<int> *ts = new vector<int>();
  int ans = 0;
  // cout<<"BREAKING POINT-1"<<endl;
  for (int i = 1; i <= n; i++) {
    if (visited[i] == false) {
      // ts=new vector<int>();
      dfs(g, i, visited, ts);
    }
  }
  reverse(ts->begin(), ts->end());
  ans = max(ans, logestPath(g, ts, n, m));
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long int
#define pb push_back
const int M = 1e9 + 7;
using namespace std;
int N = 1000000 + 1;
void dfs(vector<int> *g, int st, bool *visited, vector<int> *ts) {
  visited[st] = true;
  for (int i = 0; i < g[st].size(); i++) {
    if (visited[g[st][i]] == false) {
      dfs(g, g[st][i], visited, ts);
    }
  }
  ts->pb(st);
}
int logestPath(vector<int> *g, vector<int> *ts, int n, int m) {
  int *dp = new int[n + 1];
  for (int i = 0; i <= n; i++) {
    dp[i] = 0;
  }
  int tss = ts->size();
  dp[ts->at(tss - 1)] = 0;
  //	for(int i=0;i<tss;i++)
  //	{
  //		cout<<ts->at(i)<<" ";
  //	}
  //	cout<<endl;
  for (int i = tss - 1; i >= 0; i--) {
    int curVertex = ts->at(i);
    // cout<<curVertex<<" ";
    for (int j = 0; j < g[curVertex].size(); j++) {
      dp[curVertex] = max(dp[curVertex], 1 + dp[g[curVertex][j]]);
    }
    // cout<<curVertex<<" "<<dp[curVertex]<<" ";
  }
  // cout<<endl;
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    ans = max(ans, dp[i]);
  }
  return ans;
}
int main() {

  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  vector<int> *g = new vector<int>[n + 1];
  for (int i = 1; i <= m; i++) {
    int u, v;
    cin >> u >> v;
    g[u].pb(v);
  }
  bool *visited = new bool[n + 1]();
  // memset(visited,0,n+1);
  vector<int> *ts = new vector<int>();
  int ans = 0;
  // cout<<"BREAKING POINT-1"<<endl;
  for (int i = 1; i <= n; i++) {
    if (visited[i] == false) {
      // ts=new vector<int>();
      dfs(g, i, visited, ts);
    }
  }
  reverse(ts->begin(), ts->end());
  ans = max(ans, logestPath(g, ts, n, m));
  cout << ans << endl;
  return 0;
}