prithivMLmods/Coder-Stat huggingface dataset:
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <assert.h>
#include <climits>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>

using namespace std;
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define all(x) (x).begin(), (x).end()
typedef long long ll;
typedef unsigned long long ull;

template <typename T> T gcd(T a, T b) {
  if (a < b)
    gcd(b, a);
  if (b == 1)
    return 1;
  T r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}

#define MOD (1000000000 + 7)

bool _less(pair<int, int> a, pair<int, int> b) { return a.second < b.second; }

int main() {
  int N, M;
  cin >> N >> M;
  vector<pair<int, int>> E;
  vector<int> inedges(N), outedges(N);
  vector<vector<int>> nextnodes(N);
  REP(i, M) {
    int x, y;
    cin >> x >> y;
    E.push_back(make_pair(x - 1, y - 1));
    inedges[y - 1]++;
    outedges[x - 1]++;
    nextnodes[x - 1].push_back(y - 1);
  }

  vector<int> dp(N);
  REP(i, N) {
    if (outedges[i] == 0)
      dp[i] = 1;
  }

  auto rec = [&](auto f, int n) mutable -> int {
    if (dp[n] > 0)
      return dp[n];
    int m = 0;
    REP(i, nextnodes[n].size()) {
      int nn = nextnodes[n][i];
      m = max(m, f(f, nn) + 1);
    }
    return m;
  };

  int cnt = 0;
  REP(i, N) {
    if (inedges[i] == 0) {
      cnt = max(cnt, rec(rec, i));
    }
  }

  cout << cnt - 1 << endl;
}


Fixed submission:
#include <algorithm>
#include <assert.h>
#include <climits>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>

using namespace std;
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define all(x) (x).begin(), (x).end()
typedef long long ll;
typedef unsigned long long ull;

template <typename T> T gcd(T a, T b) {
  if (a < b)
    gcd(b, a);
  if (b == 1)
    return 1;
  T r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}

#define MOD (1000000000 + 7)

bool _less(pair<int, int> a, pair<int, int> b) { return a.second < b.second; }

