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

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

#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#define time__()                                                               \
  for (long blockTime = NULL;                                                  \
       (blockTime == NULL ? (blockTime = clock()) != NULL : false);            \
       debug("Time:%.4fs\n", (double)(clock() - blockTime) / CLOCKS_PER_SEC))
#define setp(x) cout << fixed << setprecision(x)
#define forn(i, a, b) for (int i = a; i < b; i++)
#define fore(i, a, b) for (int i = a; i <= b; i++)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define PI 3.14159265358979323846264338327
#define INF 1000000000
#define MOD 998244353
#define MAXN 111111
typedef long long ll;
typedef pair<ll, ll> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef unsigned long long ull;

int n, m, ans = 0;
int memo[MAXN];
vi adj[MAXN];

int dp(int v) {
  if (memo[v] != -1)
    return memo[v];
  int tmp = 0;
  for (auto i : adj[v])
    tmp = max(tmp, dp(i) + 1);
  return tmp;
}

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

  cin >> n >> m;

  fore(i, 1, n) memo[i] = -1;

  forn(i, 0, m) {
    int x, y;
    cin >> x >> y;
    adj[y].pb(x);
  }

  fore(i, 1, n) { ans = max(ans, dp(i)); }

  cout << ans;
  return 0;
}


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

#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#define time__()                                                               \
  for (long blockTime = NULL;                                                  \
       (blockTime == NULL ? (blockTime = clock()) != NULL : false);            \
       debug("Time:%.4fs\n", (double)(clock() - blockTime) / CLOCKS_PER_SEC))
#define setp(x) cout << fixed << setprecision(x)
#define forn(i, a, b) for (int i = a; i < b; i++)
#define fore(i, a, b) for (int i = a; i <= b; i++)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define PI 3.14159265358979323846264338327
#define INF 1000000000
#define MOD 998244353
#define MAXN 111111
typedef long long ll;
typedef pair<ll, ll> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef unsigned long long ull;

int n, m, ans = 0;
int memo[MAXN];
vi adj[MAXN];

int dp(int v) {
  if (memo[v] != -1)
    return memo[v];
  int tmp = 0;
  for (auto i : adj[v])
    tmp = max(tmp, dp(i) + 1);
  memo[v] = tmp;
  return tmp;
}

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

  cin >> n >> m;

  fore(i, 1, n) memo[i] = -1;

  forn(i, 0, m) {
    int x, y;
    cin >> x >> y;
    adj[y].pb(x);
  }

  fore(i, 1, n) { ans = max(ans, dp(i)); }

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

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

#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
const int INF = 1e9;

vector<vector<int>> G;
ll n, m, ans = 0;
ll dp[100100];

ll f(int v) {
  if (dp[v] != -1)
    return dp[v];

  ll res = 0;
  for (auto g : G[v])
    res = max(res, f(g) + 1);
  return res;
}

int main() {
  cin >> n >> m;
  G.resize(n);

  rep(i, m) {
    int x, y;
    cin >> x >> y;
    x--, y--;
    G[x].push_back(y);
  }
  rep(i, n) dp[i] = -1;

  rep(i, n) ans = max(ans, f(i));

  cout << ans << endl;
  return 0;
}

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

#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
const int INF = 1e9;

vector<vector<int>> G;
ll n, m, ans = 0;
ll dp[100100];

ll f(int v) {
  if (dp[v] != -1)
    return dp[v];

  ll res = 0;
  for (auto g : G[v])
    res = max(res, f(g) + 1);
  return dp[v] = res;
}

