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

Buggy submission (status: Runtime Error):
/*
https://atcoder.jp/contests/dp/tasks/dp_g
*/
#include <bits/stdc++.h>

using namespace std;

const int N = 10;
const int oo = 1e9 + 7;
vector<vector<int>> adj;
int n, m;
int f[N], ans = 0;
bool Visited[N];

void Init() {
  cin >> n >> m;
  for (int i = 0; i <= n; ++i) {
    adj.push_back(vector<int>());
  }
  int u, v;
  for (int i = 0; i < m; ++i) {
    cin >> u >> v;
    adj[u].push_back(v);
  }
}

void DFS(int pos) {
  Visited[pos] = true;
  for (int i = 0; i < adj[pos].size(); ++i) {
    if (!Visited[adj[pos][i]])
      DFS(adj[pos][i]);
    f[pos] = max(f[pos], f[adj[pos][i]] + 1);
  }
}

int main() {
  Init();
  int ans = 0;
  for (int i = 1; i <= n; ++i) {
    if (Visited[i])
      continue;
    DFS(i);
  }
  for (int i = 1; i <= n; ++i) {
    ans = max(ans, f[i]);
  }
  cout << ans;
  return 0;
}

Fixed submission:
/*
https://atcoder.jp/contests/dp/tasks/dp_g
*/
#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 1;
const int oo = 1e9 + 7;
vector<vector<int>> adj;
int n, m;
int f[N], ans = 0;
bool Visited[N];

void Init() {
  cin >> n >> m;
  for (int i = 0; i <= n; ++i) {
    adj.push_back(vector<int>());
  }
  int u, v;
  for (int i = 0; i < m; ++i) {
    cin >> u >> v;
    adj[u].push_back(v);
  }
}

void DFS(int pos) {
  Visited[pos] = true;
  for (int i = 0; i < adj[pos].size(); ++i) {
    if (!Visited[adj[pos][i]])
      DFS(adj[pos][i]);
    f[pos] = max(f[pos], f[adj[pos][i]] + 1);
  }
}

int main() {
  Init();
  int ans = 0;
  for (int i = 1; i <= n; ++i) {
    if (Visited[i])
      continue;
    DFS(i);
  }
  for (int i = 1; i <= n; ++i) {
    ans = max(ans, f[i]);
  }
  cout << ans;
  return 0;
}
Problem p03166 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define eb emplace_back
#define f first
#define s second
#define ll long long int
#define ull unsigned long long int
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define mod 1000000007
vector<int> g[11111];
int in[11111];
int dp[11111];
ll dfs(ll u) {
  if (dp[u])
    return dp[u];
  ll ans = 0;
  for (int v : g[u]) {
    ans = max(ans, 1 + dfs(v));
  }
  return dp[u] = (ans);
}
int main() {
  fast;
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    g[u].pb(v);
    in[v]++;
  }
  ll ans = -1;
  for (int i = 1; i <= n; i++) {
    if (!dp[i]) {
      ans = max(ans, dfs(i));
    }
  }
  cout << ans;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define eb emplace_back
#define f first
#define s second
#define ll long long int
#define ull unsigned long long int
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define mod 1000000007
vector<int> g[111111];
int in[111111];
int dp[111111];
ll dfs(ll u) {
  if (dp[u])
    return dp[u];
  ll ans = 0;
  for (int v : g[u]) {
    ans = max(ans, 1 + dfs(v));
  }
  return dp[u] = (ans);
}
int main() {
  fast;
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    g[u].pb(v);
    in[v]++;
  }
  ll ans = -1;
  for (int i = 1; i <= n; i++) {
    if (!dp[i]) {
      ans = max(ans, dfs(i));
    }
  }
  cout << ans;
  return 0;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <vector>
using namespace std;

long long N, W;
vector<vector<int>> G;

long long solve(long long *dp, long long v);

int main() {
  cin >> N >> W;
  G.assign(N, vector<int>());

  // dp[v]: v を始点としたときの、G の有効パスの長さの最大値
  long long dp[N];

  for (int i = 0; i < N; i++) {
    dp[i] = -1;
  }

  for (int i = 0; i < W; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    G[x].push_back(y);
    // cout << x << " connected to " << y << endl;
  }

  long long res = 0;
  for (int i = 0; i < N; i++) {
    long long tmp = solve(dp, i);
    // cout << i << " " << tmp << endl;
    if (res < tmp) {
      res = tmp;
    }
  }

  std::cout << res;
  return 0;
}

long long solve(long long *dp, long long v) {
  long long res = 0;
  for (int edge : G[v]) {
    long long tmp = solve(dp, edge) + 1;
    if (res < tmp) {
      res = tmp;
    }
  }
  dp[v] = res;
  return res;
}

Fixed submission:
#include <iostream>
#include <vector>
using namespace std;

long long N, W;
vector<vector<int>> G;

long long solve(long long *dp, long long v);

int main() {
  cin >> N >> W;
  G.assign(N, vector<int>());

  // dp[v]: v を始点としたときの、G の有効パスの長さの最大値
  long long dp[N];

  for (int i = 0; i < N; i++) {
    dp[i] = -1;
  }

  for (int i = 0; i < W; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    G[x].push_back(y);
    // cout << x << " connected to " << y << endl;
  }

  long long res = 0;
  for (int i = 0; i < N; i++) {
    long long tmp = solve(dp, i);
    // cout << i << " " << tmp << endl;
    if (res < tmp) {
      res = tmp;
    }
  }

  std::cout << res;
  return 0;
}

long long solve(long long *dp, long long v) {
  if (dp[v] != -1) {
    return dp[v];
  }

  long long res = 0;
  for (int edge : G[v]) {
    long long tmp = solve(dp, edge) + 1;
    if (res < tmp) {
      res = tmp;
    }
  }
  dp[v] = res;
  return res;
}
Problem p03166 (C++)

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