int main() {
  int N, M;
  cin >> N >> M;
  vector<pair<int, int>> E;
  vector<int> inedges(N), outedges(N);
  vector<vector<int>> nextnodes(N);
  REP(i, M) {
    int x, y;
    cin >> x >> y;
    E.push_back(make_pair(x - 1, y - 1));
    inedges[y - 1]++;
    outedges[x - 1]++;
    nextnodes[x - 1].push_back(y - 1);
  }

  vector<int> dp(N);
  REP(i, N) {
    if (outedges[i] == 0)
      dp[i] = 1;
  }

  auto rec = [&](auto f, int n) mutable -> int {
    if (dp[n] > 0)
      return dp[n];
    int m = 0;
    REP(i, nextnodes[n].size()) {
      int nn = nextnodes[n][i];
      m = max(m, f(f, nn) + 1);
    }
    dp[n] = m;
    return m;
  };

  int cnt = 0;
  REP(i, N) {
    if (inedges[i] == 0) {
      cnt = max(cnt, rec(rec, i));
    }
  }

  cout << cnt - 1 << endl;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional>
#define ff first
#define endl "\n"
#define ss second
#define li list<int>
#define vi vector<int>
#define ll long long int
#define pii pair<int, int>
#define vii vector<pair<int, int>>
#define mp make_pair
#define pb push_back
#define setbits(x) __builtin_popcountll(x)
#define custom pair<int, pair<int, int>>
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds;
#define mod 1000000007
int dp[100001];
vector<int> graph[100001];
int solve(int node) {
  if (dp[node] != -1) {
    return dp[node];
  }
  bool flag = true;
  int bestans = 0;
  for (auto child : graph[node]) {
    flag = false;
    bestans = max(bestans, solve(child));
  }
  return flag ? 0 : 1 + bestans;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  int x, y;
  for (int i = 0; i < m; i++) {
    cin >> x >> y;
    graph[x].pb(y);
  }
  memset(dp, -1, sizeof dp);
  int ans = INT_MIN;
  for (int i = 1; i <= n; i++) {
    ans = max(ans, solve(i));
  }
  cout << ans;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional>
#define ff first
#define endl "\n"
#define ss second
#define li list<int>
#define vi vector<int>
#define ll long long int
#define pii pair<int, int>
#define vii vector<pair<int, int>>
#define mp make_pair
#define pb push_back
#define setbits(x) __builtin_popcountll(x)
#define custom pair<int, pair<int, int>>
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds;
#define mod 1000000007
int dp[100001];
vector<int> graph[100001];
int solve(int node) {
  if (dp[node] != -1) {
    return dp[node];
  }
  bool flag = true;
  int bestans = 0;
  for (auto child : graph[node]) {
    flag = false;
    bestans = max(bestans, solve(child));
  }
  return dp[node] = flag ? 0 : 1 + bestans;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  int x, y;
  for (int i = 0; i < m; i++) {
    cin >> x >> y;
    graph[x].pb(y);
  }
  memset(dp, -1, sizeof dp);
  int ans = INT_MIN;
  for (int i = 1; i <= n; i++) {
    ans = max(ans, solve(i));
  }
  cout << ans;
  return 0;
}
Problem p03166 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define taskname ""
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define for0(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)

using namespace std;

typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;
typedef vector<cd> vcd;
typedef vector<int> vi;

mt19937 rng(chrono::system_clock::now().time_since_epoch().count());

const int maxN = 1e5;

int n, m;
vi adj[maxN];

int deg[maxN];
int dp[maxN];
bool flag[maxN];

void DFS(int u) {
  flag[u] = 1;
  dp[u] = 0;
  for (auto &v : adj[u]) {
    if (!flag[v]) {
      DFS(v);
    }
    dp[u] = max(dp[u], dp[v] + 1);
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> n >> m;
  while (m--) {
    int u, v;
    cin >> u >> v;
    adj[v].eb(u);
    ++deg[u];
  }
  for1(u, n) {
    if (!flag[u] && deg[u] == 0) {
      DFS(u);
    }
  }
  cout << *max_element(dp + 1, dp + n + 1);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

#define taskname ""
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define for0(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)

using namespace std;

typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;
typedef vector<cd> vcd;
typedef vector<int> vi;

mt19937 rng(chrono::system_clock::now().time_since_epoch().count());

const int maxN = 1e5 + 1;

int n, m;
vi adj[maxN];

int deg[maxN];
int dp[maxN];
bool flag[maxN];

void DFS(int u) {
  flag[u] = 1;
  dp[u] = 0;
  for (auto &v : adj[u]) {
    if (!flag[v]) {
      DFS(v);
    }
    dp[u] = max(dp[u], dp[v] + 1);
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> n >> m;
  while (m--) {
    int u, v;
    cin >> u >> v;
    adj[v].eb(u);
    ++deg[u];
  }
  for1(u, n) {
    if (!flag[u] && deg[u] == 0) {
      DFS(u);
    }
  }
  cout << *max_element(dp + 1, dp + n + 1);
  return 0;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <climits>
#include <fstream>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>

using namespace std;

typedef vector<vector<int>> matrix;
int N, M;

int solve_rec(matrix &G, int u, vector<int> &DP) {
  if (DP[u] != -1)
    return DP[u];
  int massimo = 0;
  for (int i = 0; i < G[u].size(); i++) {
    massimo = max(solve_rec(G, G[u][i], DP), massimo);
  }
  return massimo + 1;
}

int solve(matrix &G) {
  vector<int> DP(N, -1);
  int ret = -1;

  for (int i = 0; i < N; i++) {
    DP[i] = solve_rec(G, i, DP);
  }

  for (int i = 0; i < N; i++) {
    ret = max(ret, DP[i]);
    // cout << DP[i] << " (" << i << ")  " ;
  }

  return ret - 1;
}

int main(int argc, char **argv) {
  cin >> N >> M;

  matrix G(N, vector<int>());

  for (int i = 0; i < M; i++) {
    int p, e;
    cin >> p >> e;
    G[p - 1].push_back(e - 1);
  }

  cout << solve(G);
  return 0;
}

Fixed submission:
#include <climits>
#include <fstream>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>

using namespace std;

typedef vector<vector<int>> matrix;
int N, M;

int solve_rec(matrix &G, int u, vector<int> &DP) {
  if (DP[u] != -1)
    return DP[u];
  int massimo = 0;
  for (int i = 0; i < G[u].size(); i++) {
    massimo = max(solve_rec(G, G[u][i], DP), massimo);
  }
  DP[u] = massimo + 1;
  return massimo + 1;
}

int solve(matrix &G) {
  vector<int> DP(N, -1);
  int ret = -1;

  for (int i = 0; i < N; i++) {
    DP[i] = solve_rec(G, i, DP);
  }

  for (int i = 0; i < N; i++) {
    ret = max(ret, DP[i]);
    // cout << DP[i] << " (" << i << ")  " ;
  }

  return ret - 1;
}

int main(int argc, char **argv) {
  cin >> N >> M;

  matrix G(N, vector<int>());

  for (int i = 0; i < M; i++) {
    int p, e;
    cin >> p >> e;
    G[p - 1].push_back(e - 1);
  }

  cout << solve(G);
  return 0;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cmath>
#include <iostream>
#include <unistd.h>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;

int n, m;
vector<vector<int>> G;
vector<vector<int>> invG;
vector<int> memo;

int search(int v) {
  if (invG[v].size() == 0) {
    memo[v] = 0;
  } else {
    rep(i, invG[v].size()) { memo[v] = max(memo[v], search(invG[v][i]) + 1); }
  }
  return memo[v];
}

int main() {
  cin >> n >> m;
  G.resize(n);
  invG.resize(n);
  memo.resize(n);
  rep(i, n) memo[i] = -1;
  rep(i, m) {
    int x, y;
    cin >> x >> y;
    --x;
    --y;
    G[x].push_back(y);
    invG[y].push_back(x);
  }
  rep(i, n) search(i);
  int ans = 0;
  rep(i, n) ans = max(ans, memo[i]);
  cout << ans << endl;
}


Fixed submission:
#include <cmath>
#include <iostream>
#include <unistd.h>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;

int n, m;
vector<vector<int>> G;
vector<vector<int>> invG;
vector<int> memo;

int search(int v) {
  if (invG[v].size() == 0) {
    memo[v] = 0;
  } else {
    rep(i, invG[v].size()) {
      if (memo[invG[v][i]] != -1) {
        memo[v] = max(memo[v], memo[invG[v][i]] + 1);
        continue;
      }
      memo[v] = max(memo[v], search(invG[v][i]) + 1);
    }
  }
  return memo[v];
}

int main() {
  cin >> n >> m;
  G.resize(n);
  invG.resize(n);
  memo.resize(n);
  rep(i, n) memo[i] = -1;
  rep(i, m) {
    int x, y;
    cin >> x >> y;
    --x;
    --y;
    G[x].push_back(y);
    invG[y].push_back(x);
  }
  rep(i, n) search(i);
  int ans = 0;
  rep(i, n) ans = max(ans, memo[i]);
  cout << ans << endl;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

using ll = long long;

const int INF = 1e9 + 5;

template <typename T> void min_self(T &a, T b) { a = min(a, b); }

template <typename T> void max_self(T &a, T b) { a = max(a, b); }

const int Nax = 1e5 + 5;

vector<int> edges[Nax];
bool visited[Nax];
int dist[Nax];
int in[Nax];

void dfs(int a) {
  visited[a] = true;

  for (int b : edges[a]) {
    dist[b] = max(dist[b], 1 + dist[a]);

    dfs(b);
  }
}

int main() {

  ios::sync_with_stdio(0);
  cin.tie(0);

  int N, M;
  cin >> N >> M;

  int a, b;

  while (M--) {
    cin >> a >> b;
    edges[a].push_back(b);

    ++in[b];
  }

  for (int i = 1; i <= N; ++i) {
    if (!visited[i] && in[i] == 0) {
      dfs(i);
    }
  }

  cout << *max_element(dist + 1, dist + N + 1);
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

using ll = long long;

const int INF = 1e9 + 5;

template <typename T> void min_self(T &a, T b) { a = min(a, b); }

template <typename T> void max_self(T &a, T b) { a = max(a, b); }

const int Nax = 1e5 + 5;

vector<int> edges[Nax];
bool visited[Nax];
int dist[Nax];
int in[Nax];

void dfs(int a) {
  visited[a] = true;

  for (int b : edges[a]) {
    dist[b] = max(dist[b], 1 + dist[a]);

    if (--in[b] == 0) {
      dfs(b);
    }
  }
}

int main() {

  ios::sync_with_stdio(0);
  cin.tie(0);

  int N, M;
  cin >> N >> M;

  int a, b;

  while (M--) {
    cin >> a >> b;
    edges[a].push_back(b);

    ++in[b];
  }

  for (int i = 1; i <= N; ++i) {
    if (!visited[i] && in[i] == 0) {
      dfs(i);
    }
  }

  cout << *max_element(dist + 1, dist + N + 1);
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
//  @ /\/\ BESH //

#include <bits/stdc++.h>
#define inp_out_work                                                           \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define testCase                                                               \
  int T;                                                                       \
  cin >> T;                                                                    \
  while (T--)
#define fr(i, n) for (int i = 0; i < n; i++)
#define fro(i, s, e) for (int i = s; i <= e; i++)
#define endl '\n'
#define debug(P) cout << #P << " = " << P << endl;
#define pb push_back

using namespace std;

typedef long long ll;

const int MAXN = 100001;
const int mod = 1000000007;

vector<int> adj[MAXN];
vector<int> bdj[MAXN];

int mxdis[MAXN];

void dfs(int x, bool vis[]) {
  vis[x] = 1;
  for (int c : adj[x]) {
    dfs(c, vis);
    mxdis[x] = max(mxdis[x], mxdis[c] + 1);
  }
}

int solve(int n) {
  bool vis[n + 1];
  fill(vis, vis + n + 1, 0);

  int ans = 0;

  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      dfs(i, vis);
    }
    ans = max(ans, mxdis[i]);
  }

  return ans;
}

int main() {
  inp_out_work int n, m;
  cin >> n >> m;

  fr(i, m) {
    int u, v;
    cin >> u >> v;
    adj[u].pb(v);
    bdj[v].pb(u);
  }

  cout << solve(n);
}


Fixed submission:
//  @ /\/\ BESH //

#include <bits/stdc++.h>
#define inp_out_work                                                           \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define testCase                                                               \
  int T;                                                                       \
  cin >> T;                                                                    \
  while (T--)
#define fr(i, n) for (int i = 0; i < n; i++)
#define fro(i, s, e) for (int i = s; i <= e; i++)
#define endl '\n'
#define debug(P) cout << #P << " = " << P << endl;
#define pb push_back

using namespace std;

typedef long long ll;

const int MAXN = 100001;
const int mod = 1000000007;

vector<int> adj[MAXN];
vector<int> bdj[MAXN];

int mxdis[MAXN];

void dfs(int x, bool vis[]) {
  vis[x] = 1;
  for (int c : adj[x]) {
    if (!vis[c])
      dfs(c, vis);
    mxdis[x] = max(mxdis[x], mxdis[c] + 1);
  }
}

int solve(int n) {
  bool vis[n + 1];
  fill(vis, vis + n + 1, 0);

  int ans = 0;

  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      dfs(i, vis);
    }
    ans = max(ans, mxdis[i]);
  }

  return ans;
}

int main() {
  inp_out_work int n, m;
  cin >> n >> m;

  fr(i, m) {
    int u, v;
    cin >> u >> v;
    adj[u].pb(v);
    bdj[v].pb(u);
  }

  cout << solve(n);
}
Problem p03166 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
#define ll long long
vector<int> dp(100001);
int n, m, x, y;
int vis[100001] = {0};
vector<int> g[100001];
int solve(int vertex) {
  vis[vertex] = 1;
  dp[vertex] = 0;
  for (int child : g[vertex]) {
    if (!vis[child]) {
      solve(child);
    }
    dp[vertex] = max(dp[vertex], 1 + dp[child]);
  }
}
int main() {
  cin >> n >> m;
  for (ll i = 0; i < m; i++) {
    cin >> x >> y;
    g[x].push_back(y);
  }
  for (ll i = 1; i <= n; i++) {
    if (!vis[i]) {
      solve(i);
    }
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    ans = max(ans, dp[i]);
  }
  cout << ans;
}


Fixed submission:
#include "bits/stdc++.h"
using namespace std;
#define ll long long
vector<int> dp(100001);
int n, m, x, y;
int vis[100001] = {0};
vector<int> g[100001];
void solve(int vertex) {
  vis[vertex] = 1;
  dp[vertex] = 0;
  for (int child : g[vertex]) {
    if (!vis[child]) {
      solve(child);
    }
    dp[vertex] = max(dp[vertex], 1 + dp[child]);
  }
}
int main() {
  cin >> n >> m;
  for (ll i = 0; i < m; i++) {
    cin >> x >> y;
    g[x].push_back(y);
  }
  for (ll i = 1; i <= n; i++) {
    if (!vis[i]) {
      solve(i);
    }
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    ans = max(ans, dp[i]);
  }
  cout << ans;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define all(x) (x).begin(), (x).end()
#define int long long
#define fr first
#define sc second
#define vc vector
#define endl '\n'
#define pii pair<int, int>
#define msi map<string, int>
#define mii map<int, int>
#define psi pair<string, int>
#define pis pair<int, string>
#define mem1(a) memset(a, -1, sizeof(a))
#define mem0(a) memset(a, 0, sizeof(a))
#define rep(i, a, b) for (int i = a; i < b; i++)
#define LSB(i) ((i) & -(i))

template <typename T>
using oset =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

const int N = 1e5 + 5;
int n, m;
vc<int> g[N];
int dp[N];

int DFS(int v) {
  if (dp[v] != -1)
    return dp[v];
  int ans = 0;
  for (auto u : g[v]) {
    ans = max(ans, DFS(u) + 1);
  }
  return ans;
}

void solve() {
  cin >> n >> m;
  mem1(dp);
  for (int i = 1; i <= m; i++) {
    int x, y;
    cin >> x >> y;
    g[x].pb(y);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    ans = max(ans, DFS(i));
  }
  cout << ans;
}
signed main() {
  ios_base::sync_with_stdio(false);
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  int t = 1;
  // cin>>t;
  while (t--)
    solve();
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define all(x) (x).begin(), (x).end()
#define int long long
#define fr first
#define sc second
#define vc vector
#define endl '\n'
#define pii pair<int, int>
#define msi map<string, int>
#define mii map<int, int>
#define psi pair<string, int>
#define pis pair<int, string>
#define mem1(a) memset(a, -1, sizeof(a))
#define mem0(a) memset(a, 0, sizeof(a))
#define rep(i, a, b) for (int i = a; i < b; i++)
#define LSB(i) ((i) & -(i))

template <typename T>
using oset =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

const int N = 1e5 + 5;
int n, m;
vc<int> g[N];
int dp[N];

int DFS(int v) {
  if (dp[v] != -1)
    return dp[v];
  int ans = 0;
  for (auto u : g[v]) {
    ans = max(ans, DFS(u) + 1);
  }
  dp[v] = ans;
  return ans;
}

void solve() {
  cin >> n >> m;
  mem1(dp);
  for (int i = 1; i <= m; i++) {
    int x, y;
    cin >> x >> y;
    g[x].pb(y);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    ans = max(ans, DFS(i));
  }
  cout << ans;
}
signed main() {
  ios_base::sync_with_stdio(false);
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  int t = 1;
  // cin>>t;
  while (t--)
    solve();
  return 0;
}
Problem p03166 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <cstdio>
using namespace std;
#define ll long long
#define PB push_back
#define ld long double
#define ff first
#define ss second
#define st(v) (v).begin(), (v).end()
#define pr pair<int, int>
const int N = 505, M = N, inf = (int)1e9;
const ll mod = 1e9 + 7;
int fastAbs(int n) { return (n ^ (n >> 31)) - (n >> 31); }
ll multiply(ll a, ll b) { return ((a % mod) * (b % mod)) % mod; }
ll add(ll a, ll b) { return ((a % mod) + (b % mod)) % mod; }
ll sub(ll a, ll b) { return ((a % mod) - (b % mod) + mod) % mod; }
int ans = INT_MAX;
map<pair<int, int>, int> mp;
const int dx[4] = {1, 0, 0, -1};
const int dy[4] = {0, -1, 1, 0};
vector<int> tree[N];
vector<bool> visited(N, 0);
vector<int> long_path(N, 0);
void dfs(int x) {
  visited[x] = true;
  for (int child = 0; child < tree[x].size(); child++) {
    if (!visited[tree[x][child]])
      dfs(tree[x][child]);
    long_path[x] = max(long_path[x], long_path[tree[x][child]] + 1);
  }
}
void solve() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    tree[x].PB(y);
  }
  for (int i = 1; i <= n; i++) {
    if (!visited[i])
      dfs(i);
  }
  cout << *max_element(st(long_path));
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int q = 1;
  // cin >> q;
  while (q--)
    solve();
}


Fixed submission:
#include <bits/stdc++.h>
#include <cstdio>
using namespace std;
#define ll long long
#define PB push_back
#define ld long double
#define ff first
#define ss second
#define st(v) (v).begin(), (v).end()
#define pr pair<int, int>
const int N = 1e5 + 4, M = N, inf = (int)1e9;
const ll mod = 1e9 + 7;
int fastAbs(int n) { return (n ^ (n >> 31)) - (n >> 31); }
ll multiply(ll a, ll b) { return ((a % mod) * (b % mod)) % mod; }
ll add(ll a, ll b) { return ((a % mod) + (b % mod)) % mod; }
ll sub(ll a, ll b) { return ((a % mod) - (b % mod) + mod) % mod; }
int ans = INT_MAX;
map<pair<int, int>, int> mp;
const int dx[4] = {1, 0, 0, -1};
const int dy[4] = {0, -1, 1, 0};
vector<int> tree[N];
vector<bool> visited(N, 0);
vector<int> long_path(N, 0);
void dfs(int x) {
  visited[x] = true;
  for (int child = 0; child < tree[x].size(); child++) {
    if (!visited[tree[x][child]])
      dfs(tree[x][child]);
    long_path[x] = max(long_path[x], long_path[tree[x][child]] + 1);
  }
}
void solve() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    tree[x].PB(y);
  }
  for (int i = 1; i <= n; i++) {
    if (!visited[i])
      dfs(i);
  }
  cout << *max_element(st(long_path));
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int q = 1;
  // cin >> q;
  while (q--)
    solve();
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <vector>

using namespace std;

#define ll long long

const int MAX = 1e5 + 7;

vector<int> graph[MAX];
bool vis[MAX];
int dp[MAX];

int dfs(int s) {
  if (vis[s])
    return dp[s];
  for (int i = 0; i < graph[s].size(); ++i) {
    int t = graph[s][i];
    dp[s] = max(dp[s], dfs(t) + 1);
  }

  return dp[s];
}

int main() {
  // freopen("tree.in","r",stdin);
  // freopen("tree.out","w",stdout);
  // freopen("input.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);
  ios_base::sync_with_stdio(0);
  // cin.tie(0);

  int N, M;
  cin >> N >> M;

  for (int i = 0; i < M; ++i) {
    int u, v;
    cin >> u >> v;
    graph[u].push_back(v);
  }

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


Fixed submission:
#include <iostream>
#include <vector>

using namespace std;

#define ll long long

const int MAX = 1e5 + 7;

vector<int> graph[MAX];
bool vis[MAX];
int dp[MAX];

int dfs(int s) {
  if (vis[s])
    return dp[s];
  vis[s] = true;
  for (int i = 0; i < graph[s].size(); ++i) {
    int t = graph[s][i];
    dp[s] = max(dp[s], dfs(t) + 1);
  }

  return dp[s];
}

int main() {
  // freopen("tree.in","r",stdin);
  // freopen("tree.out","w",stdout);
  // freopen("input.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);
  ios_base::sync_with_stdio(0);
  // cin.tie(0);

  int N, M;
  cin >> N >> M;

  for (int i = 0; i < M; ++i) {
    int u, v;
    cin >> u >> v;
    graph[u].push_back(v);
  }

  int ans = 0;
  for (int i = 1; i <= N; ++i) {
    ans = max(ans, dfs(i));
    // cout << ans << endl;
  }
  cout << ans << endl;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):

#include <bits/stdc++.h>
#include <dirent.h>
#include <stdlib.h>
using namespace std;
#define sp << " " <<
typedef vector<int> vi;
typedef vector<long int> vli;
typedef vector<long long int> vlli;
typedef long int li;
typedef long long int lli;
typedef vector<pair<int, int>> vpi;
typedef vector<pair<lli, lli>> vplli;
typedef unordered_map<int, int> ump;

int largest(vector<vector<int>> adjList, vector<int> &memo, int source) {
  if (memo[source] != -1) {
    return memo[source];
  }
  int ans = -1;

  for (int val : adjList[source]) {
    ans = max(ans, largest(adjList, memo, val));
  }
  memo[source] = ans + 1;
  return ans + 1;
}

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> adjList(n);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    adjList[a - 1].push_back(b - 1);
  }

  vector<int> memo(n, -1);
  int maxi = 0;

  for (int i = 0; i < n; i++) {
    maxi = max(maxi, largest(adjList, memo, i));
  }
  cout << maxi << endl;

  return 0;
}

Fixed submission:

#include <bits/stdc++.h>
#include <dirent.h>
#include <stdlib.h>
using namespace std;
#define sp << " " <<
typedef vector<int> vi;
typedef vector<long int> vli;
typedef vector<long long int> vlli;
typedef long int li;
typedef long long int lli;
typedef vector<pair<int, int>> vpi;
typedef vector<pair<lli, lli>> vplli;
typedef unordered_map<int, int> ump;

int largest(vector<vector<int>> &adjList, vector<int> &memo, int source) {
  if (memo[source] != -1) {
    return memo[source];
  }
  int ans = -1;

  for (int val : adjList[source]) {
    ans = max(ans, largest(adjList, memo, val));
  }
  memo[source] = ans + 1;
  return ans + 1;
}

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> adjList(n);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    adjList[a - 1].push_back(b - 1);
  }

  vector<int> memo(n, -1);
  int maxi = 0;

  for (int i = 0; i < n; i++) {
    maxi = max(maxi, largest(adjList, memo, i));
  }
  cout << maxi << endl;

  return 0;
}
Problem p03166 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <queue>
#include <string>
#include <vector>
#define inp(X) cin >> X
#define out(X) cout << X << endl
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, f, n) for (int i = f; i < n; i++)
#define MAX(A) *max_element(A.begin(), A.end())
#define MIN(A) *min_element(A.begin(), A.end())
#define SORT(A) sort(A.begin(), A.end())
using namespace std;

typedef long long int ll;
#define MOD 1000000007
#define INF 1000000001
vector<vector<int>> g;

ll gcd(ll x, ll y) {
  if (y == 0)
    return x;
  return gcd(y, x % y);
}

bool bfs(vector<int> &dist, int sv) {
  queue<int> q;
  q.push(sv);
  dist[sv] = 0;
  while (!q.empty()) {
    int now = q.front();
    q.pop();
    for (auto v : g[now]) {
      if (dist[v] != -1)
        continue;
      dist[v] = dist[now] + 1;
      q.push(v);
    }
  }
}

int rec(vector<int> &dp, int sv) {
  if (dp[sv] != -1)
    return dp[sv];
  if (g[sv].empty())
    return dp[sv] = 0;

  int ret = -1;
  for (int v : g[sv])
    ret = max(ret, rec(dp, g[sv][v]) + 1);
  return dp[sv] = ret;
}

int main(void) {
  int n, m;
  cin >> n >> m;
  g.assign(n, vector<int>());
  vector<int> dp(n, -1);
  rep(i, m) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    g[x].push_back(y);
  }
  int ans = -1;
  rep(i, n) ans = max(ans, rec(dp, i));
  out(ans);
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <queue>
#include <string>
#include <vector>
#define inp(X) cin >> X
#define out(X) cout << X << endl
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, f, n) for (int i = f; i < n; i++)
#define MAX(A) *max_element(A.begin(), A.end())
#define MIN(A) *min_element(A.begin(), A.end())
#define SORT(A) sort(A.begin(), A.end())
using namespace std;