int main() {
  cin >> n >> m;
  G.resize(n);

  rep(i, m) {
    int x, y;
    cin >> x >> y;
    x--, y--;
    G[x].push_back(y);
  }
  rep(i, n) dp[i] = -1;

  rep(i, n) ans = max(ans, f(i));

  cout << ans << endl;
  return 0;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define Phuong // <3
typedef pair<int, int> ii;

const int inf = 1e9 + 1;
int n, m;

vector<int> d, deg;
vector<vector<int>> g;

void DFS(int x) {
  stack<int> st;
  st.push(x);
  while (!st.empty()) {
    int u = st.top();
    st.pop();
    for (auto x : g[u]) {
      d[x] = max(d[x], d[u] + 1);
      st.push(x);
    }
  }
}

int main() {
#ifndef ONLINE_JUDGE
  // freopen("input.txt","r",stdin);
#endif // ONLINE_JUDGE
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cin >> n >> m;
  g = vector<vector<int>>(n);
  deg = vector<int>(n, 0);
  d = vector<int>(n, 0);
  for (int i = 0; i < m; ++i) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    g[u].push_back(v);
    deg[v]++;
  }
  for (int i = 0; i < n; ++i)
    if (deg[i] == 0) {
      DFS(i);
    }
  int ans = 0;
  for (auto x : d)
    ans = max(ans, x);
  cout << ans;
  return (0);
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define Phuong // <3
typedef pair<int, int> ii;

const int inf = 1e9 + 1;
int n, m;

vector<int> d, deg;
vector<vector<int>> g;

void DFS(int x) {
  stack<int> st;
  st.push(x);
  while (!st.empty()) {
    int u = st.top();
    st.pop();
    for (auto x : g[u])
      if (deg[x] == 1) {
        d[x] = max(d[x], d[u] + 1);
        st.push(x);
      } else {
        deg[x]--;
        d[x] = max(d[x], d[u] + 1);
      }
  }
}

int main() {
#ifndef ONLINE_JUDGE
  // freopen("input.txt","r",stdin);
#endif // ONLINE_JUDGE
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cin >> n >> m;
  g = vector<vector<int>>(n);
  deg = vector<int>(n, 0);
  d = vector<int>(n, 0);
  for (int i = 0; i < m; ++i) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    g[u].push_back(v);
    deg[v]++;
  }
  for (int i = 0; i < n; ++i)
    if (deg[i] == 0) {
      DFS(i);
    }
  int ans = 0;
  for (auto x : d)
    ans = max(ans, x);
  cout << ans;
  return (0);
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
#define loop(n) for (ll i = 0; i < n; i++)
#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
using namespace std;
typedef long long int ll;

unordered_map<ll, vector<ll>> graph;
ll dp[100001];

ll dfs(ll node, ll level) {

  if (dp[node] != -1)
    return level + dp[node];
  ll ans = level;
  auto itr = graph.find(node);
  if (itr == graph.end())
    return ans;
  else {
    vector<ll> nbs = graph[node];
    for (ll i = 0; i < (ll)nbs.size(); i++) {
      ans = max(ans, dfs(nbs[i], level + 1));
    }
  }

  if (level == 0)
    dp[node] = ans;
  return ans;
}

int main() {
  fastio;
  ll n, m;
  cin >> n >> m;
  for (ll i = 1; i <= n; i++)
    dp[i] = -1;

  for (ll i = 0; i < m; i++) {
    ll x, y;
    cin >> x >> y;
    auto itr = graph.find(x);
    if (itr == graph.end()) {
      vector<ll> hold;
      hold.push_back(y);
      graph.insert({x, hold});
    } else {
      graph[x].push_back(y);
    }
  }

  ll ans = INT_MIN;
  for (ll i = 1; i <= n; i++) {

    ans = max(ans, dfs(i, 0));
  }
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
#define loop(n) for (ll i = 0; i < n; i++)
#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
using namespace std;
typedef long long int ll;

unordered_map<ll, vector<ll>> graph;
ll dp[100001];

ll dfs(ll node, ll level) {

  if (dp[node] != -1)
    return level + dp[node];
  ll ans = level;
  auto itr = graph.find(node);
  if (itr == graph.end())
    return ans;
  else {
    vector<ll> nbs = graph[node];
    for (ll i = 0; i < (ll)nbs.size(); i++) {
      ans = max(ans, dfs(nbs[i], level + 1));
    }
  }

  dp[node] = ans - level;
  return ans;
}

int main() {
  fastio;
  ll n, m;
  cin >> n >> m;
  for (ll i = 1; i <= n; i++)
    dp[i] = -1;

  for (ll i = 0; i < m; i++) {
    ll x, y;
    cin >> x >> y;
    auto itr = graph.find(x);
    if (itr == graph.end()) {
      vector<ll> hold;
      hold.push_back(y);
      graph.insert({x, hold});
    } else {
      graph[x].push_back(y);
    }
  }

  ll ans = INT_MIN;
  for (ll i = 1; i <= n; i++) {

    ans = max(ans, dfs(i, 0));
  }
  cout << ans << endl;

  return 0;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
#define all(v) v.begin(), v.end()
#define mp make_pair
#define pb push_back
#define f first
#define s second

vi c[100005];
bool visit[100005];
int ind[100005];
int dp[100005];

void dfs(int x) {
  visit[x] = true;
  for (int y : c[x]) {
    dp[y] = max(dp[y], dp[x] + 1);
    // ind[y]--;
    // if(ind[y] == 0){
    dfs(y);
    //}
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  // freopen("input.in", "r", stdin);
  // freopen("output.out", "w", stdout);
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int a, b;
    cin >> a >> b;
    c[a].pb(b);
    ind[b]++;
  }
  for (int i = 1; i <= n; i++) {
    if (!visit[i] && !ind[i]) {
      dfs(i);
    }
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    ans = max(ans, dp[i]);
  }
  cout << ans << endl;
  return 0;
}
/*
Things to look out for:
        - Integer overflows
        - Array bounds
        - Special cases
Be careful!
*/


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
#define all(v) v.begin(), v.end()
#define mp make_pair
#define pb push_back
#define f first
#define s second

vi c[100005];
bool visit[100005];
int ind[100005];
int dp[100005];

void dfs(int x) {
  visit[x] = true;
  for (int y : c[x]) {
    dp[y] = max(dp[y], dp[x] + 1);
    ind[y]--;
    if (ind[y] == 0) {
      dfs(y);
    }
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  // freopen("input.in", "r", stdin);
  // freopen("output.out", "w", stdout);
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int a, b;
    cin >> a >> b;
    c[a].pb(b);
    ind[b]++;
  }
  for (int i = 1; i <= n; i++) {
    if (!visit[i] && !ind[i]) {
      dfs(i);
    }
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    ans = max(ans, dp[i]);
  }
  cout << ans << endl;
  return 0;
}
/*
Things to look out for:
        - Integer overflows
        - Array bounds
        - Special cases
Be careful!
*/
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define MAX 2000000
#define MOD 1000000007
#define INF 10000000000000000
using Graph = vector<vector<int>>;

int dfs(Graph G, int v, vector<int> &length, vector<bool> &seen) {
  if (seen.at(v)) {
    return length.at(v);
  }
  seen.at(v) = true;
  int ans = 0;
  for (auto nv : G.at(v)) {
    ans = max(ans, dfs(G, nv, length, seen) + 1);
  }
  return length.at(v) = ans;
}

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

  vector<int> length(N, 0);
  vector<bool> seen(N, false);
  for (int i = 0; i < N; i++) {
    dfs(G, i, length, seen);
  }

  int ans = 0;
  for (int i = 0; i < N; i++) {
    ans = max(ans, length.at(i));
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define MAX 2000000
#define MOD 1000000007
#define INF 10000000000000000
using Graph = vector<vector<int>>;

int dfs(Graph &G, int v, vector<int> &length, vector<bool> &seen) {
  if (seen.at(v)) {
    return length.at(v);
  }
  seen.at(v) = true;
  int ans = 0;
  for (auto nv : G.at(v)) {
    ans = max(ans, dfs(G, nv, length, seen) + 1);
  }
  return length.at(v) = ans;
}

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

  vector<int> length(N, 0);
  vector<bool> seen(N, false);
  for (int i = 0; i < N; i++) {
    dfs(G, i, length, seen);
  }

  int ans = 0;
  for (int i = 0; i < N; i++) {
    ans = max(ans, length.at(i));
  }
  cout << ans << endl;
}
Problem p03166 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bits/stdc++.h>
#include <bitset>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

typedef long long ll;
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define exout(x) printf("%.10f\n", x)
const double pi = acos(-1.0);
const ll MOD = 1000000007;
const ll INF = 1e10;
const ll MAX_N = 1010;

// 最大公約数
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }

ll lcm(ll x, ll y) {
  if (x == 0 || y == 0)
    return 0;
  return (x / gcd(x, y) * y);
}

ll dp[101010];
ll n, m;
vector<vector<ll>> to;

ll rec(ll v) {
  if (dp[v] != -1)
    return dp[v];
  else {
    ll res = 0;
    for (auto nv : to[v]) {
      res = max(res, rec(nv) + 1);
    }
    return dp[v] = res;
  }
}

// long longしか使わない
// 素数は1より大きい
int main() {
  cin >> n >> m;
  rep(i, m) {
    ll x, y;
    cin >> x >> y;
    x--;
    y--;
    to[x].push_back(y);
  }
  rep(i, n) { dp[i] = -1; }
  ll ans = 0;
  rep(i, n) { ans = max(ans, rec(i)); }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <bitset>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

typedef long long ll;
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define exout(x) printf("%.10f\n", x)
const double pi = acos(-1.0);
const ll MOD = 1000000007;
const ll INF = 1e10;
const ll MAX_N = 1010;

// 最大公約数
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }

ll lcm(ll x, ll y) {
  if (x == 0 || y == 0)
    return 0;
  return (x / gcd(x, y) * y);
}

ll dp[101010];
ll n, m;
vector<vector<ll>> to;

ll rec(ll v) {
  if (dp[v] != -1)
    return dp[v];
  else {
    ll res = 0;
    for (auto nv : to[v]) {
      res = max(res, rec(nv) + 1);
    }
    return dp[v] = res;
  }
}

// long longしか使わない
// 素数は1より大きい
int main() {
  cin >> n >> m;
  to.resize(n);
  rep(i, m) {
    ll x, y;
    cin >> x >> y;
    x--;
    y--;
    to[x].push_back(y);
  }
  rep(i, n) { dp[i] = -1; }
  ll ans = 0;
  rep(i, n) { ans = max(ans, rec(i)); }
  cout << ans << endl;
  return 0;
}
Problem p03166 (C++)

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

vector<int> depth;
vector<pair<int, int>> G;

int depthG(int n) {
  if (depth[n] != -1)
    return depth[n];

  auto it = lower_bound(G.begin(), G.end(), make_pair(n, 0));
  int ans = 0;
  while (it != G.end() and *it < make_pair(n + 1, 0)) {
    ans = max(ans, depthG(it->second) + 1);
    it++;
  }
  return ans;
}

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> dp(N, 0);

  for (int i = 0; i < M; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    G.emplace_back(x, y);
  }
  sort(G.begin(), G.end());
  vector<int> _depth(N, -1);
  depth = _depth;

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

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

vector<int> depth;
vector<pair<int, int>> G;

int depthG(int n) {
  if (depth[n] != -1)
    return depth[n];

  auto it = lower_bound(G.begin(), G.end(), make_pair(n, 0));
  int ans = 0;
  while (it != G.end() and *it < make_pair(n + 1, 0)) {
    ans = max(ans, depthG(it->second) + 1);
    it++;
  }
  return depth[n] = ans;
}

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> dp(N, 0);

  for (int i = 0; i < M; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    G.emplace_back(x, y);
  }
  sort(G.begin(), G.end());
  vector<int> _depth(N, -1);
  depth = _depth;

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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define PI acos(-1)

using namespace std;
using ll = long long;
using P = pair<int, int>;
using LP = pair<ll, ll>;

int dfs(int v, vector<vector<int>> to, vector<int> &dist) {
  if (dist[v] >= 0)
    return dist[v];

  int res = 0;
  for (auto e : to[v])
    res = max(res, dfs(e, to, dist) + 1);
  dist[v] = res;
  return res;
}

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

  int n, m;
  cin >> n >> m;
  vector<vector<int>> to(n);
  rep(i, m) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    to[x].push_back(y);
  }

  vector<int> dist(n, -1);
  int ans = 0;
  rep(i, n) { ans = max(ans, dfs(i, to, dist)); }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define PI acos(-1)

using namespace std;
using ll = long long;
using P = pair<int, int>;
using LP = pair<ll, ll>;

int dfs(int v, vector<vector<int>> &to, vector<int> &dist) {
  if (dist[v] >= 0)
    return dist[v];

  int res = 0;
  for (auto e : to[v])
    res = max(res, dfs(e, to, dist) + 1);
  dist[v] = res;
  return res;
}

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

  int n, m;
  cin >> n >> m;
  vector<vector<int>> to(n);
  rep(i, m) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    to[x].push_back(y);
  }

  vector<int> dist(n, -1);
  int ans = 0;
  rep(i, n) { ans = max(ans, dfs(i, to, dist)); }
  cout << ans << endl;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define assert_range(x, a, b) assert((a) <= (x) and (x) <= (b))