using namespace std;

const int MN = 100010;
int n, m;
int dp[MN];
bool used[MN];
vector<int> g[MN];

int dfs(int v) {
  if (used[v])
    return dp[v];
  for (int i = 0; i < int(g[v].size()); i++) {
    int u = g[v][i];
    dp[v] = max(dp[v], dfs(u) + 1);
  }
  return dp[v];
}

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

  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].push_back(b);
  }
  int ans = 0;
  for (int i = 0; i < n; i++)
    ans = max(ans, dfs(i));
  cout << ans << endl;
  return 0;
}

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

using namespace std;

const int MN = 100010;
int n, m;
int dp[MN];
bool used[MN];
vector<int> g[MN];

int dfs(int v) {
  if (used[v])
    return dp[v];
  for (int i = 0; i < int(g[v].size()); i++) {
    int u = g[v][i];
    dp[v] = max(dp[v], dfs(u) + 1);
  }
  used[v] = true;
  return dp[v];
}

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

  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].push_back(b);
  }
  int ans = 0;
  for (int i = 0; i < n; i++)
    ans = max(ans, dfs(i));
  cout << ans << endl;
  return 0;
}
Problem p03166 (C++)

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

using namespace std;

int N, M;
vector<vector<int>> adj;
vector<int> lp;
vector<bool> visited;

void dfs(const int vertex) {
  for (const auto child : adj[vertex]) {
    if (not visited[child]) {
      dfs(child);
    }
    lp[vertex] = max(lp[vertex], lp[child] + 1);
  }
}

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

  cin >> N >> M;
  adj.resize(N);
  lp.resize(N, 0);
  visited.resize(N, false);

  for (int edge_number = 0; edge_number < M; ++edge_number) {
    int u, v;
    cin >> u >> v;
    --u, --v;
    adj[u].emplace_back(v);
  }

  for (int vertex = 0; vertex < N; ++vertex) {
    if (not visited[vertex]) {
      visited[vertex] = true;
      dfs(vertex);
    }
  }

  int result{};
  for (const auto p : lp) {
    result = max(result, p);
  }

  cout << result << "\n";

  return 0;
}

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

using namespace std;

int N, M;
vector<vector<int>> adj;
vector<int> lp;
vector<bool> visited;

void dfs(const int vertex) {
  for (const auto child : adj[vertex]) {
    if (not visited[child]) {
      visited[child] = true;
      dfs(child);
    }
    lp[vertex] = max(lp[vertex], lp[child] + 1);
  }
}

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

  cin >> N >> M;
  adj.resize(N);
  lp.resize(N, 0);
  visited.resize(N, false);

  for (int edge_number = 0; edge_number < M; ++edge_number) {
    int u, v;
    cin >> u >> v;
    --u, --v;
    adj[u].emplace_back(v);
  }

  for (int vertex = 0; vertex < N; ++vertex) {
    if (not visited[vertex]) {
      visited[vertex] = true;
      dfs(vertex);
    }
  }

  int result{};
  for (const auto p : lp) {
    result = max(result, p);
  }

  cout << result << "\n";

  return 0;
}
Problem p03166 (C++)

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

#define _u(i, a, b) for (int i = a; i <= b; ++i)
#define _d(i, a, b) for (int i = a; i >= b; --i)
#define endl "\n"
#define ii pair<int, int>
#define fi first
#define se second
#define ll long long
#define FIO()                                                                  \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define FI(task) freopen(task ".inp", "r", stdin)
#define FO(task) freopen(task ".out", "w", stdout)

using namespace std;

const int N = 1e5 + 7;
const int M = 1e9 + 7;

vector<int> edge[N];
int d[N];
int n, m, res;

void dfs(int u) {
  d[u] = 0;
  for (int v : edge[u]) {
    dfs(v);
    d[u] = max(d[u], d[v] + 1);
  }
  res = max(res, d[u]);
}

#define task "test"

int main() {
  FIO();
  // FI(task);
  /// FO(task);

  cin >> n >> m;
  while (m--) {
    int u, v;
    cin >> u >> v;
    edge[u].push_back(v);
  }
  memset(d, -1, sizeof d);
  _u(i, 1, n) if (d[i] == -1) dfs(i);
  cout << res;
  return 0;
}


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

#define _u(i, a, b) for (int i = a; i <= b; ++i)
#define _d(i, a, b) for (int i = a; i >= b; --i)
#define endl "\n"
#define ii pair<int, int>
#define fi first
#define se second
#define ll long long
#define FIO()                                                                  \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define FI(task) freopen(task ".inp", "r", stdin)
#define FO(task) freopen(task ".out", "w", stdout)

using namespace std;

const int N = 1e5 + 7;
const int M = 1e9 + 7;

vector<int> edge[N];
int d[N];
int n, m, res;

void dfs(int u) {
  d[u] = 0;
  for (int v : edge[u]) {
    if (d[v] == -1)
      dfs(v);
    d[u] = max(d[u], d[v] + 1);
  }
  res = max(res, d[u]);
}

#define task "test"

int main() {
  FIO();
  // FI(task);
  /// FO(task);

  cin >> n >> m;
  while (m--) {
    int u, v;
    cin >> u >> v;
    edge[u].push_back(v);
  }
  memset(d, -1, sizeof d);
  _u(i, 1, n) if (d[i] == -1) dfs(i);
  cout << res;
  return 0;
}
Problem p03166 (C++)

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

#define pb push_back

// var
int n, m, t1, t2, dp[100001], ans = 0;
vector<int> e[100001];
bool v[100001];

void dfs(int g) {
  dp[g] = 1;
  int am = 0;
  for (int u : e[g]) {
    if (!v[u])
      dfs(u);
    am = max(am, dp[u]);
  }
  dp[g] += am;
}

