Problem p03166 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long int
#define N 1e6
using namespace std;

/*******  All Required define Pre-Processors and typedef Constants *******/
#define SCD(t) scanf("%d", &t)
#define SCLD(t) scanf("%ld", &t)
#define SCLLD(t) scanf("%lld", &t)
#define SCC(t) scanf("%c", &t)
#define SCS(t) scanf("%s", t)
#define SCF(t) scanf("%f", &t)
#define SCLF(t) scanf("%lf", &t)
#define MEM(a, b) memset(a, (b), sizeof(a))
#define FOR(i, j, k, in) for (int i = j; i < k; i += in)
#define RFOR(i, j, k, in) for (int i = j; i >= k; i -= in)
#define REP(i, j) FOR(i, 0, j, 1)
#define RREP(i, j) RFOR(i, j, 0, 1)
#define all(cont) cont.begin(), cont.end()
#define rall(cont) cont.end(), cont.begin()
#define FOREACH(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define IN(A, B, C) assert(B <= A && A <= C)
#define MP make_pair
#define PB push_back
#define INF (int)1e9
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
#define read(type) readInt<type>()
const double pi = acos(-1.0);
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef map<int, int> MPII;
typedef set<int> SETI;
typedef multiset<int> MSETI;
typedef long int int32;
typedef unsigned long int uint32;
typedef unsigned long long int uint64;

vector<vector<int>> gV(100005 + 1);

int dP[100005] = {0};
bool vis[100005] = {false};

void dfs(int i) {

  if (vis[i] == true) {
    return;
  }

  vis[i] = true;
  FOR(j, 0, gV[i].size(), 1) {
    int nei = gV[i][j];

    if (!vis[nei]) {
      dfs(nei);
    }
    dP[i] = max(dP[i], dP[nei] + 1);
  }
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  ios_base::sync_with_stdio(false);
  cin.tie(0);

  int n, m;
  cin >> n >> m;

  FOR(i, 0, m, 1) {
    int x, y;
    cin >> x >> y;
    gV[x].PB(y);
  }

  FOR(i, 1, n + 1, 1) {
    if (!vis[i]) {
      dfs(i);
    }
  }
  int max = INT_MIN;

  FOR(i, 1, n + 1, 1) {
    if (dP[i] > max) {
      max = dP[i];
    }
  }

  cout << max << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long int
#define N 1e6
using namespace std;

/*******  All Required define Pre-Processors and typedef Constants *******/
#define SCD(t) scanf("%d", &t)
#define SCLD(t) scanf("%ld", &t)
#define SCLLD(t) scanf("%lld", &t)
#define SCC(t) scanf("%c", &t)
#define SCS(t) scanf("%s", t)
#define SCF(t) scanf("%f", &t)
#define SCLF(t) scanf("%lf", &t)
#define MEM(a, b) memset(a, (b), sizeof(a))
#define FOR(i, j, k, in) for (int i = j; i < k; i += in)
#define RFOR(i, j, k, in) for (int i = j; i >= k; i -= in)
#define REP(i, j) FOR(i, 0, j, 1)
#define RREP(i, j) RFOR(i, j, 0, 1)
#define all(cont) cont.begin(), cont.end()
#define rall(cont) cont.end(), cont.begin()
#define FOREACH(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define IN(A, B, C) assert(B <= A && A <= C)
#define MP make_pair
#define PB push_back
#define INF (int)1e9
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
#define read(type) readInt<type>()
const double pi = acos(-1.0);
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef map<int, int> MPII;
typedef set<int> SETI;
typedef multiset<int> MSETI;
typedef long int int32;
typedef unsigned long int uint32;
typedef unsigned long long int uint64;

vector<vector<int>> gV(100005 + 1);

int dP[100005] = {0};
bool vis[100005] = {false};

void dfs(int i) {

  if (vis[i] == true) {
    return;
  }

  vis[i] = true;
  FOR(j, 0, gV[i].size(), 1) {
    int nei = gV[i][j];

    if (!vis[nei]) {
      dfs(nei);
    }
    dP[i] = max(dP[i], dP[nei] + 1);
  }
}

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(0);

  int n, m;
  cin >> n >> m;

  FOR(i, 0, m, 1) {
    int x, y;
    cin >> x >> y;
    gV[x].PB(y);
  }

  FOR(i, 1, n + 1, 1) {
    if (!vis[i]) {
      dfs(i);
    }
  }
  int max = INT_MIN;

  FOR(i, 1, n + 1, 1) {
    if (dP[i] > max) {
      max = dP[i];
    }
  }

  cout << max << endl;
}