using ll = long long;
const int INF = 1e9;

const int N = 1e5;
vector<int> g[N];
bool visited[N];

void dfs(int v, vector<int> &order) {
  for (auto u : g[v]) {
    if (visited[u])
      continue;
    dfs(u, order);
  }
  order.push_back(v);
}
int main() {
  ios_base::sync_with_stdio(0);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    int v, u;
    cin >> v >> u;
    --v, --u;
    g[v].push_back(u);
  }
  vector<int> order;
  for (int i = 0; i < n; ++i) {
    if (visited[i])
      continue;
    dfs(i, order);
  }
  vector<int> dp(n);
  for (auto v : order) {
    for (auto u : g[v]) {
      dp[v] = max(dp[v], dp[u] + 1);
    }
  }
  int res = 0;
  for (int i = 0; i < n; ++i) {
    res = max(res, dp[i]);
  }
  cout << res << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define assert_range(x, a, b) assert((a) <= (x) and (x) <= (b))
using ll = long long;
const int INF = 1e9;

const int N = 1e5;
vector<int> g[N];
bool visited[N];

void dfs(int v, vector<int> &order) {
  visited[v] = true;
  for (auto u : g[v]) {
    if (visited[u])
      continue;
    dfs(u, order);
  }
  order.push_back(v);
}
int main() {
  ios_base::sync_with_stdio(0);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    int v, u;
    cin >> v >> u;
    --v, --u;
    g[v].push_back(u);
  }
  vector<int> order;
  for (int i = 0; i < n; ++i) {
    if (visited[i])
      continue;
    dfs(i, order);
  }
  vector<int> dp(n);
  for (auto v : order) {
    for (auto u : g[v]) {
      dp[v] = max(dp[v], dp[u] + 1);
    }
  }
  int res = 0;
  for (int i = 0; i < n; ++i) {
    res = max(res, dp[i]);
  }
  cout << res << endl;

  return 0;
}
Problem p03166 (C++)

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