typedef long long int ll;
#define MOD 1000000007
#define INF 1000000001
vector<vector<int>> g;

ll gcd(ll x, ll y) {
  if (y == 0)
    return x;
  return gcd(y, x % y);
}

bool bfs(vector<int> &dist, int sv) {
  queue<int> q;
  q.push(sv);
  dist[sv] = 0;
  while (!q.empty()) {
    int now = q.front();
    q.pop();
    for (auto v : g[now]) {
      if (dist[v] != -1)
        continue;
      dist[v] = dist[now] + 1;
      q.push(v);
    }
  }
}

int rec(vector<int> &dp, int sv) {
  if (dp[sv] != -1)
    return dp[sv];
  if (g[sv].empty())
    return dp[sv] = 0;

  int ret = -1;
  for (int v : g[sv])
    ret = max(ret, rec(dp, v) + 1);
  return dp[sv] = ret;
}

int main(void) {
  int n, m;
  cin >> n >> m;
  g.assign(n, vector<int>());
  vector<int> dp(n, -1);
  rep(i, m) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    g[x].push_back(y);
  }
  int ans = -1;
  rep(i, n) ans = max(ans, rec(dp, i));
  out(ans);
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

int n, m;
vector<int> a[100002];
int f[100002];
int vis[100002];
int res = 0;
int cal(int u) {
  vis[u] = 1;
  for (int i = 0; i < a[u].size(); i++) {
    if (vis[a[u][i]])
      continue;
    f[u] = max(f[u], cal(a[u][i]) + 1);
    vis[a[u][i]] = 0;
  }
  res = max(res, f[u]);
  return f[u];
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (int i = 1, x, y; i <= m; i++) {
    cin >> x >> y;
    a[x].push_back(y);
  }
  for (int i = 1; i <= n; i++) {
    if (f[i] != 0)
      continue;
    f[i] = 0;
    cal(i);
    vis[i] = 0;
  }
  cout << res;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int n, m;
vector<int> a[100002];
int f[100002];
int vis[100002];
int res = 0;
int cal(int u) {
  if (f[u] != 0)
    return f[u];
  vis[u] = 1;
  for (int i = 0; i < a[u].size(); i++) {
    if (vis[a[u][i]])
      continue;
    f[u] = max(f[u], cal(a[u][i]) + 1);
    vis[a[u][i]] = 0;
  }
  res = max(res, f[u]);
  return f[u];
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (int i = 1, x, y; i <= m; i++) {
    cin >> x >> y;
    a[x].push_back(y);
  }
  for (int i = 1; i <= n; i++) {
    if (f[i] != 0)
      continue;
    f[i] = 0;
    cal(i);
    vis[i] = 0;
  }
  cout << res;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
vector<int> v[100001];
int level[100001];
bool visited[100001];
void dfs(int u) {
  visited[u] = true;
  for (int i = 0; i < v[u].size(); i++) {
    level[v[u][i]] = max(level[v[u][i]], level[u] + 1);
    dfs(v[u][i]);
  }
}
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i <= n; i++) {
    level[i] = 0;
    visited[i] = false;
  }
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    v[a].push_back(b);
  }
  for (int i = 1; i <= n; i++) {
    if (!visited[i])
      dfs(i);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {

    ans = max(ans, level[i]);
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
vector<int> v[100001];
int level[100001];
bool visited[100001];
void dfs(int u) {
  visited[u] = true;
  for (int i = 0; i < v[u].size(); i++) {
    if (!visited[v[u][i]])
      dfs(v[u][i]);
    level[u] = max(level[v[u][i]] + 1, level[u]);
  }
}
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i <= n; i++) {
    level[i] = 0;
    visited[i] = false;
  }
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    v[a].push_back(b);
  }
  for (int i = 1; i <= n; i++) {
    if (!visited[i])
      dfs(i);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {

    ans = max(ans, level[i]);
  }
  cout << ans << endl;
  return 0;
}
Problem p03166 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#define ios                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
typedef long long int ll;
using namespace std;
const int N = 3e3 + 5;
#define fi first
#define se second
#define pb push_back
#define endl "\n"
ll n, m;
vector<ll> adj[N];
ll cnt[N];
ll ans;
ll dfs(ll u) {
  ll &g = cnt[u];
  if (g != -1)
    return g;
  g = 0;
  for (auto f : adj[u]) {
    g = max(g, dfs(f) + 1);
  }
  return g;
}
int main() {
  cin >> n >> m;
  for (ll i = 1; i <= n; ++i)
    cnt[i] = -1;
  for (ll i = 0; i < m; ++i) {
    ll u, v;
    cin >> u >> v;
    adj[u].pb(v);
    // adj[v].pb(u);
  }
  for (ll i = 1; i <= n; ++i) {
    if (cnt[i] == -1) {
      ans = max(ans, dfs(i));
    }
  }
  // cout<<endl;
  // for(ll i=1;i<=n;++i)
  // cout<<cnt[i]<<" ";
  cout << ans;
}