int main() {
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    cin >> t1 >> t2;
    e[t1].pb(t2);
  }

  for (int i = 1; i <= n; i++) {
    if (!v[i])
      dfs(i);
    ans = max(ans, dp[i]);
  }
  cout << --ans << endl;
}

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

#define pb push_back

// var
int n, m, t1, t2, dp[100001], ans = 0;
vector<int> e[100001];
bool v[100001];

void dfs(int g) {
  dp[g] = 1;
  int am = 0;
  v[g] = 1;
  for (int u : e[g]) {
    if (!v[u])
      dfs(u);
    am = max(am, dp[u]);
  }
  dp[g] += am;
}

int main() {
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    cin >> t1 >> t2;
    e[t1].pb(t2);
  }

  for (int i = 1; i <= n; i++) {
    if (!v[i])
      dfs(i);
    ans = max(ans, dp[i]);
  }
  cout << --ans << endl;
}
Problem p03166 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define V(i) vector<i>
#define rep(i, n) for (__typeof(n) i = 0; i < n; i++)
#define crep(i, N, n) for (__typeof(n) i = N; i < n; i++)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define P(i, j) pair<i, j>
#define MP make_pair
#define MOD 1000000007
typedef long long ll;
typedef long l;
#define fio                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
int main() {
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  ll n, m;
  cin >> n >> m;
  V(ll) cn(n + 1, 0), og(n + 1, 0), vis(n + 1, 0), piss;
  V(V(ll)) graph(n + 1);
  rep(i, m) {
    ll a, b;
    cin >> a >> b;
    graph[b].push_back(a);
    cn[a]++;
  }
  crep(i, 1, n + 1) {
    if (cn[i] == 0) {
      piss.push_back(i);
    }
  }
  while (piss.size() > 0) {
    ll node = piss[0];
    if (vis[node] != 0) {
      piss.erase(piss.begin());
      continue;
    }
    vis[node] = 1;
    for (ll i = 0; i < graph[node].size(); i++) {
      og[graph[node][i]] = max(og[graph[node][i]], og[node] + 1);
      cn[graph[node][i]]--;
      if (cn[graph[node][i]] == 0 && vis[graph[node][i]] == 0) {
        piss.push_back(graph[node][i]);
      }
    }
    piss.erase(piss.begin());
  }
  cout << *max_element(all(og));

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define V(i) vector<i>
#define rep(i, n) for (__typeof(n) i = 0; i < n; i++)
#define crep(i, N, n) for (__typeof(n) i = N; i < n; i++)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define P(i, j) pair<i, j>
#define MP make_pair
#define MOD 1000000007
typedef long long ll;
typedef long l;
#define fio                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
int main() {
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  ll n, m;
  cin >> n >> m;
  V(ll) cn(n + 1, 0), og(n + 1, 0), vis(n + 1, 0), piss;
  V(V(ll)) graph(n + 1);
  rep(i, m) {
    ll a, b;
    cin >> a >> b;
    graph[b].push_back(a);
    cn[a]++;
  }
  crep(i, 1, n + 1) {
    if (cn[i] == 0) {
      piss.push_back(i);
    }
  }
  while (piss.size() > 0) {
    ll node = piss[0];
    if (vis[node] != 0) {
      piss.erase(piss.begin());
      continue;
    }
    vis[node] = 1;
    for (ll i = 0; i < graph[node].size(); i++) {
      og[graph[node][i]] = max(og[graph[node][i]], og[node] + 1);
      cn[graph[node][i]]--;
      if (cn[graph[node][i]] == 0 && vis[graph[node][i]] == 0) {
        piss.push_back(graph[node][i]);
      }
    }
    piss.erase(piss.begin());
  }
  cout << *max_element(all(og));

  return 0;
}
Problem p03166 (C++)

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

using namespace std;

#define ll long long
#define FASTIO                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);

const int maxN = 1e5 + 5;
const int mod = 1e9 + 7;
vector<int> g[maxN];
int dp[maxN];

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

void solve() {
  int n, m, u, v;
  cin >> n >> m;
  memset(dp, -1, 8 * n);
  for (int i = 0; i < m; i++) {
    cin >> u >> v, u--, v--;
    g[u].push_back(v);
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    ans = max(ans, dfs(i));
  }
  cout << ans << endl;
}

int main() {
  FASTIO
  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}


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

using namespace std;

#define ll long long
#define FASTIO                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);

const int maxN = 1e5 + 5;
const int mod = 1e9 + 7;
vector<int> g[maxN];
int dp[maxN];

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

void solve() {
  int n, m, u, v;
  cin >> n >> m;
  memset(dp, -1, sizeof dp);
  for (int i = 0; i < m; i++) {
    cin >> u >> v, u--, v--;
    g[u].push_back(v);
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    ans = max(ans, dfs(i));
  }
  cout << ans << endl;
}

int main() {
  FASTIO
  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int dp[100009];
#define pb push_back
int vis[100009];
vector<int> adj[100009];
int n;
int call(int u) {
  if (adj[u].size() == 0)
    return 0;
  int mx = 1;
  for (int i = 0; i < adj[u].size(); i++) {
    int chi = adj[u][i];
    int val = call(chi) + 1;
    mx = max(val, mx);
  }
  dp[u] = mx;
  return mx;
}
int main() {
  int m;
  cin >> n >> m;
  memset(dp, -1, sizeof(dp));
  for (int i = 1; i <= m; i++) {
    int u, v;
    cin >> u >> v;
    adj[u].pb(v);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    int val = call(i);
    //    cout<<"max from "<<i<<" = "<<val<<endl;
    ans = max(val, ans);
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int dp[100009];
#define pb push_back
int vis[100009];
vector<int> adj[100009];
int n;
int call(int u) {
  if (adj[u].size() == 0)
    return 0;
  int mx = 1;
  if (dp[u] != -1)
    return dp[u];
  for (int i = 0; i < adj[u].size(); i++) {
    int chi = adj[u][i];
    int val = call(chi) + 1;
    mx = max(val, mx);
  }
  dp[u] = mx;
  return mx;
}
int main() {
  int m;
  cin >> n >> m;
  memset(dp, -1, sizeof(dp));
  for (int i = 1; i <= m; i++) {
    int u, v;
    cin >> u >> v;
    adj[u].pb(v);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    int val = call(i);
    //    cout<<"max from "<<i<<" = "<<val<<endl;
    ans = max(val, ans);
  }
  cout << ans << endl;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int n, m;
vector<vector<int>> G;
vector<int> dp;

int rec(int now) {
  if (dp[now] != -1)
    return dp[now];
  int ans = 0;
  for (auto next : G[now]) {
    ans = max(ans, rec(next) + 1);
  }
  return ans;
}

signed main() {
  scanf("%d %d", &n, &m);
  G.resize(n);
  for (int i = 0; i < m; i++) {
    int x, y;
    scanf("%d %d", &x, &y);
    --x;
    --y;
    G[x].push_back(y);
  }
  dp.resize(n, -1);

  int ans = 0;
  for (int i = 0; i < n; i++) {
    ans = max(ans, rec(i));
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int n, m;
vector<vector<int>> G;
vector<int> dp;

int rec(int now) {
  if (dp[now] != -1)
    return dp[now];
  int ans = 0;
  for (auto next : G[now]) {
    ans = max(ans, rec(next) + 1);
  }
  return dp[now] = ans;
}

signed main() {
  scanf("%d %d", &n, &m);
  G.resize(n);
  for (int i = 0; i < m; i++) {
    int x, y;
    scanf("%d %d", &x, &y);
    --x;
    --y;
    G[x].push_back(y);
  }
  dp.resize(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: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define repd(i, n) for (int i = n - 1; i > -1; i--)
#define repran(i, a, b) for (int i = a; i < b; i++)
#define all(x) (x).begin(), (x).end()
#define v(T) vector<T>
#define vv(T) vector<v(T)>
typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
int n, m;
vvi g(100005);
int memo[100005];
int dp(int s) {
  if (g[s].size() == 0) {
    return memo[s] = 0;
  }
  if (memo[s] > 0)
    return memo[s];
  int ret = 0;
  for (int v : g[s]) {
    chmax(ret, dp(v) + 1);
  }
  return ret;
}

int main() {
  cin >> n >> m;
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].push_back(b);
  }
  int ans = 0;
  rep(i, n) chmax(ans, dp(i));
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define repd(i, n) for (int i = n - 1; i > -1; i--)
#define repran(i, a, b) for (int i = a; i < b; i++)
#define all(x) (x).begin(), (x).end()
#define v(T) vector<T>
#define vv(T) vector<v(T)>
typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
int n, m;
vvi g(100005);
int memo[100005];
int dp(int s) {
  if (g[s].size() == 0) {
    return memo[s] = 0;
  }
  if (memo[s] > 0)
    return memo[s];
  int ret = 0;
  for (int v : g[s]) {
    chmax(ret, dp(v) + 1);
  }
  return memo[s] = ret;
}

int main() {
  cin >> n >> m;
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].push_back(b);
  }
  int ans = 0;
  rep(i, n) chmax(ans, dp(i));
  cout << ans << endl;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bits/stdc++.h>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
#define ll long long int
#define N 100005
using namespace std;
string s1, s2;
template <typename T> void max_self(T &a, T b) {
  if (b >= a) {
    a = b;
  }
}
int vis[N];
int dis[N];
vector<int> g[N];

void dfs(int r, int p) {
  vis[r] = 1;
  int m = -1;
  for (auto v : g[r]) {
    dfs(v, r);
    m = max(m, dis[v]);
  }
  dis[r] = m + 1;
}
int main() {
  int n, m, x, y;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> x >> y;
    g[x].push_back(y);
  }
  for (int i = 1; i <= n; i++) {
    g[n + 1].push_back(i);
  }
  dis[0] = -1;
  dfs(n + 1, 0);
  int ans = 0;
  for (int i = 0; i <= n + 1; i++) {
    // cout<<i<<" "<<dis[i]<<endl;
    ans = max(ans, dis[i]);
  }
  cout << ans - 1;
}


Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
#define ll long long int
#define N 100005
using namespace std;
string s1, s2;
template <typename T> void max_self(T &a, T b) {
  if (b >= a) {
    a = b;
  }
}
int vis[N];
int dis[N];
vector<int> g[N];

void dfs(int r, int p) {
  vis[r] = 1;
  int m = -1;
  for (auto v : g[r]) {
    if (!vis[v])
      dfs(v, r);
    m = max(m, dis[v]);
  }
  dis[r] = m + 1;
}
int main() {
  int n, m, x, y;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> x >> y;
    g[x].push_back(y);
  }
  for (int i = 1; i <= n; i++) {
    g[n + 1].push_back(i);
  }
  dis[0] = -1;
  dfs(n + 1, 0);
  int ans = 0;
  for (int i = 0; i <= n + 1; i++) {
    // cout<<i<<" "<<dis[i]<<endl;
    ans = max(ans, dis[i]);
  }
  cout << ans - 1;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); ++i)

using namespace std;
typedef long long ll;

const int INF = 1 << 30;
const int MOD = 1e9 + 7;

vector<vector<int>> g;
vector<int> dist;

int dfs(int v) {
  int d = 0;
  for (int nv : g[v]) {
    d = max(d, dfs(nv) + 1);
  }
  dist[v] = d;
  return d;
}

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

  g.resize(n);
  dist.assign(n, -1);

  rep(i, m) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    g[x].push_back(y);
  }

  rep(i, n) {
    if (dist[i] != -1)
      continue;
    dfs(i);
  }

  int ans = 0;
  rep(i, n) ans = max(ans, dist[i]);
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); ++i)

using namespace std;
typedef long long ll;

const int INF = 1 << 30;
const int MOD = 1e9 + 7;

vector<vector<int>> g;
vector<int> dist;

int dfs(int v) {
  int d = 0;
  for (int nv : g[v]) {
    if (dist[nv] != -1)
      d = max(d, dist[nv] + 1);
    else
      d = max(d, dfs(nv) + 1);
  }
  dist[v] = d;
  return d;
}

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

  g.resize(n);
  dist.assign(n, -1);

  rep(i, m) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    g[x].push_back(y);
  }

  rep(i, n) {
    if (dist[i] != -1)
      continue;
    dfs(i);
  }

  int ans = 0;
  rep(i, n) ans = max(ans, dist[i]);
  cout << ans << endl;

  return 0;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <array>