using namespace std;

int n, m;

vector<int> g[323456];
int cnt[323456];
int ans;
int d[323456];
vector<int> ord;
bool used[323456];

void dfs(int v) {
  used[v] = true;
  for (size_t i = 0; i < g[v].size(); ++i) {
    int to = g[v][i];
    if (!used[to])
      dfs(to);
  }
  ord.push_back(v);
}

void bfs() {
  queue<int> q;

  for (int i = 0; i < ord.size(); i++)
    q.push(ord[i]);
  while (q.size()) {
    int x = q.front();
    q.pop();
    for (int i = 0; i < g[x].size(); i++) {
      int y = g[x][i];
      if (d[x] + 1 > d[y])
        d[y] = d[x] + 1, q.push(y), ans = max(ans, d[y]);
    }
  }
  cout << ans;
  exit(0);
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int x, y;
    cin >> x >> y;
    g[x].push_back(y);
    cnt[y]++;
    // g[y].push_back(x);
  }
  for (int i = 1; i <= n; i++)
    if (used[i] == 0)
      dfs(i);
  sort(ord.begin(), ord.end());
  bfs();
  cout << ans;
}


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

using namespace std;

int n, m;

vector<int> g[323456];
int cnt[323456];
int ans;
int d[323456];
vector<int> ord;
bool used[323456];

void dfs(int v) {
  used[v] = true;
  for (size_t i = 0; i < g[v].size(); ++i) {
    int to = g[v][i];
    if (!used[to])
      dfs(to);
  }
  ord.push_back(v);
}

void bfs() {
  queue<int> q;

  for (int i = 0; i < ord.size(); i++)
    q.push(ord[i]);
  while (q.size()) {
    int x = q.front();
    q.pop();
    for (int i = 0; i < g[x].size(); i++) {
      int y = g[x][i];
      if (d[x] + 1 > d[y])
        d[y] = d[x] + 1, q.push(y), ans = max(ans, d[y]);
    }
  }
  cout << ans;
  exit(0);
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int x, y;
    cin >> x >> y;
    g[x].push_back(y);
    cnt[y]++;
    // g[y].push_back(x);
  }
  for (int i = 1; i <= n; i++)
    if (used[i] == 0)
      dfs(i);
  reverse(ord.begin(), ord.end());
  bfs();
  cout << ans;
}
Problem p03166 (C++)

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

using namespace std;

int vertices, edges;
vector<int> grp[10005];
vector<int> dp(10005, -1);
vector<int> visited(10005, -1);

int findMaxAns(int src) {
  if (dp[src] != -1) {
    return dp[src];
  }

  visited[src] = 1;
  int maxPath = 0;

  for (auto m : grp[src]) {
    maxPath = max(maxPath, 1 + findMaxAns(m));
  }
  dp[src] = maxPath;
  return dp[src];
}

void solve() {
  cin >> vertices >> edges;
  for (int i = 0; i < edges; i++) {
    int a, b;
    cin >> a >> b;
    grp[a].push_back(b);
  }

  int ans = 0;
  for (int i = 1; i <= vertices; i++) {
    ans = max(ans, findMaxAns(i));
  }
  cout << ans;
}

int main() {

  solve();

  return 0;
}


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

using namespace std;

int vertices, edges;
vector<int> grp[100005];
vector<int> dp(100005, -1);
vector<int> visited(100005, -1);