Fixed submission:
#include <bits/stdc++.h>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#define ios                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
typedef long long int ll;
using namespace std;
const int N = 1e5 + 5;
#define fi first
#define se second
#define pb push_back
#define endl "\n"
ll n, m;
vector<ll> adj[N];
ll cnt[N];
ll ans;
ll dfs(ll u) {
  ll &g = cnt[u];
  if (g != -1)
    return g;
  g = 0;
  for (auto f : adj[u]) {
    g = max(g, dfs(f) + 1);
  }
  return g;
}
int main() {
  cin >> n >> m;
  for (ll i = 1; i <= n; ++i)
    cnt[i] = -1;
  for (ll i = 0; i < m; ++i) {
    ll u, v;
    cin >> u >> v;
    adj[u].pb(v);
    // adj[v].pb(u);
  }
  for (ll i = 1; i <= n; ++i) {
    if (cnt[i] == -1) {
      ans = max(ans, dfs(i));
    }
  }
  // cout<<endl;
  // for(ll i=1;i<=n;++i)
  // cout<<cnt[i]<<" ";
  cout << ans;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ll long long int
#define vi vector<int>
#define vll vector<ll>
#define vvi vector<vi>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define mod 1000000007
#define inf 1000000000000000001
#define all(c) c.begin(), c.end()
#define mp(x, y) make_pair(x, y)
#define mem(a, val) memset(a, val, sizeof(a))
#define eb emplace_back
#define pb push_back
#define f first
#define s second
#define fast_cin                                                               \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define precise                                                                \
  fixed(cout);                                                                 \
  cout << setprecision(16);
#define vb vector<bool>
using namespace std;

ostream &operator<<(ostream &out, vector<int> &v) {
  for (auto a : v)
    out << a << " ";
  out << endl;
}

const int maxn = 100005;
vi G[maxn];
bool visited[maxn];
ll dp[maxn];

int dfs(int src) {
  if (G[src].empty())
    return dp[src] = 0;

  int len = 0;

  for (auto v : G[src]) {
    len = max(len, 1 + dfs(v));
  }
  return dp[src] = len;
}

int main() {
  fast_cin int n, m;

  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int u, v;
    cin >> u >> v;
    G[u].pb(v);
  }
  int lmax = 0;
  for (int i = 1; i <= n; i++) {
    lmax = max(lmax, dfs(i));
  }
  cout << lmax << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long int
#define vi vector<int>
#define vll vector<ll>
#define vvi vector<vi>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define mod 1000000007
#define inf 1000000000000000001
#define all(c) c.begin(), c.end()
#define mp(x, y) make_pair(x, y)
#define mem(a, val) memset(a, val, sizeof(a))
#define eb emplace_back
#define pb push_back
#define f first
#define s second
#define fast_cin                                                               \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define precise                                                                \
  fixed(cout);                                                                 \
  cout << setprecision(16);
#define vb vector<bool>
using namespace std;

ostream &operator<<(ostream &out, vector<int> &v) {
  for (auto a : v)
    out << a << " ";
  out << endl;
}

const int maxn = 100005;
vi G[maxn];
bool visited[maxn];
ll dp[maxn];

int dfs(int src) {
  if (dp[src])
    return dp[src];
  if (G[src].empty())
    return dp[src] = 0;

  int len = 0;

  for (auto v : G[src]) {
    len = max(len, 1 + dfs(v));
  }
  return dp[src] = len;
}

int main() {
  fast_cin int n, m;

  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int u, v;
    cin >> u >> v;
    G[u].pb(v);
  }
  int lmax = 0;
  for (int i = 1; i <= n; i++) {
    lmax = max(lmax, dfs(i));
  }
  cout << lmax << endl;
}
Problem p03166 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
using namespace std;
int N, M;
int dp[100010];
vector<int> G[100010];

