Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
#pragma GCC optimize "03"
using namespace std;

#define int long long int
#define pb push_back
#define pii pair<int, int>
#define fi first
#define se second
#define rep(i, a, b) for (int i = a; i < b; ++i)
#define dbg(x)                                                                 \
  { cerr << "> " << #x << ": " << x << endl; }
#define dbg2(x, y)                                                             \
  { cerr << "> " << #x << ": " << x << " , " << #y << ": " << y << endl; }
#define dbg3(x, y, z)                                                          \
  {                                                                            \
    cerr << "> " << #x << ": " << x << " , " << #y << ": " << y << " , " << #z \
         << ": " << z << endl;                                                 \
  }
#define IOS                                                                    \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0)
#ifndef LOCAL
#define endl '\n'
#endif
const int inf = INT_MAX;
const double eps = 0.0000001;
const double PI = acos(-1.0);
const int MOD = 1e9 + 7;
const int N = 1e5 + 5;

vector<int> g[N];
int dp[N];

int solve(int n) {
  if (dp[n])
    return dp[n];
  for (auto i : g[n]) {
    dp[n] = max(dp[n], 1 + solve(i));
  }
  return dp[n];
}

signed main() {
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
#endif
  IOS;

  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      int a, b;
      cin >> a >> b;
      g[a].pb(b);
    }
  }
  int ans = 0;
  for (int i = 1; i <= n; i++)
    ans = max(ans, solve(i));
  cout << ans;
  return 0;
}

Fixed submission:
#include "bits/stdc++.h"
#pragma GCC optimize "03"
using namespace std;

#define int long long int
#define pb push_back
#define pii pair<int, int>
#define fi first
#define se second
#define rep(i, a, b) for (int i = a; i < b; ++i)
#define dbg(x)                                                                 \
  { cerr << "> " << #x << ": " << x << endl; }
#define dbg2(x, y)                                                             \
  { cerr << "> " << #x << ": " << x << " , " << #y << ": " << y << endl; }
#define dbg3(x, y, z)                                                          \
  {                                                                            \
    cerr << "> " << #x << ": " << x << " , " << #y << ": " << y << " , " << #z \
         << ": " << z << endl;                                                 \
  }
#define IOS                                                                    \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0)
#ifndef LOCAL
#define endl '\n'
#endif
const int inf = INT_MAX;
const double eps = 0.0000001;
const double PI = acos(-1.0);
const int MOD = 1e9 + 7;
const int N = 1e5 + 5;

vector<int> g[N];
int dp[N];

int solve(int n) {
  if (dp[n])
    return dp[n];
  for (auto i : g[n]) {
    dp[n] = max(dp[n], 1 + solve(i));
  }
  return dp[n];
}

signed main() {
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
#endif
  IOS;

  int n, m;
  cin >> n >> m;
  for (int j = 0; j < m; j++) {
    int a, b;
    cin >> a >> b;
    g[a].pb(b);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++)
    ans = max(ans, solve(i));
  cout << ans;
  return 0;
}