int findMaxAns(int src) {
  if (dp[src] != -1) {
    return dp[src];
  }

  visited[src] = 1;
  int maxPath = 0;

  for (auto m : grp[src]) {
    maxPath = max(maxPath, 1 + findMaxAns(m));
  }
  dp[src] = maxPath;
  return dp[src];
}

void solve() {
  cin >> vertices >> edges;
  for (int i = 0; i < edges; i++) {
    int a, b;
    cin >> a >> b;
    grp[a].push_back(b);
  }

  int ans = 0;
  for (int i = 1; i <= vertices; i++) {
    ans = max(ans, findMaxAns(i));
  }
  cout << ans;
}

int main() {

  solve();

  return 0;
}
Problem p03166 (C++)

Buggy submission (status: Runtime Error):
/*	Don’t Quit

        When things go wrong as they sometimes will;
                When the road you’re trudging seems all uphill;
                        When the funds are low, and the debts are high;
                                And you want to smile, but you have to sigh;
                                        When care is pressing you down a bit
                                                Rest if you must, but don’t you
   quit.
*/
#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0)
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define all(a) a.begin(), a.end()
// #define int long long
const int MOD = 1e9 + 7;
const int N = 1e5 + 10;
int gcd(int a, int b) {
  if (a == 0)
    return b;
  else
    return gcd(b % a, a);
}
vector<int> vp[N];
int dp[N];
int go(int n) {
  if (dp[n] != -1)
    return dp[n];
  int ans = 0;
  for (int i = 0; i < vp[n].size(); i++) {
    ans = max(ans, go(vp[n][i]) + 1);
  }
  dp[n] = ans;
  return ans;
}
int main() {
  int n, m;
  cin >> n >> m;
  int x, y;
  for (int i = 0; i < m; i++) {
    cin >> x >> y;
    vp[x].pb(y);
    vp[y].pb(x);
  }

  int ans = 0;
  memset(dp, -1, sizeof(dp));
  for (int i = 1; i <= n; i++) {

    ans = max(ans, go(i));
  }
  cout << ans << endl;
}

Fixed submission:
/*	Don’t Quit

        When things go wrong as they sometimes will;
                When the road you’re trudging seems all uphill;
                        When the funds are low, and the debts are high;
                                And you want to smile, but you have to sigh;
                                        When care is pressing you down a bit
                                                Rest if you must, but don’t you
   quit.
*/
#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0)
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define all(a) a.begin(), a.end()
// #define int long long
const int MOD = 1e9 + 7;
const int N = 1e5 + 10;
int gcd(int a, int b) {
  if (a == 0)
    return b;
  else
    return gcd(b % a, a);
}
vector<int> vp[N];
int dp[N];
int go(int n) {
  if (dp[n] != -1)
    return dp[n];
  int ans = 0;
  for (int i = 0; i < vp[n].size(); i++) {
    ans = max(ans, go(vp[n][i]) + 1);
  }
  dp[n] = ans;
  return ans;
}
int main() {
  int n, m;
  cin >> n >> m;
  int x, y;
  for (int i = 0; i < m; i++) {
    cin >> x >> y;
    vp[x].pb(y);
  }

  int ans = 0;
  memset(dp, -1, sizeof(dp));
  for (int i = 1; i <= n; i++) {

    ans = max(ans, go(i));
  }
  cout << ans << endl;
}
Problem p03166 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define N_MAX 100000
#define MOD (1000000007)

using namespace std;

typedef long long int Int;

int dp[N_MAX + 5];
vector<int> G[N_MAX];
int N, M;
int dfs(int node) {
  if (dp[node] != -1)
    return dp[node];

  int ans = 0;
  for (auto x : G[node]) {
    ans = max(ans, dfs(x) + 1);
  }

  return dp[node] = ans;
}