#include <bitset>
#include <complex>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define full(c) c.begin(), c.end()
#define vector2d(name, type, h, w, init)                                       \
  vector<vector<type>> name(h, vector<type>(w, init))
const int PRIME = 1000000007;
const int INF = (int)1e9;
const int INT_INF = 2147483647;
const long long int LL_INF = 9223372036854775807;
const double PI = acos(-1);
const double EPS = 0.00001;
typedef unsigned long long int ull;
typedef long long int ll;
using namespace std;

int n, m;
vector<vector<int>> g;
vector<int> path;
int dfs(int current) {
  path[current] = 0;
  for (int next : g[current]) {
    path[current] = max(path[current], dfs(next) + 1);
  }
  return path[current];
}
int main() {
  // cin.tie(0);
  // ios::sync_with_stdio(false);
  cin >> n >> m;
  g.resize(n);
  path.resize(n, -1);
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    g[x].push_back(y);
  }
  int most = 0;
  for (int i = 0; i < n; i++) {
    if (path[i] == -1) {
      path[i] = dfs(i);
    }
    most = max(most, path[i]);
  }
  cout << most << endl;
  return 0;
}


Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <array>
#include <bitset>
#include <complex>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define full(c) c.begin(), c.end()
#define vector2d(name, type, h, w, init)                                       \
  vector<vector<type>> name(h, vector<type>(w, init))
