Problem p03166 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <utility>
#include <vector>

#define ALL(obj) (obj).begin(), (obj).end()
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (a); (b) <= i; i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = n; n <= i; i--)
#define ABS(a) ((a < 0) ? ((-1) * (a)) : (a))
#define elif else if
#define MOD 1000000007
#define INF (1 << 29)

using namespace std;

#define ld long double
#define ll long long

map<int, int> mpa, mpb;
typedef pair<ll, ll> P;
priority_queue<P, vector<P>, greater<P>> pque;

const int N_MAX = 100100;
vector<vector<int>> g;
int dp[N_MAX];
int N, M;
bool used[N_MAX];

int dfs(int s) {
  if (used[s])
    return dp[s];
  used[s] = true;
  int ret = 0;
  for (auto t : g[s]) {
    ret = max(ret, dfs(t) + 1);
  }
  return dp[s] = ret;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);

  cin >> N >> M;

  int x[N_MAX], y[N_MAX];
  for (int i = 0; i < N_MAX; i++) {
    used[i] = false;
  }

  /*for(int i=0;i<N_MAX;i++){
    for(int j=0;j<N_MAX;j++){
      x[i][j]=0;
      //if(i==j)x[i][j]==1;
    }
  }*/

  for (int i = 0; i < N_MAX; i++) {
    dp[i] = 0;
  }
  // int x,y;
  for (int i = 0; i < M; i++) {
    cin >> x[i] >> y[i];
    x[i]--;
    y[i]--;
    g[x[i]].push_back(y[i]);
  }

  int ans = 0;
  for (int i = 0; i < N; i++) {
    ans = max(ans, dfs(i));
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <utility>
#include <vector>

#define ALL(obj) (obj).begin(), (obj).end()
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (a); (b) <= i; i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = n; n <= i; i--)
#define ABS(a) ((a < 0) ? ((-1) * (a)) : (a))
#define elif else if
#define MOD 1000000007
#define INF (1 << 29)

using namespace std;

#define ld long double
#define ll long long

map<int, int> mpa, mpb;
typedef pair<ll, ll> P;
priority_queue<P, vector<P>, greater<P>> pque;

const int N_MAX = 100100;
vector<int> g[N_MAX];
int dp[N_MAX];
int N, M;
bool used[N_MAX];

int dfs(int s) {
  if (used[s])
    return dp[s];
  used[s] = true;
  int ret = 0;
  for (auto t : g[s]) {
    ret = max(ret, dfs(t) + 1);
  }
  return dp[s] = ret;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);

  cin >> N >> M;

  int x[N_MAX], y[N_MAX];
  for (int i = 0; i < N_MAX; i++) {
    used[i] = false;
  }

  /*for(int i=0;i<N_MAX;i++){
    for(int j=0;j<N_MAX;j++){
      x[i][j]=0;
      //if(i==j)x[i][j]==1;
    }
  }*/

  for (int i = 0; i < N_MAX; i++) {
    dp[i] = 0;
  }
  // int x,y;
  for (int i = 0; i < M; i++) {
    cin >> x[i] >> y[i];
    x[i]--;
    y[i]--;
    g[x[i]].push_back(y[i]);
  }

  int ans = 0;
  for (int i = 0; i < N; i++) {
    ans = max(ans, dfs(i));
  }
  cout << ans << endl;
  return 0;
}