int main(void) {
  cin >> N >> M;

  for (int i = 0; i < M; i++) {
    int from, to;
    cin >> from >> to;
    G[from].push_back(to);
  }
  memset(dp, -1, sizeof(dp));

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

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define N_MAX 100000
#define MOD (1000000007)

using namespace std;

typedef long long int Int;

int dp[N_MAX + 5];
vector<int> G[N_MAX + 10];
int N, M;
int dfs(int node) {
  if (dp[node] != -1)
    return dp[node];

  int ans = 0;
  for (auto x : G[node]) {
    ans = max(ans, dfs(x) + 1);
  }

  return dp[node] = ans;
}

int main(void) {
  cin >> N >> M;

  for (int i = 0; i < M; i++) {
    int from, to;
    cin >> from >> to;
    G[from].push_back(to);
  }
  memset(dp, -1, sizeof(dp));

  int ans = 0;
  for (int i = 1; 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<int> adj[100005];
vector<int> toposort;
int vis[100005];

void topo(int x) {
  if (vis[x])
    return;
  for (int i = 0; i < (int)adj[x].size(); i++) {
    if (vis[adj[x][i]])
      continue;
    topo(adj[x][i]);
  }
  toposort.push_back(x);
}

int main() {
  scanf("%d%d", &n, &m);
  int x, y;
  int indeg[n];
  memset(indeg, 0, sizeof(indeg));
  for (int i = 0; i < m; i++) {
    scanf("%d%d", &x, &y);
    x--;
    y--;
    adj[x].push_back(y);
    indeg[y]++;
  }
  int dp[n];
  memset(dp, 0, sizeof(dp));
  for (int i = 0; i < n; i++) {
    if (indeg[i] == 0) {
      topo(i);
    }
  }
  reverse(toposort.begin(), toposort.end());
  for (int i = 0; i < (int)toposort.size(); i++) {
    for (int j = 0; j < (int)adj[toposort[i]].size(); j++) {
      dp[adj[toposort[i]][j]] =
          max(dp[adj[toposort[i]][j]], dp[toposort[i]] + 1);
    }
  }
  int ans = 0;
  for (int i = 0; i < n; i++)
    ans = max(ans, dp[i]);
  printf("%d", ans);
}

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

int n, m;
vector<int> adj[100005];
vector<int> toposort;
int vis[100005];

void topo(int x) {
  if (vis[x])
    return;
  vis[x] = 1;
  for (int i = 0; i < (int)adj[x].size(); i++) {
    if (vis[adj[x][i]])
      continue;
    topo(adj[x][i]);
  }
  toposort.push_back(x);
}

int main() {
  scanf("%d%d", &n, &m);
  int x, y;
  int indeg[n];
  memset(indeg, 0, sizeof(indeg));
  for (int i = 0; i < m; i++) {
    scanf("%d%d", &x, &y);
    x--;
    y--;
    adj[x].push_back(y);
    indeg[y]++;
  }
  int dp[n];
  memset(dp, 0, sizeof(dp));
  for (int i = 0; i < n; i++) {
    if (indeg[i] == 0) {
      topo(i);
    }
  }
  reverse(toposort.begin(), toposort.end());
  for (int i = 0; i < (int)toposort.size(); i++) {
    for (int j = 0; j < (int)adj[toposort[i]].size(); j++) {
      dp[adj[toposort[i]][j]] =
          max(dp[adj[toposort[i]][j]], dp[toposort[i]] + 1);
    }
  }
  int ans = 0;
  for (int i = 0; i < n; i++)
    ans = max(ans, dp[i]);
  printf("%d", ans);
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
/* Murad Eynizade */

#include <bits/stdc++.h>
#define intt long long
#define FAST_READ                                                              \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);
#define F first
#define S second

using namespace std;

const int sz = 100005;

int n, m, x, y, used[sz], ans[sz], mx;

vector<int> v[sz];

void dfs(int s) {
  int res = 0;
  for (int to : v[s]) {
    if (!used[to])
      dfs(to);
    res = max(res, ans[to] + 1);
  }
  ans[s] = res;
  mx = max(mx, ans[s]);
}

int main() {
  FAST_READ;
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    cin >> x >> y;
    v[x].push_back(y);
  }
  for (int i = 1; i <= n; i++)
    if (!used[i])
      dfs(i);
  cout << mx << endl;
  return 0;
}


Fixed submission:
/* Murad Eynizade */

#include <bits/stdc++.h>
#define intt long long
#define FAST_READ                                                              \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);
#define F first
#define S second

using namespace std;

const int sz = 100005;

int n, m, x, y, used[sz], ans[sz], mx;

vector<int> v[sz];

void dfs(int s) {
  used[s] = 1;
  int res = 0;
  for (int to : v[s]) {
    if (!used[to])
      dfs(to);
    res = max(res, ans[to] + 1);
  }
  ans[s] = res;
  mx = max(mx, ans[s]);
}

int main() {
  FAST_READ;
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    cin >> x >> y;
    v[x].push_back(y);
  }
  for (int i = 1; i <= n; i++)
    if (!used[i])
      dfs(i);
  cout << mx << endl;
  return 0;
}
Problem p03166 (C++)

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

using namespace std;

vector<vector<int>> g;
vector<int> indegree;
vector<int> visited;
vector<int> depth;

void dfs(int v) {
  visited[v] = 1;
  for (int u : g[v]) {
    depth[u] = max(depth[u], depth[v] + 1);
    dfs(u);
  }
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int n, m;
  cin >> n >> m;
  g.resize(n, vector<int>());
  visited.resize(n);
  indegree.resize(n);
  depth.resize(n);

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

  for (int i = 0; i < n; i++) {
    if (!visited[i] && indegree[i] == 0) {
      dfs(i);
    }
  }

  cout << *max_element(depth.begin(), depth.end());

#ifdef LOCAL
  cerr << "\n";
#endif
  return 0;
}


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

using namespace std;

vector<vector<int>> g;
vector<int> indegree;
vector<int> visited;
vector<int> depth;

void dfs(int v) {
  visited[v] = 1;
  for (int u : g[v]) {
    depth[u] = max(depth[u], depth[v] + 1);
    --indegree[u];
    if (indegree[u] == 0)
      dfs(u);
  }
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int n, m;
  cin >> n >> m;
  g.resize(n, vector<int>());
  visited.resize(n);
  indegree.resize(n);
  depth.resize(n);

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

  for (int i = 0; i < n; i++) {
    if (!visited[i] && indegree[i] == 0) {
      dfs(i);
    }
  }

  cout << *max_element(depth.begin(), depth.end());

#ifdef LOCAL
  cerr << "\n";
#endif
  return 0;
}
Problem p03166 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <stack>
#include <vector>
using namespace std;

typedef long long ll;

const int N = 1e5 + 5;

vector<vector<int>> g(N);
vector<bool> vis(N);
vector<int> dp(N);
stack<int> topo;

void dfs(int u) {
  vis[u] = 1;
  for (auto v : g[u])
    if (!vis[v])
      dfs(v);
  topo.push(u);
}

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

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

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

  for (int i = 1; i <= n; i++)
    if (!vis[i])
      dfs(i);

  for (; !topo.empty(); topo.pop()) {
    int u = topo.top();
    for (auto v : g[u])
      dp[v] = max(dp[v], dp[u] + 1);
  }

  cout << *max_element(dp.begin() + 1, dp.end() + n + 1) << '\n';
  return 0;
}


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

typedef long long ll;

const int N = 1e5 + 5;

vector<vector<int>> g(N);
vector<bool> vis(N);
vector<int> dp(N);
stack<int> topo;

void dfs(int u) {
  vis[u] = 1;
  for (auto v : g[u])
    if (!vis[v])
      dfs(v);
  topo.push(u);
}

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

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

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

  for (int i = 1; i <= n; i++)
    if (!vis[i])
      dfs(i);

  for (; !topo.empty(); topo.pop()) {
    int u = topo.top();
    for (auto v : g[u])
      dp[v] = max(dp[v], dp[u] + 1);
  }

  cout << *max_element(dp.begin() + 1, dp.begin() + n + 1) << '\n';
  return 0;
}
Problem p03166 (C++)

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