const int PRIME = 1000000007;
const int INF = (int)1e9;
const int INT_INF = 2147483647;
const long long int LL_INF = 9223372036854775807;
const double PI = acos(-1);
const double EPS = 0.00001;
typedef unsigned long long int ull;
typedef long long int ll;
using namespace std;

int n, m;
vector<vector<int>> g;
vector<int> path;
int dfs(int current) {
  if (path[current] != -1)
    return path[current];
  path[current] = 0;
  for (int next : g[current]) {
    path[current] = max(path[current], dfs(next) + 1);
  }
  return path[current];
}
int main() {
  // cin.tie(0);
  // ios::sync_with_stdio(false);
  cin >> n >> m;
  g.resize(n);
  path.resize(n, -1);
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    g[x].push_back(y);
  }
  int most = 0;
  for (int i = 0; i < n; i++) {
    if (path[i] == -1) {
      path[i] = dfs(i);
    }
    most = max(most, path[i]);
  }
  cout << most << endl;
  return 0;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, x, y;
  cin >> n >> m;
  vector<int> g[n + 1];
  int indegree[n + 1];
  int dp[n + 1];

  for (int i = 0; i <= n; i++) {
    indegree[i] = 0;
    dp[i] = 0;
  }
  // cout<<dp[2]<<endl;
  for (int i = 0; i < m; i++) {
    cin >> x >> y;
    g[x].push_back(y);
    indegree[y]++;
  }
  queue<int> q;
  for (int i = 1; i <= n; i++) {
    if (indegree[i] == 0) {
      q.push(i);
    }
  }

  while (!q.empty()) {
    int t = q.front();
    q.pop();
    for (int i = 0; i < g[t].size(); i++) {
      int v = g[t][i];
      dp[v] = max(dp[v], dp[t] + 1);
      q.push(v);
    }
  }
  int h = dp[1];
  for (int i = 2; i <= n; i++) {
    h = max(h, dp[i]);
  }
  cout << h << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, x, y;
  cin >> n >> m;
  vector<int> g[n + 1];
  int indegree[n + 1];
  int dp[n + 1];

  for (int i = 0; i <= n; i++) {
    indegree[i] = 0;
    dp[i] = 0;
  }
  // cout<<dp[2]<<endl;
  for (int i = 0; i < m; i++) {
    cin >> x >> y;
    g[x].push_back(y);
    indegree[y]++;
  }
  queue<int> q;
  for (int i = 1; i <= n; i++) {
    if (indegree[i] == 0) {
      q.push(i);
    }
  }

  while (!q.empty()) {
    int t = q.front();
    q.pop();
    for (int i = 0; i < g[t].size(); i++) {
      int v = g[t][i];
      dp[v] = max(dp[v], dp[t] + 1);
      indegree[v]--;
      if (indegree[v] == 0)
        q.push(v);
    }
  }
  int h = dp[1];
  for (int i = 2; i <= n; i++) {
    h = max(h, dp[i]);
  }
  cout << h << endl;

  return 0;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const long long INF = 1e18 + 10;
const int inf = 1e9 + 10, dx[] = {0, 0, 1, -1, 1, -1, 1, -1},
          dy[] = {1, -1, 0, 0, 1, 1, -1, -1};
ll MOD = 1e9 + 7;
ld eps = 1e-7, pi = acos(-1);

int n, m, x, y, dist[100100];
vector<int> adj[100100];

int dfs(int node) {
  if (dist[node])
    return dist[node];
  int res = 0;

  for (auto nx : adj[node]) {
    res = max(res, dfs(nx));
  }

  return res + 1;
}