int rec(int v) {
  if (dp[v] != -1)
    return dp[v];
  int res = 0;
  for (int u : G[v]) {
    res = max(res, rec(u) + 1);
  }
  return dp[v] = res;
}

int main() {
  cin >> N >> M;
  for (int i = 0; i < N; i++) {
    int x, y;
    cin >> x >> y;
    x--, y--;
    G[x].push_back(y);
  }

  fill(dp, dp + N, -1);
  int ans = 0;
  for (int i = 0; i < N; i++)
    ans = max(ans, rec(i));
  cout << ans << endl;
}

Fixed submission:
#include <iostream>
#include <vector>
using namespace std;
int N, M;
int dp[100010];
vector<int> G[100010];

int rec(int v) {
  if (dp[v] != -1)
    return dp[v];
  int res = 0;
  for (int u : G[v]) {
    res = max(res, rec(u) + 1);
  }
  return dp[v] = res;
}

int main() {
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    int x, y;
    cin >> x >> y;
    x--, y--;
    G[x].push_back(y);
  }

  fill(dp, dp + N, -1);
  int ans = 0;
  for (int i = 0; i < N; i++)
    ans = max(ans, rec(i));
  cout << ans << endl;
}
Problem p03166 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll d[10005];
using Graph = vector<vector<ll>>;
vector<ll> topological_sort(const Graph &G, vector<ll> &indeg, ll V) {
  vector<ll> sorted;
  queue<ll> que;
  for (int i = 0; i < V; i++)
    if (indeg[i] == 0)
      que.push(i);
  while (!que.empty()) {
    ll v = que.front();
    que.pop();
    sorted.push_back(v);
    for (auto u : G[v]) {
      indeg[u]--;
      if (indeg[u] == 0)
        que.push(u);
    }
  }
  return sorted;
}
ll N, M;
int main() {
  cin >> N >> M;
  Graph G(N);
  vector<ll> indeg(N);
  for (int i = 0; i < M; i++) {
    ll s, t;
    cin >> s >> t;
    s--;
    t--;
    G[s].push_back(t);
    indeg[t]++;
  }
  vector<ll> A = topological_sort(G, indeg, N);
  for (int i = 0; i < N; i++) {
    ll v = A[i];
    for (auto u : G[v]) {
      d[u] = max(d[v] + 1, d[u]);
    }
  }
  ll ans = 0;
  for (int i = 0; i < N; i++)
    if (ans < d[i])
      ans = d[i];
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll d[100005];
using Graph = vector<vector<ll>>;
vector<ll> topological_sort(const Graph &G, vector<ll> &indeg, ll V) {
  vector<ll> sorted;
  queue<ll> que;
  for (int i = 0; i < V; i++)
    if (indeg[i] == 0)
      que.push(i);
  while (!que.empty()) {
    ll v = que.front();
    que.pop();
    sorted.push_back(v);
    for (auto u : G[v]) {
      indeg[u]--;
      if (indeg[u] == 0)
        que.push(u);
    }
  }
  return sorted;
}
ll N, M;
int main() {
  cin >> N >> M;
  Graph G(N);
  vector<ll> indeg(N);
  for (int i = 0; i < M; i++) {
    ll s, t;
    cin >> s >> t;
    s--;
    t--;
    G[s].push_back(t);
    indeg[t]++;
  }
  vector<ll> A = topological_sort(G, indeg, N);
  for (int i = 0; i < N; i++) {
    ll v = A[i];
    for (auto u : G[v]) {
      d[u] = max(d[v] + 1, d[u]);
    }
  }
  ll ans = 0;
  for (int i = 0; i < N; i++)
    if (ans < d[i])
      ans = d[i];
  cout << ans << endl;
  return 0;
}
Problem p03166 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define F(i, a, n) for (int i = a; i < n; i++)
#define INF 1000000007
using namespace std;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

vector<vector<int>> G;
int dp[100010];

int rec(int v) {
  if (dp[v] != -1)
    return dp[v];
  int res = 0;
  for (auto nv : G[v]) {
    chmax(res, rec(nv) + 1);
  }
  return dp[v] = res;
}

int main() {
  int n, m;
  cin >> n >> m;
  G.assign(n, vector<int>());
  vector<int> x(m), y(m);
  rep(i, m) {
    cin >> x[i] >> y[i];
    G[x[i]].push_back(y[i]);
  }
  rep(i, n) dp[i] = -1;
  int res = 0;
  rep(i, n) chmax(res, rec(i));
  cout << res << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define F(i, a, n) for (int i = a; i < n; i++)
#define INF 1000000007
using namespace std;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

vector<vector<int>> G;
int dp[100010];

int rec(int v) {
  if (dp[v] != -1)
    return dp[v];
  int res = 0;
  for (auto nv : G[v]) {
    chmax(res, rec(nv) + 1);
  }
  return dp[v] = res;
}

int main() {
  int n, m;
  cin >> n >> m;
  G.assign(n, vector<int>());
  vector<int> x(m), y(m);
  rep(i, m) {
    cin >> x[i] >> y[i];
    x[i]--;
    y[i]--;
    G[x[i]].push_back(y[i]);
  }
  rep(i, n) dp[i] = -1;
  int res = 0;
  rep(i, n) chmax(res, rec(i));
  cout << res << endl;
  return 0;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
// #include <bits/stdc++.h>
#include <algorithm>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <unordered_map>
#include <vector>
#define ii int64_t
#define PI 3.14
#define lli long long int
#define li long int
#define deb(x) cout << "\nhi" << x << "\n"
#define mod 1000000007
#define f(x, n) for (int x = 0; x < n; ++x)
#define ff(x, y, z) for (x = y; x <= z; ++x)
#define fio                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
// while(scanf("%d",&n)!=EOF)

using namespace std;

vector<vector<int>> v(100005);
int dp[100005];

int dhundhle(int i) {
  // deb(1);
  if (v[i].empty())
    return 0;

  if (dp[i] >= 0)
    return dp[i];

  int ans = -1;
  for (int j = 0; j < v[i].size(); j++) {
    ans = max(ans, dhundhle(v[i][j]) + 1);
  }
  return ans;
}

int main()

{
  fio int n, m, i, j, x, y;
  cin >> n >> m;
  for (i = 0; i < m; i++) {
    cin >> x >> y;
    v[x].push_back(y);
  }
  // deb(2);
  memset(dp, -1, sizeof(dp));

  int ans = -1;
  for (i = 1; i <= n; i++) {
    ans = max(ans, dhundhle(i));
  }
  cout << ans;
}


Fixed submission:
// #include <bits/stdc++.h>
#include <algorithm>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <unordered_map>
#include <vector>
#define ii int64_t
#define PI 3.14
#define lli long long int
#define li long int
#define deb(x) cout << "\nhi" << x << "\n"
#define mod 1000000007
#define f(x, n) for (int x = 0; x < n; ++x)
#define ff(x, y, z) for (x = y; x <= z; ++x)
#define fio                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
// while(scanf("%d",&n)!=EOF)

using namespace std;

vector<vector<int>> v(100005);
int dp[100005];

int dhundhle(int i) {
  // deb(1);
  if (v[i].empty())
    return 0;

  if (dp[i] >= 0)
    return dp[i];

  int ans = -1;
  for (int j = 0; j < v[i].size(); j++) {
    ans = max(ans, dhundhle(v[i][j]) + 1);
    dp[i] = ans;
  }
  return ans;
}

int main()

{
  fio int n, m, i, j, x, y;
  cin >> n >> m;
  for (i = 0; i < m; i++) {
    cin >> x >> y;
    v[x].push_back(y);
  }
  // deb(2);
  memset(dp, -1, sizeof(dp));

  int ans = -1;
  for (i = 1; i <= n; i++) {
    ans = max(ans, dhundhle(i));
  }
  cout << ans;
}
Problem p03166 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

const int mxN = 1e5 + 2;
int n, m, visited[mxN], dp[mxN];
vector<int> adj[mxN], radj[mxN], topo;

void dfs(int node) {
  visited[node] = true;
  for (auto &v : adj[node]) {
    if (!visited[v]) {
      dfs(v);
    }
  }
  topo.push_back(node);
}

int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    adj[x].push_back(y);
    radj[y].push_back(x);
  }
  dfs(0);
  reverse(topo.begin(), topo.end());
  // for (auto& num : topo)
  //     cout << num << " ";
  // cout << endl;

  int ret = 0;
  for (int i = 0; i < n; i++) {
    for (auto &v : radj[topo[i]]) {
      dp[topo[i]] = max(dp[topo[i]], dp[v] + 1);
    }
    ret = max(ret, dp[topo[i]]);
  }
  cout << ret << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

const int mxN = 1e5 + 2;
int n, m, visited[mxN], dp[mxN];
vector<int> adj[mxN], radj[mxN], topo;

void dfs(int node) {
  visited[node] = true;
  for (auto &v : adj[node]) {
    if (!visited[v]) {
      dfs(v);
    }
  }
  topo.push_back(node);
}

int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    adj[x].push_back(y);
    radj[y].push_back(x);
  }
  for (int i = 0; i < n; i++) {
    if (!visited[i])
      dfs(i);
  }
  reverse(topo.begin(), topo.end());
  // for (auto& num : topo)
  //     cout << num << " ";
  // cout << endl;

  int ret = 0;
  for (int i = 0; i < n; i++) {
    for (auto &v : radj[topo[i]]) {
      dp[topo[i]] = max(dp[topo[i]], dp[v] + 1);
    }
    ret = max(ret, dp[topo[i]]);
  }
  cout << ret << endl;
}