void dfs(int u, vector<int> adj[], vector<int> &dp, vector<bool> vis) {
  int i, j;
  vis[u] = true;
  for (i = 0; i < adj[u].size(); i++) {
    if (vis[adj[u][i]] == false)
      dfs(adj[u][i], adj, dp, vis);
    dp[u] = max(dp[u], 1 + dp[adj[u][i]]);
  }
}

int main() {
  int n, m, i, j, u, v;
  cin >> n >> m;
  vector<int> adj[n + 1];
  for (i = 0; i < m; i++) {
    cin >> u >> v;
    adj[u].push_back(v);
  }
  vector<int> dp(n + 1, 0);
  vector<bool> vis(n + 1, false);
  int ans = -1;
  for (i = 1; i <= n; i++) {
    if (vis[i] == false)
      dfs(i, adj, dp, vis);
    ans = max(ans, dp[i]);
  }
  cout << ans << endl;
  return 0;
}


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

void dfs(int u, vector<int> adj[], vector<int> &dp, vector<bool> &vis) {
  int i, j;
  vis[u] = true;
  for (i = 0; i < adj[u].size(); i++) {
    if (vis[adj[u][i]] == false)
      dfs(adj[u][i], adj, dp, vis);
    dp[u] = max(dp[u], 1 + dp[adj[u][i]]);
  }
}

int main() {
  int n, m, i, j, u, v;
  cin >> n >> m;
  vector<int> adj[n + 1];
  for (i = 0; i < m; i++) {
    cin >> u >> v;
    adj[u].push_back(v);
  }
  vector<int> dp(n + 1, 0);
  vector<bool> vis(n + 1, false);
  int ans = -1;
  for (i = 1; i <= n; i++) {
    if (vis[i] == false)
      dfs(i, adj, dp, vis);
    ans = max(ans, dp[i]);
  }
  cout << ans << endl;
  return 0;
}
Problem p03166 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;
#define rep(i, s, e) for (int(i) = (s); (i) <= (e); ++(i))
#define all(x) x.begin(), x.end()
int n, m;
vector<vector<int>> g;
int dp[100100];

int dfs(int now) {
  if (dp[now] != -1)
    return dp[now];

  int res = 0;
  for (int x : g[now]) {
    res = max(res, dfs(x) + 1);
  }
  return dp[now] = res;
}