int main() {

  scanf("%d %d", &n, &m);

  for (int i = 0; i < m; i++) {
    scanf("%d %d", &x, &y);
    adj[x].push_back(y);
  }

  int ans = 0;

  for (int i = 1; i <= n; i++)
    ans = max(ans, dfs(i));

  printf("%d", ans - 1);

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const long long INF = 1e18 + 10;
const int inf = 1e9 + 10, dx[] = {0, 0, 1, -1, 1, -1, 1, -1},
          dy[] = {1, -1, 0, 0, 1, 1, -1, -1};
ll MOD = 1e9 + 7;
ld eps = 1e-7, pi = acos(-1);

int n, m, x, y, dist[100100];
vector<int> adj[100100];

int dfs(int node) {
  if (dist[node])
    return dist[node];
  int res = 0;

  for (auto nx : adj[node]) {
    res = max(res, dfs(nx));
  }

  return dist[node] = res + 1;
}

int main() {

  scanf("%d %d", &n, &m);

  for (int i = 0; i < m; i++) {
    scanf("%d %d", &x, &y);
    adj[x].push_back(y);
  }

  int ans = 0;

  for (int i = 1; i <= n; i++)
    ans = max(ans, dfs(i));

  printf("%d", ans - 1);

  return 0;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#define N 100005
int n, m;
int vis[N];
int dp[N];
int head[N], nxt[N], to[N], tot;
void add(int x, int y) {
  nxt[++tot] = head[x];
  head[x] = tot;
  to[tot] = y;
}
int DP(int u) {
  if (vis[u])
    return dp[u];
  dp[u] = 0;
  for (int e = head[u]; e; e = nxt[e]) {
    int v = to[e];
    int cur = DP(v) + 1;
    if (cur > dp[u])
      dp[u] = cur;
  }
  return dp[u];
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; ++i) {
    int u, v;
    scanf("%d%d", &u, &v);
    --u, --v;
    add(u, v);
  }
  int ans = 0;
  for (int i = 0; i < n; ++i) {
    int cur = DP(i);
    if (cur > ans)
      ans = cur;
  }
  printf("%d\n", ans);
  return 0;
}

Fixed submission:
#include <cstdio>
#define N 100005
int n, m;
int vis[N];
int dp[N];
int head[N], nxt[N], to[N], tot;
void add(int x, int y) {
  nxt[++tot] = head[x];
  head[x] = tot;
  to[tot] = y;
}
int DP(int u) {
  if (vis[u])
    return dp[u];
  vis[u] = 1;
  dp[u] = 0;
  for (int e = head[u]; e; e = nxt[e]) {
    int v = to[e];
    int cur = DP(v) + 1;
    if (cur > dp[u])
      dp[u] = cur;
  }
  return dp[u];
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; ++i) {
    int u, v;
    scanf("%d%d", &u, &v);
    --u, --v;
    add(u, v);
  }
  int ans = 0;
  for (int i = 0; i < n; ++i) {
    int cur = DP(i);
    if (cur > ans)
      ans = cur;
  }
  printf("%d\n", ans);
  return 0;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
// Om Sree Sai Ram
#include "bits/stdc++.h"
using namespace std;

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

vector<int> dp;
vector<int> vis;

int dfs(int id, vector<vector<int>> adj) {
  // cout<<" called "<<id<<endl;
  if (vis[id]) {
    return dp[id];
  }

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

int main() {

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

  int n, e;
  cin >> n >> e;
  vector<vector<int>> adj(n);

  for (int i = 0; i < e; i++) {
    int a, b;
    cin >> a >> b;
    // cout<<a<<" "<<b<<endl;
    adj[a - 1].push_back(b - 1);
  }
  // cout<<" adj done"<<endl;
  dp.assign(n, 0);
  vis.assign(n, false);

  for (int i = 0; i < n; i++) {
    if (!vis[i]) {
      dfs(i, adj);
    }
  }

  cout << *max_element(dp.begin(), dp.end()) << endl;
}


Fixed submission:
// Om Sree Sai Ram
#include "bits/stdc++.h"
using namespace std;

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

vector<int> dp;
vector<int> vis;

int dfs(int id, vector<vector<int>> &adj) {
  // cout<<" called "<<id<<endl;
  if (vis[id]) {
    return dp[id];
  }

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

int main() {

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

  int n, e;
  cin >> n >> e;
  vector<vector<int>> adj(n);

  for (int i = 0; i < e; i++) {
    int a, b;
    cin >> a >> b;
    // cout<<a<<" "<<b<<endl;
    adj[a - 1].push_back(b - 1);
  }
  // cout<<" adj done"<<endl;
  dp.assign(n, 0);
  vis.assign(n, false);

  for (int i = 0; i < n; i++) {
    if (!vis[i]) {
      dfs(i, adj);
    }
  }

  cout << *max_element(dp.begin(), dp.end()) << endl;
}
Problem p03166 (C++)

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

using Graph = vector<vector<int>>;
int main() {
  int n, m;
  cin >> n >> m;
  Graph g(n);
  vector<int> dist(n);
  vector<int> delta(n);
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    x--, y--;
    delta[y]++;
    g[x].push_back(y);
  }
  queue<int> que;
  for (int i = 0; i < n; i++) {
    if (delta[i] == 0)
      que.push(i);
  }
  while (!que.empty()) {
    int v = que.front();
    for (auto &to : g[v]) {
      dist[to] = max(dist[to], dist[v] + 1);
      if (--delta[to] == 0)
        que.push(to);
    }
  }
  cout << *max_element(dist.begin(), dist.end()) << endl;
  return 0;
}

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

using Graph = vector<vector<int>>;
int main() {
  int n, m;
  cin >> n >> m;
  Graph g(n);
  vector<int> dist(n);
  vector<int> delta(n);
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    x--, y--;
    delta[y]++;
    g[x].push_back(y);
  }
  queue<int> que;
  for (int i = 0; i < n; i++) {
    if (delta[i] == 0)
      que.push(i);
  }
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    for (auto &to : g[v]) {
      dist[to] = max(dist[to], dist[v] + 1);
      if (--delta[to] == 0)
        que.push(to);
    }
  }
  cout << *max_element(dist.begin(), dist.end()) << endl;
  return 0;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#include <iostream>
#include <vector>
#define MAXN 100500
using namespace std;
int n, m;
int dist[MAXN];
vector<int> g[MAXN];
bool was[MAXN];
int ans;

void dfs(int v) {
  // cout << v << ' ';
  was[v] = true;
  for (int w : g[v]) {
    dfs(w);
    if (dist[v] < dist[w] + 1)
      dist[v] = dist[w] + 1;
    if (ans < dist[v])
      ans = dist[v];
  }
}

int x, y;
int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> x >> y;
    g[x].push_back(y);
  }
  for (int i = 1; i <= n; i++) {
    if (!was[i]) {
      dfs(i);
      // cout << endl;
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <cstdio>
#include <iostream>
#include <vector>
#define MAXN 100500
using namespace std;
int n, m;
int dist[MAXN];
vector<int> g[MAXN];
bool was[MAXN];
int ans;

void dfs(int v) {
  // cout << v << ' ';
  was[v] = true;
  for (int w : g[v]) {
    if (!was[w])
      dfs(w);
    if (dist[v] < dist[w] + 1)
      dist[v] = dist[w] + 1;
    if (ans < dist[v])
      ans = dist[v];
  }
}

int x, y;
int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> x >> y;
    g[x].push_back(y);
  }
  for (int i = 1; i <= n; i++) {
    if (!was[i]) {
      dfs(i);
      // cout << endl;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03166 (C++)

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

#define N (int)1e5 + 5
#define f1 first
#define s2 second
#define INF 9999999
#define pb push_back
#define ll long long
#define ull unsigned long long;
#define all(a) a.begin(), a.end()
#define fri(a) freopen(a, "r", stdin);
#define fro(a) freopen(a, "w", stdout);

vector<int> v[N];
int n, m, a, b, ans, dp[N];
bool visit[N];

void dfs(int x) {
  visit[x] = 1;
  for (auto i : v[x]) {
    if (!visit[i])
      ;
    dfs(i);
    dp[x] = max(dp[x], 1 + dp[i]);
  }
}

int main() {
  // fri("in.txt");
  // fro("out.txt");
  cin >> n >> m;
  memset(dp, 0, sizeof(dp));
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    v[a].pb(b);
  }
  for (int i = 1; i <= n; i++) {
    if (!visit[i])
      dfs(i);
    ans = max(ans, dp[i]);
  }
  cout << ans << endl;
  return 0;
}

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

#define N (int)1e5 + 5
#define f1 first
#define s2 second
#define INF 9999999
#define pb push_back
#define ll long long
#define ull unsigned long long;
#define all(a) a.begin(), a.end()
#define fri(a) freopen(a, "r", stdin);
#define fro(a) freopen(a, "w", stdout);

vector<int> v[N];
int n, m, a, b, ans, dp[N];
bool visit[N];

void dfs(int x) {
  visit[x] = 1;
  for (auto i : v[x]) {
    if (!visit[i])
      dfs(i);
    dp[x] = max(dp[x], 1 + dp[i]);
  }
}

int main() {
  // fri("in.txt");
  // fro("out.txt");
  cin >> n >> m;
  memset(dp, 0, sizeof(dp));
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    v[a].pb(b);
  }
  for (int i = 1; i <= n; i++) {
    if (!visit[i])
      dfs(i);
    ans = max(ans, dp[i]);
  }
  cout << ans << endl;
  return 0;
}
Problem p03166 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>

int const MAXN = 10000, MAXE = 10000;

int N, E;

std::vector<int> G[MAXN];

int L[MAXN];

int length(int v) {
  if (L[v] >= 0)
    return L[v];
  int ret = 0;
  for (auto it = G[v].begin(); it != G[v].end(); ++it) {
    ret = std::max(ret, length(*it));
  }
  return L[v] = ret + 1;
}

int main() {
  std::cin >> N >> E;

  std::fill(L, L + N, 0);
  for (int i = 0; i < E; ++i) {
    int x, y;
    std::cin >> x >> y;
    G[x - 1].push_back(y - 1);
    L[x - 1] = -1;
  }
  int ret = 0;
  for (int i = 0; i < N; ++i) {
    ret = std::max(ret, length(i));
  }
  std::cout << ret << std::endl;
  return 0;
}


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

int const MAXN = 100000, MAXE = 100000;

int N, E;

std::vector<int> G[MAXN];

int L[MAXN];

int length(int v) {
  if (L[v] >= 0)
    return L[v];
  int ret = 0;
  for (auto it = G[v].begin(); it != G[v].end(); ++it) {
    ret = std::max(ret, length(*it));
  }
  return L[v] = ret + 1;
}

int main() {
  std::cin >> N >> E;

  std::fill(L, L + N, 0);
  for (int i = 0; i < E; ++i) {
    int x, y;
    std::cin >> x >> y;
    G[x - 1].push_back(y - 1);
    L[x - 1] = -1;
  }
  int ret = 0;
  for (int i = 0; i < N; ++i) {
    ret = std::max(ret, length(i));
  }
  std::cout << ret << std::endl;
  return 0;
}
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 __gnu_pbds;
using namespace std;

// #define ordered_set tree<int, null_type,less<int>,
// rb_tree_tag,tree_order_statistics_node_update>
#define ll long long int
#define ld long double
#define pb push_back
#define mp make_pair
#define pll pair<ll, ll>

vector<ll> v, adj[100005];
ll pr[100005], d[100005];

ll fep(ll x, ll y, ll mod) {
  ll res = 1;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % mod;
    y = y >> 1;
    x = (x * x) % mod;
  }
  return res % mod;
}