int main() {
  cin >> n >> m;
  g.resize(n);
  rep(i, 1, m) {
    int x, y;
    cin >> x >> y;
    --x, --y;
    g[x].push_back(y);
  }
  rep(i, 0, 100100) dp[i] = -1;
  int res = 0;
  rep(i, 0, n - 1) res = max(res, dfs(i));
  cout << res << endl;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;
#define rep(i, s, e) for (int(i) = (s); (i) <= (e); ++(i))
#define all(x) x.begin(), x.end()
int n, m;
vector<vector<int>> g;
int dp[100100];

int dfs(int now) {
  if (dp[now] != -1)
    return dp[now];

  int res = 0;
  for (int x : g[now]) {
    res = max(res, dfs(x) + 1);
  }
  return dp[now] = res;
}

int main() {
  cin >> n >> m;
  g.resize(n);
  rep(i, 1, m) {
    int x, y;
    cin >> x >> y;
    --x, --y;
    g[x].push_back(y);
  }
  rep(i, 0, 100010) dp[i] = -1;
  int res = 0;
  rep(i, 0, n - 1) res = max(res, dfs(i));
  cout << res << endl;
}
Problem p03166 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>

using namespace std;

#define ll long long

const unsigned long long MAXN = 3001;
const int MAXI = (int)2e9;
const ll INF = (1e18);

vector<vector<int>> g(MAXN, vector<int>());
int in_degree[MAXN], dp[MAXN];
bool was[MAXN];

void dfs(int v) {
  assert(!was[v]);
  was[v] = 1;
  for (int to : g[v]) {
    dp[to] = max(dp[to], dp[v] + 1);
    in_degree[to]--;
    if (in_degree[to] == 0)
      dfs(to);
  }
}

int main() {
  int n, m;
  cin >> n >> m;
  while (m--) {
    int u, w;
    cin >> u >> w;
    g[u].push_back(w);
    in_degree[w]++;
  }
  for (int i = 1; i <= n; i++) {
    if (!was[i] && in_degree[i] == 0) {
      dfs(i);
    }
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    ans = max(ans, dp[i]);
  }
  cout << ans;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>

using namespace std;

#define ll long long

const unsigned long long MAXN = int(1e5) + 5;
const ll INF = (1e18);

vector<vector<int>> g(MAXN, vector<int>());
int in_degree[MAXN], dp[MAXN];
bool was[MAXN];

void dfs(int v) {
  assert(!was[v]);
  was[v] = 1;
  for (int to : g[v]) {
    dp[to] = max(dp[to], dp[v] + 1);
    in_degree[to]--;
    if (in_degree[to] == 0)
      dfs(to);
  }
}

int main() {
  int n, m;
  cin >> n >> m;
  while (m--) {
    int u, w;
    cin >> u >> w;
    g[u].push_back(w);
    in_degree[w]++;
  }
  for (int i = 1; i <= n; i++) {
    if (!was[i] && in_degree[i] == 0) {
      dfs(i);
    }
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    ans = max(ans, dp[i]);
  }
  cout << ans;
  return 0;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
// G - Longest Path
#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
int in() {
  int x;
  scanf("%d", &x);
  return x;
}

#define VMAX 100000
vvi V(VMAX);
vi D(VMAX, -1);

int dfs(int p) {
  int a = D[p];
  if (a > -1)
    return a;
  for (int c : V[p])
    a = max(a, dfs(c) + 1);
  return a;
}

int main() {
  int N, M;
  cin >> N >> M;
  while (M--)
    V[in() - 1].push_back(in() - 1);
  int a = 0;
  while (N--)
    a = max(a, dfs(N) + 1);
  cout << a << endl;
}

Fixed submission:
// G - Longest Path
#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
int in() {
  int x;
  scanf("%d", &x);
  return x;
}

#define VMAX 100000
vvi V(VMAX);
vi D(VMAX, -1);

int dfs(int p) {
  int &a = D[p];
  if (a > -1)
    return a;
  for (int c : V[p])
    a = max(a, dfs(c) + 1);
  return a;
}

int main() {
  int N, M;
  cin >> N >> M;
  while (M--)
    V[in() - 1].push_back(in() - 1);
  int a = 0;
  while (N--)
    a = max(a, dfs(N) + 1);
  cout << a << endl;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <vector>
#define LL long long
using namespace std;
int n, m, d1, d2, dp[100005], vis[100005];
vector<int> v[100005];
int dfs(int x, int k) {
  //	cout << x << " " << k << "\n";
  int MMM = 0;
  if (dp[x] != 0)
    return dp[x] + k;
  if (v[x].empty())
    return k;
  for (auto i : v[x]) {
    //		cout << x << " " << i << "\n";
    MMM = max(dfs(i, k + 1), MMM);
  }
  return MMM;
}
int main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> d1 >> d2, v[d2].push_back(d1);
  }
  int Max = 0;
  for (int i = 1; i <= n; i++) {
    dp[i] = dfs(i, 0);
    Max = max(dp[i], Max);
  }
  cout << Max;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
#define LL long long
using namespace std;
int n, m, d1, d2, dp[100005], vis[100005];
vector<int> v[100005];
int dfs(int x, int k) {
  //	cout << x << " " << k << "\n";
  int MMM = 0;
  if (dp[x] != 0)
    return dp[x] + k;
  if (v[x].empty())
    return k;
  for (auto i : v[x]) {
    //		cout << x << " " << i << "\n";
    dp[i] = dfs(i, k + 1);
    MMM = max(dp[i], MMM);
    dp[i] -= (k + 1);
  }
  return MMM;
}
int main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> d1 >> d2, v[d2].push_back(d1);
  }
  int Max = 0;
  for (int i = 1; i <= n; i++) {
    dp[i] = dfs(i, 0);
    Max = max(dp[i], Max);
  }
  cout << Max;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
vector<vector<int>> vec;
vector<int> dp;

void dfs(int i) {
  if (vec[i].size() == 0) {
    dp[i] = 0;
  }
  for (auto v : vec[i]) {
    if (dp[v] = -1)
      dfs(v);
    dp[i] = max(dp[v] + 1, dp[i]);
  }
}

int main() {
  int n, m;
  cin >> n >> m;
  vec.assign(n, vector<int>(0));
  dp.assign(n, -1);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    vec[a].pb(b);
  }
  int mx = -1;
  for (int i = 0; i < n; i++) {
    if (dp[i] == -1)
      dfs(i);
    mx = max(mx, dp[i]);
  }
  cout << mx << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
vector<vector<int>> vec;
vector<int> dp;

void dfs(int i) {
  if (vec[i].size() == 0) {
    dp[i] = 0;
  }
  for (auto v : vec[i]) {
    if (dp[v] == -1)
      dfs(v);
    dp[i] = max(dp[v] + 1, dp[i]);
  }
}

int main() {
  int n, m;
  cin >> n >> m;
  vec.assign(n, vector<int>(0));
  dp.assign(n, -1);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    vec[a].pb(b);
  }
  int mx = -1;
  for (int i = 0; i < n; i++) {
    if (dp[i] == -1)
      dfs(i);
    mx = max(mx, dp[i]);
  }
  cout << mx << endl;
}
Problem p03166 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#define pb push_back
typedef long long ll;
const int MAXN = 3e3 + 5;
const int MAXW = 1e5 + 5;
const int MOD = 1e9 + 7;

using namespace std;
vector<int> g[MAXN];
int mem[MAXN];
int solve(int u, int c) {
  if (g[u].empty())
    return 0;
  int &ret = mem[u];
  if (ret != -1)
    return ret;
  ret = -1e9;
  for (auto &k : g[u])
    ret = max(ret, solve(k, c + 1) + 1);

  return ret;
}

int main() {

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

  cout << ans;
}


Fixed submission:
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#define pb push_back
typedef long long ll;
const int MAXN = 1e5 + 5;
const int MAXW = 1e5 + 5;
const int MOD = 1e9 + 7;

using namespace std;
vector<int> g[MAXN];
int mem[MAXN];
int solve(int u, int c) {
  if (g[u].empty())
    return 0;
  int &ret = mem[u];
  if (ret != -1)
    return ret;
  ret = -1e9;
  for (auto &k : g[u])
    ret = max(ret, solve(k, c + 1) + 1);

  return ret;
}

int main() {

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

  cout << ans;
}