ll dfs(ll node) {
  pr[node] = 1;
  ll m = 0, i;
  for (i = 0; i < adj[node].size(); i++)
    m = max(m, dfs(adj[node][i]));
  d[node] = 1 + m;
  return d[node];
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  ll n, i, m, j, k, x, y, a, b;
  string s;

  cin >> n >> m;
  for (i = 0; i < m; i++) {
    cin >> x >> y;
    adj[x].pb(y);
  }
  m = 0;
  for (i = 1; i <= n; i++) {
    if (!pr[i])
      x = dfs(i);
    m = max(m, d[i]);
  }
  cout << m - 1;
  return 0;
}


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

// #define ordered_set tree<int, null_type,less<int>,
// rb_tree_tag,tree_order_statistics_node_update>
#define ll long long int
#define ld long double
#define pb push_back
#define mp make_pair
#define pll pair<ll, ll>

vector<ll> v, adj[100005];
ll pr[100005], d[100005];

ll fep(ll x, ll y, ll mod) {
  ll res = 1;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % mod;
    y = y >> 1;
    x = (x * x) % mod;
  }
  return res % mod;
}

ll dfs(ll node) {
  if (pr[node])
    return d[node];
  pr[node] = 1;
  ll m = 0, i;
  for (i = 0; i < adj[node].size(); i++)
    m = max(m, dfs(adj[node][i]));
  d[node] = 1 + m;
  return d[node];
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  ll n, i, m, j, k, x, y, a, b;
  string s;

  cin >> n >> m;
  for (i = 0; i < m; i++) {
    cin >> x >> y;
    adj[x].pb(y);
  }
  m = 0;
  for (i = 1; i <= n; i++) {
    if (!pr[i])
      x = dfs(i);
    m = max(m, d[i]);
  }
  cout << m - 1;
  return 0;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <stack>
#include <utility>
#include <vector>
#define ll long long int
using namespace std;
vector<int> v[100001];
bool vis[100001] = {false};
ll d[100001] = {0};
void dfs(int s) {
  ll mx = 0;
  for (int i = 0; i < v[s].size(); i++) {
    if (!vis[v[s][i]]) {
      dfs(v[s][i]);
    }
  }
  for (int i = 0; i < v[s].size(); i++) {
    mx = max(d[v[s][i]], mx);
  }
  d[s] = mx + 1;
}
int main() {
  ll n = 0, m = 0, a = 0, b = 0, dis = 0;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    scanf("%d%d", &a, &b);
    v[a].push_back(b);
  }
  memset(vis, false, sizeof(vis));
  memset(d, 0, sizeof(d));
  for (int i = 1; i <= n; i++) {
    if (!vis[i])
      dfs(i);
  }
  for (int i = 1; i <= n; i++) {
    dis = max(d[i], dis);
  }
  cout << dis - 1;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <stack>
#include <utility>
#include <vector>
#define ll long long int
using namespace std;
vector<int> v[100001];
bool vis[100001] = {false};
ll d[100001] = {0};
void dfs(int s) {
  ll mx = 0;
  vis[s] = true;
  for (int i = 0; i < v[s].size(); i++) {
    if (!vis[v[s][i]]) {
      dfs(v[s][i]);
    }
  }
  for (int i = 0; i < v[s].size(); i++) {
    mx = max(d[v[s][i]], mx);
  }
  d[s] = mx + 1;
}
int main() {
  ll n = 0, m = 0, a = 0, b = 0, dis = 0;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    scanf("%d%d", &a, &b);
    v[a].push_back(b);
  }
  memset(vis, false, sizeof(vis));
  memset(d, 0, sizeof(d));
  for (int i = 1; i <= n; i++) {
    if (!vis[i])
      dfs(i);
  }
  for (int i = 1; i <= n; i++) {
    dis = max(d[i], dis);
  }
  cout << dis - 1;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
long long MOD = 1000000007;
int lim = 100010;
#define IOS()                                                                  \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);
typedef long long ll;
#define pb push_back
typedef pair<string, string> pi;

std::vector<bool> visited(lim);
std::vector<int> g[100010];
int dist[100010] = {0};

void dfs(int node) {
  // int ans=0;
  visited[node] = true;
  for (auto child : g[node]) {
    // cout<<node<<" visiting "<<child<<endl;
    dist[child] = std::max(dist[child], dist[node] + 1);
    dfs(child);
  }
  // cout<<"node ans "<<node<<" "<<ans<<endl;
}

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

  int a, b;
  for (int i = 0; i < m; ++i) {
    cin >> a >> b;
    g[a].push_back(b);
  }

  int ans = INT_MIN;
  for (int i = 1; i <= n; i++) {
    if (!visited[i]) {
      dfs(i);
    }
  }

  // cout<<ans<<endl;

  for (int i = 1; i <= n; ++i) {
    ans = std::max(ans, dist[i]);
  }
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
long long MOD = 1000000007;
int lim = 100010;
#define IOS()                                                                  \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);
typedef long long ll;
#define pb push_back
typedef pair<string, string> pi;

std::vector<bool> visited(lim);
std::vector<int> g[100010];
int dist[100010] = {0};

void dfs(int node) {
  // int ans=0;
  visited[node] = true;
  for (auto child : g[node]) {
    if (!visited[child])
      dfs(child);

    dist[node] = std::max(dist[child] + 1, dist[node]);
  }
  // cout<<"node ans "<<node<<" "<<ans<<endl;
}

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

  int a, b;
  for (int i = 0; i < m; ++i) {
    cin >> a >> b;
    g[a].push_back(b);
  }

  int ans = INT_MIN;
  for (int i = 1; i <= n; i++) {
    if (!visited[i]) {
      dfs(i);
    }
  }

  // cout<<ans<<endl;

  for (int i = 1; i <= n; ++i) {
    ans = std::max(ans, dist[i]);
  }
  cout << ans << endl;

  return 0;
}