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

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

using namespace std;

vector<int> adj[10007];

int used[100007], dp[100007], mx;

void dfs(int v) {
  if (used[v])
    return;
  used[v] = 1;
  for (auto u : adj[v]) {
    dfs(u);
    dp[v] = max(dp[v], dp[u] + 1);
  }
}

int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    adj[u].push_back(v);
  }
  for (int i = 1; i <= n; i++) {
    dfs(i);
    mx = max(mx, dp[i]);
  }
  cout << mx << endl;
}

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

using namespace std;

vector<int> adj[100007];

int used[100007], dp[100007], mx;

void dfs(int v) {
  if (used[v])
    return;
  used[v] = 1;
  for (auto u : adj[v]) {
    dfs(u);
    dp[v] = max(dp[v], dp[u] + 1);
  }
}

int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    adj[u].push_back(v);
  }
  for (int i = 1; i <= n; i++) {
    dfs(i);
    mx = max(mx, dp[i]);
  }
  cout << mx << endl;
}
Problem p03166 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
vector<int> g[100000];
int visited[100000];
int dp[100000];
void dfs(int node) {
  dp[node] = 0;
  visited[node] = 1;
  for (auto child : g[node]) {
    if (!visited[child]) {
      dfs(child);
    }
    dp[node] = max(dp[node], 1 + dp[child]);
  }
}
int main() {
  int n, m, i, j, k;
  cin >> n >> m;
  for (i = 0; i < m; i++)
    cin >> j >> k, g[j].push_back(k);
  memset(visited, 0, sizeof(visited));
  for (i = 1; i <= n; i++)
    if (!visited[i])
      dfs(i);
  int ans = 0;
  for (i = 1; i <= n; i++)
    ans = max(ans, dp[i]);
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
vector<int> g[100001];
int visited[100001];
int dp[100001];
void dfs(int node) {
  dp[node] = 0;
  visited[node] = 1;
  for (auto child : g[node]) {
    if (!visited[child]) {
      dfs(child);
    }
    dp[node] = max(dp[node], 1 + dp[child]);
  }
}
int main() {
  int n, m, i, j, k;
  cin >> n >> m;
  for (i = 0; i < m; i++)
    cin >> j >> k, g[j].push_back(k);
  memset(visited, 0, sizeof(visited));
  for (i = 1; i <= n; i++)
    if (!visited[i])
      dfs(i);
  int ans = 0;
  for (i = 1; i <= n; i++)
    ans = max(ans, dp[i]);
  cout << ans << endl;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
vector<int> g[100005];
int dp[100005], vis[100005], mx;

void dfs(int node) {
  vis[node] = 1;
  for (int a : g[node])
    dfs(a), dp[node] = max(dp[a] + 1, dp[node]);
  mx = max(mx, dp[node]);
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m;
  cin >> n >> m;
  while (m--) {
    int a, b;
    cin >> a >> b;
    g[a].push_back(b);
  }
  for (int i = 1; i <= n; i++) {
    if (!vis[i])
      dfs(i);
  }
  cout << mx << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
vector<int> g[100005];
int dp[100005], vis[100005], mx;

void dfs(int node) {
  vis[node] = 1;
  for (int a : g[node]) {
    if (!vis[a])
      dfs(a);
    dp[node] = max(dp[a] + 1, dp[node]);
  }
  mx = max(mx, dp[node]);
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m;
  cin >> n >> m;
  while (m--) {
    int a, b;
    cin >> a >> b;
    g[a].push_back(b);
  }
  for (int i = 1; i <= n; i++) {
    if (!vis[i])
      dfs(i);
  }
  cout << mx << endl;
}
Problem p03166 (C++)

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

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

  vector<int> h(N, -1);
  function<int(int, int)> dfs = [&](int v, int p) {
    if (h[v] >= 0)
      return h[v];
    if (G[v].size() == 0)
      return 0;
    int x = 0;
    for (int u : G[v]) {
      if (u == p)
        continue;
      x = max(x, dfs(u, v) + 1);
    }
    return x;
  };

  ll ans = 0;
  for (int i = 0; i < N; i++) {
    if (h[i] >= 0)
      continue;
    ans = max(ans, (ll)dfs(i, -1));
  }
  return ans;
}

int main() {
  auto ans = solve();
  cout << ans << "\n";
  return 0;
}

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

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

  vector<int> h(N, -1);
  function<int(int, int)> dfs = [&](int v, int p) {
    if (h[v] >= 0)
      return h[v];
    if (G[v].size() == 0)
      return 0;
    int x = 0;
    for (int u : G[v]) {
      if (u == p)
        continue;
      x = max(x, dfs(u, v) + 1);
    }
    h[v] = x;
    return x;
  };

  ll ans = 0;
  for (int i = 0; i < N; i++) {
    if (h[i] >= 0)
      continue;
    ans = max(ans, (ll)dfs(i, -1));
  }
  return ans;
}

int main() {
  auto ans = solve();
  cout << ans << "\n";
  return 0;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
const int maxValue = 1e5 + 5;

int dfs(int i, vector<int> g[], int *maxNodes, int dist) {
  if (g[i].size() == 0) {
    maxNodes[i] = 1;
  }
  for (int j : g[i]) {
    maxNodes[i] = max(maxNodes[i], dfs(j, g, maxNodes, dist) + 1);
  }
  return maxNodes[i];
}

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> g[n + 1];
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    g[u].push_back(v);
  }
  int maxNodes[n + 1];
  for (int i = 0; i <= n; i++) {
    maxNodes[i] = 0;
  }
  for (int i = 1; i <= n; i++) {
    if (maxNodes[i] == 0) {
      dfs(i, g, maxNodes, 0);
    }
  }
  int ans = 0;
  for (int i = 0; i <= n; i++) {
    ans = max(ans, maxNodes[i]);
  }
  ans -= 1;
  cout << ans;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
const int maxValue = 1e5 + 5;

int dfs(int i, vector<int> g[], int *maxNodes, int dist) {
  if (g[i].size() == 0) {
    maxNodes[i] = 1;
  }
  if (maxNodes[i] != 0)
    return maxNodes[i];
  for (int j : g[i]) {
    maxNodes[i] = max(maxNodes[i], dfs(j, g, maxNodes, dist) + 1);
  }
  return maxNodes[i];
}

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> g[n + 1];
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    g[u].push_back(v);
  }
  int maxNodes[n + 1];
  for (int i = 0; i <= n; i++) {
    maxNodes[i] = 0;
  }
  for (int i = 1; i <= n; i++) {
    if (maxNodes[i] == 0) {
      dfs(i, g, maxNodes, 0);
    }
  }
  int ans = 0;
  for (int i = 0; i <= n; i++) {
    ans = max(ans, maxNodes[i]);
  }
  ans -= 1;
  cout << ans;
}
Problem p03166 (C++)

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

using namespace std;

using ll = long long;

constexpr int N_MAX = 100000;

int n, m;
vector<int> graph[N_MAX];
vector<int> lp(N_MAX, -1);

int dfs(int node) {
  int max_child_lp = -1;
  for (int child : graph[node]) {
    if (lp[child] != -1) {
      max_child_lp = max(max_child_lp, lp[child]);
    } else {
      max_child_lp = max(max_child_lp, dfs(child));
    }
  }

  return max_child_lp + 1;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> n >> m;
  int x, y;
  for (int i = 0; i < m; ++i) {
    cin >> x >> y;
    --x;
    --y;
    graph[x].push_back(y);
  }

  int ans = 0;
  for (int i = 0; i < n; ++i) {
    if (lp[i] != -1) {
      ans = max(ans, lp[i]);
      continue;
    } else {
      ans = max(ans, dfs(i));
    }
  }

  cout << ans << endl;
}


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

using namespace std;

using ll = long long;

constexpr int N_MAX = 100000;

int n, m;
vector<int> graph[N_MAX];
vector<int> lp(N_MAX, -1);

int dfs(int node) {
  int max_child_lp = -1;
  for (int child : graph[node]) {
    if (lp[child] != -1) {
      max_child_lp = max(max_child_lp, lp[child]);
    } else {
      max_child_lp = max(max_child_lp, dfs(child));
    }
  }

  return lp[node] = max_child_lp + 1;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> n >> m;
  int x, y;
  for (int i = 0; i < m; ++i) {
    cin >> x >> y;
    --x;
    --y;
    graph[x].push_back(y);
  }

  int ans = 0;
  for (int i = 0; i < n; ++i) {
    if (lp[i] != -1) {
      ans = max(ans, lp[i]);
      continue;
    } else {
      ans = max(ans, dfs(i));
    }
  }

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

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

using namespace std;

vector<int> dp;

int longestPath(vector<vector<int>> &G, int v) {
  if (dp[v] == -1) {
    int r = 0;
    for (int i = 0; i < G[v].size(); i++) {
      r = max(r, 1 + longestPath(G, G[v][i]));
    }
    dp[v] = r;
  }
  return dp[v];
}

int main() {
  int N, M;
  cin >> N >> M;
  vector<vector<int>> G(N);
  for (int i = 0; i < M; i++) {
    int x, y;
    cin >> x >> y;
    G[x].push_back(y);
  }
  int r = 0;
  dp = vector<int>(N, -1);
  for (int i = 0; i < N; i++)
    r = max(r, longestPath(G, i));
  cout << r;
  return 0;
}

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

using namespace std;

vector<int> dp;

int longestPath(vector<vector<int>> &G, int v) {
  if (dp[v] == -1) {
    int r = 0;
    for (int i = 0; i < G[v].size(); i++) {
      r = max(r, 1 + longestPath(G, G[v][i]));
    }
    dp[v] = r;
  }
  return dp[v];
}

int main() {
  int N, M;
  cin >> N >> M;
  vector<vector<int>> G(N);
  for (int i = 0; i < M; i++) {
    int x, y;
    cin >> x >> y;
    G[x - 1].push_back(y - 1);
  }
  int r = 0;
  dp = vector<int>(N, -1);
  for (int i = 0; i < N; i++)
    r = max(r, longestPath(G, i));
  cout << r;
  return 0;
}
Problem p03166 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int js, a[100010], to[100010];
int n, m, c, b, ans, sum, dp[100010];
vector<int> v[100010];
vector<int> vv[10010];
inline void topsort() {
  queue<int> q;
  for (int i = 1; i <= n; ++i) {
    if (!to[i])
      q.push(i);
  }
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    if (to[x] == 0)
      a[++js] = x;
    for (int i = 0; i < v[x].size(); ++i) {
      to[v[x][i]]--;
      if (to[v[x][i]] == 0)
        q.push(v[x][i]);
    }
  }
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= m; ++i) {
    scanf("%d%d", &c, &b);
    v[c].push_back(b);
    vv[b].push_back(c);
    to[b]++;
  }
  topsort();
  // for(int i=1;i<=n;++i)cout<<a[i]<<" ";
  // cout<<endl;
  for (int i = 1; i <= n; ++i) {
    int k = a[i];
    for (int j = 0; j < vv[k].size(); ++j) {
      dp[k] = max(dp[k], dp[vv[k][j]] + 1);
      ans = max(ans, dp[k]);
    }
  }
  for (int i = 1; i <= n; ++i)
    ans = max(ans, dp[i]);
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int js, a[100010], to[100010];
int n, m, c, b, ans, sum, dp[100010];
vector<int> v[100010];
vector<int> vv[100010];
inline void topsort() {
  queue<int> q;
  for (int i = 1; i <= n; ++i) {
    if (!to[i])
      q.push(i);
  }
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    if (to[x] == 0)
      a[++js] = x;
    for (int i = 0; i < v[x].size(); ++i) {
      to[v[x][i]]--;
      if (to[v[x][i]] == 0)
        q.push(v[x][i]);
    }
  }
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= m; ++i) {
    scanf("%d%d", &c, &b);
    v[c].push_back(b);
    vv[b].push_back(c);
    to[b]++;
  }
  topsort();
  // for(int i=1;i<=n;++i)cout<<a[i]<<" ";
  // cout<<endl;
  for (int i = 1; i <= n; ++i) {
    int k = a[i];
    for (int j = 0; j < vv[k].size(); ++j) {
      dp[k] = max(dp[k], dp[vv[k][j]] + 1);
      ans = max(ans, dp[k]);
    }
  }
  for (int i = 1; i <= n; ++i)
    ans = max(ans, dp[i]);
  cout << ans << endl;
  return 0;
}
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 repr(i, n) for (int i = (n - 1); i >= 0; --i)

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

typedef long long ll;
typedef pair<int, int> P;

const int MAX = 200005;
const int INF = 1001001001;
const int MOD = 1000000007;

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

int dfs(int v) {
  int res = -1;
  for (int u : G[v]) {
    chmax(res, dfs(u));
  }
  if (res < 0)
    return dp[v] = 0;
  return dp[v] = res + 1;
}

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

  rep(i, M) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    G[a].push_back(b);
  }

  rep(i, N) dp[i] = -1;
  rep(i, N) {
    if (dp[i] < 0)
      dfs(i);
  }

  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 repr(i, n) for (int i = (n - 1); i >= 0; --i)

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

typedef long long ll;
typedef pair<int, int> P;

const int MAX = 200005;
const int INF = 1001001001;
const int MOD = 1000000007;

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

int dfs(int v) {
  int res = -1;
  for (int u : G[v]) {
    if (dp[u] < 0)
      chmax(res, dfs(u));
    else
      chmax(res, dp[u]);
  }
  if (res < 0)
    return dp[v] = 0;
  return dp[v] = res + 1;
}

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

  rep(i, M) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    G[a].push_back(b);
  }

  rep(i, N) dp[i] = -1;
  rep(i, N) {
    if (dp[i] < 0)
      dfs(i);
  }

  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 <iostream>
#include <vector>

using namespace std;

typedef long long ll;

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

ll dfs(int u) {
  ll d = 0;
  if (dp[u] != -1) {
    return dp[u];
  }

  for (int w : v[u]) {
    d = max(d, dfs(w) + 1);
  }

  return d;
}

int main(int argc, char *argv[]) {
  int N, M;

  cin >> N >> M;

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

  dp = vector<int>(100005, -1);
  ll ans = 0;
  for (int i = 0; i < N; i++) {
    ans = max(ans, dfs(i));
  }

  cout << ans << endl;

  return 0;
}

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

using namespace std;

typedef long long ll;

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

ll dfs(int u) {
  ll d = 0;
  if (dp[u] != -1) {
    return dp[u];
  }

  for (int w : v[u]) {
    d = max(d, dfs(w) + 1);
  }

  return dp[u] = d;
}

int main(int argc, char *argv[]) {
  int N, M;

  cin >> N >> M;

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

  dp = vector<int>(100005, -1);
  ll 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>> from;
vector<vector<int>> to;
vector<int> start, finish;
vector<int> d;

int dist(int u) {
  if (d[u] != -1)
    return d[u];

  int md = -1;
  for (int i = 0; i < from[u].size(); i++) {
    int v = from[u][i];
    md = max(md, dist(v) + 1);
  }

  return md;
}

int main(void) {
  cin >> n >> m;
  from = vector<vector<int>>(n, vector<int>());
  to = vector<vector<int>>(n, vector<int>());
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    from[u - 1].push_back(v - 1);
    to[v - 1].push_back(u - 1);
  }

  for (int i = 0; i < n; i++) {
    if (from[i].size() == 0)
      finish.push_back(i);
    if (to[i].size() == 0)
      start.push_back(i);
  }

  int md = -1;
  d = vector<int>(n, -1);
  for (int i = 0; i < finish.size(); i++)
    d[finish[i]] = 0;
  for (int i = 0; i < start.size(); i++) {
    int u = start[i];
    md = max(md, dist(u));
  }
  cout << md;

  return 0;
}


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

int n, m;
vector<vector<int>> from;
vector<vector<int>> to;
vector<int> start, finish;
vector<int> d;

int dist(int u) {
  if (d[u] != -1)
    return d[u];

  int md = -1;
  for (int i = 0; i < from[u].size(); i++) {
    int v = from[u][i];
    md = max(md, dist(v) + 1);
  }

  d[u] = md;
  return md;
}

int main(void) {
  cin >> n >> m;
  from = vector<vector<int>>(n, vector<int>());
  to = vector<vector<int>>(n, vector<int>());
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    from[u - 1].push_back(v - 1);
    to[v - 1].push_back(u - 1);
  }

  for (int i = 0; i < n; i++) {
    if (from[i].size() == 0)
      finish.push_back(i);
    if (to[i].size() == 0)
      start.push_back(i);
  }

  int md = -1;
  d = vector<int>(n, -1);
  for (int i = 0; i < finish.size(); i++)
    d[finish[i]] = 0;
  for (int i = 0; i < start.size(); i++) {
    int u = start[i];
    md = max(md, dist(u));
  }
  cout << md;

  return 0;
}
Problem p03166 (Python)

Buggy submission (status: Runtime Error):
def main():
    n, m = map(int, input().split())
    graph = [[] for _ in range(n)]
    for _ in range(m):
        x, y = map(lambda x: int(x) - 1, input().split())
        graph[x].append(y)
    dp = [-1 for _ in range(n)]
    # dp[i] := iから距離が最も長い頂点への距離

    def solve(v):
        if dp[v] != -1:
            return dp[v]
        res = 0
        for next_v in graph[v]:
            res = max(res, solve(next_v) + 1)
        dp[v] = res
        return dp[v]

    ans = 0
    for i in range(n):
        ans = max(ans, solve(i))
    print(ans)


if __name__ == "__main__":
    main()


Fixed submission:
from sys import setrecursionlimit

setrecursionlimit(10**7)


def main():
    n, m = map(int, input().split())
    graph = [[] for _ in range(n)]
    for _ in range(m):
        x, y = map(lambda x: int(x) - 1, input().split())
        graph[x].append(y)
    dp = [-1 for _ in range(n)]
    # dp[i] := iから距離が最も長い頂点への距離

    def solve(v):
        if dp[v] != -1:
            return dp[v]
        res = 0
        for next_v in graph[v]:
            res = max(res, solve(next_v) + 1)
        dp[v] = res
        return dp[v]

    ans = 0
    for i in range(n):
        ans = max(ans, solve(i))
    print(ans)


if __name__ == "__main__":
    main()
Problem p03166 (Python)

Buggy submission (status: Runtime Error):
N, M = map(int, input().split())
G = [[] for i in range(N)]
for i in range(M):
    x, y = map(int, input().split())
    x, y = x - 1, y - 1
    G[x].append(y)


def topological_sort(g):
    topological_list = []
    visited = set()

    def dfs(n):
        for e in g[n]:
            if e not in visited:
                dfs(e)
        topological_list.append(n)
        visited.add(n)

    for i in range(len(g)):
        if i not in visited:
            dfs(i)

    return topological_list[::-1]


T = topological_sort(G)
dist = [0] * N
for n in T:
    for v in G[n]:
        dist[v] = max(dist[v], dist[n] + 1)

print(max(dist))


Fixed submission:
import sys

sys.setrecursionlimit(10**9)

N, M = map(int, input().split())
G = [[] for i in range(N)]
for i in range(M):
    x, y = map(int, input().split())
    x, y = x - 1, y - 1
    G[x].append(y)


def topological_sort(g):
    topological_list = []
    visited = set()

    def dfs(n):
        for e in g[n]:
            if e not in visited:
                dfs(e)
        topological_list.append(n)
        visited.add(n)

    for i in range(len(g)):
        if i not in visited:
            dfs(i)

    return topological_list[::-1]


T = topological_sort(G)
dist = [0] * N
for n in T:
    for v in G[n]:
        dist[v] = max(dist[v], dist[n] + 1)

print(max(dist))
Problem p03166 (Python)

Buggy submission (status: Runtime Error):
import sys
from functools import lru_cache

sys.setrecursionlimit(100000)


@lru_cache(maxsize=None)
def dfs(v):
    lv = links[v]
    return max(map(dfs, lv)) + 1 if lv else 0


n, m = map(int, input().split())
sss = set(range(n))
links = [set() for _ in range(n)]
for line in sys.stdin.readlines():
    x, y = map(int, line.split())
    x -= 1
    y -= 1
    sss.discard(y)
    links[x].add(y)

print(max(map(dfs, sss)))


Fixed submission:
import sys
from functools import lru_cache

sys.setrecursionlimit(1000000)


@lru_cache(maxsize=None)
def dfs(v):
    lv = links[v]
    return max(map(dfs, lv)) + 1 if lv else 0


n, m = map(int, input().split())
sss = set(range(n))
links = [set() for _ in range(n)]
for line in sys.stdin.readlines():
    x, y = map(int, line.split())
    x -= 1
    y -= 1
    sss.discard(y)
    links[x].add(y)

print(max(map(dfs, sss)))
Problem p03166 (Python)

Buggy submission (status: Runtime Error):
def get_kyori(i):
    if kos[i] == []:
        kyori[i] = 0
        return 0
    if kyori[i] > -1:
        return kyori[i]
    re = 0
    for ko in kos[i]:
        new = get_kyori(ko) + 1
        if re < new:
            re = new
    kyori[i] = re
    return re


n, m = map(int, input().split())
kos = [[] for _ in range(n + 1)]
kyori = [-1] * (n + 1)
for _ in range(m):
    oya, ko = map(int, input().split())
    kos[oya] += [ko]

for i in range(1, n + 1):
    if kyori[i] == -1:
        kyori[i] = get_kyori(i)

print(max(kyori[1:]))


Fixed submission:
import sys

input = sys.stdin.readline
sys.setrecursionlimit(10**6)


def get_kyori(i):
    if kos[i] == []:
        kyori[i] = 0
        return 0
    if kyori[i] > -1:
        return kyori[i]
    re = 0
    for ko in kos[i]:
        new = get_kyori(ko) + 1
        if re < new:
            re = new
    kyori[i] = re
    return re


n, m = map(int, input().split())
kos = [[] for _ in range(n + 1)]
kyori = [-1] * (n + 1)
for _ in range(m):
    oya, ko = map(int, input().split())
    kos[oya] += [ko]

for i in range(1, n + 1):
    if kyori[i] == -1:
        kyori[i] = get_kyori(i)

print(max(kyori[1:]))
Problem p03166 (Python)

Buggy submission (status: Runtime Error):
N, M = map(int, input().split())
graph = [dict() for _ in range(N)]

for i in range(M):
    x, y = map(int, input().split())
    graph[y - 1][x - 1] = 1

dp = [0 for _ in range(N)]


def dfs(u):
    if dp[u] != 0:
        return dp[u]
    max_dis = 0
    for f in graph[u].keys():
        if max_dis < dfs(f) + 1:
            max_dis = dfs(f) + 1

    dp[u] = max_dis
    return dp[u]


for i in range(N):
    dfs(i)
print(max(dp))


Fixed submission:
import sys

sys.setrecursionlimit(10**8)

N, M = map(int, input().split())
graph = [dict() for _ in range(N)]

for i in range(M):
    x, y = map(int, input().split())
    graph[y - 1][x - 1] = 1

dp = [0 for _ in range(N)]


def dfs(u):
    if dp[u] != 0:
        return dp[u]
    max_dis = 0
    for f in graph[u].keys():
        if max_dis < dfs(f) + 1:
            max_dis = dfs(f) + 1

    dp[u] = max_dis
    return dp[u]


for i in range(N):
    dfs(i)
print(max(dp))
Problem p03166 (Python)

Buggy submission (status: Runtime Error):
def dfs(v):
    if d[v] != -1:
        return d[v]
    res = -1
    for u in e[v]:
        res = max(res, dfs(u))
    res += 1
    d[v] = res
    return res


n, m = map(int, input().split())
e = tuple(set() for _ in range(n))
for _ in range(m):
    x, y = map(int, input().split())
    x -= 1
    y -= 1
    e[x].add(y)

d = [-1] * n
res = -1
for v in range(n):
    res = max(res, dfs(v))
print(res)


Fixed submission:
import sys

sys.setrecursionlimit(10**7)


def dfs(v):
    if d[v] != -1:
        return d[v]
    res = -1
    for u in e[v]:
        res = max(res, dfs(u))
    res += 1
    d[v] = res
    return res


n, m = map(int, input().split())
e = tuple(set() for _ in range(n))
for _ in range(m):
    x, y = map(int, input().split())
    x -= 1
    y -= 1
    e[x].add(y)

d = [-1] * n
res = -1
for v in range(n):
    res = max(res, dfs(v))
print(res)
Problem p03166 (Python)

Buggy submission (status: Runtime Error):
import sys


def input():
    return sys.stdin.readline().strip()


def dfs(v):
    if dp[v] >= 0:
        return dp[v]
    res = 0
    for nv in g[v]:
        res = max(res, dfs(nv) + 1)
    dp[v] = res
    return res


n, m = map(int, input().split())
g = {i: set() for i in range(1, n + 1)}
for _ in range(m):
    x, y = map(int, input().split())
    g[x].add(y)

dp = [-1] * (n + 1)
for v in g:
    dp[v] = max(dp[v], dfs(v))
print(max(dp))


Fixed submission:
import sys

sys.setrecursionlimit(10**7)


def input():
    return sys.stdin.readline().strip()


def dfs(v):
    if dp[v] >= 0:
        return dp[v]
    res = 0
    for nv in g[v]:
        res = max(res, dfs(nv) + 1)
    dp[v] = res
    return res


n, m = map(int, input().split())
g = {i: set() for i in range(1, n + 1)}
for _ in range(m):
    x, y = map(int, input().split())
    g[x].add(y)

dp = [-1] * (n + 1)
for v in g:
    dp[v] = max(dp[v], dfs(v))
print(max(dp))
Problem p03166 (C++)

Buggy submission (status: Runtime Error):
/***********************
 *  Author: xuziyuan    *
 ***********************/

#include <bits/stdc++.h>

#define rt0 return 0
#define rep(i, n) for (int i = 0; i < n; i++)
#define repn(i, n) for (int i = 1; i <= n; i++)
#define replet(c) for (char c = 'a'; c <= 'z'; c++)
#define LL long long
#define pii pair<int, int>
#define pb push_back
#define fi first
#define se second
#define mpr make_pair
#define sqr(a) ((a) * (a))

using namespace std;

const LL MOD = 1e9 + 7;

int n, m, dp[100010], num[100010];
vector<int> g[100010], dg[100010], topo;
bool vis[5010];

void dfs(int pos) {
  if (vis[pos])
    return;
  vis[pos] = true;
  rep(i, g[pos].size()) dfs(g[pos][i]);
  topo.pb(pos);
}

int main() {
  rep(i, 100005) dp[i] = 1;
  cin >> n >> m;
  rep(i, m) {
    int x, y;
    scanf("%d%d", &x, &y);
    g[x].pb(y);
    dg[y].pb(x);
  }
  repn(i, n) if (!vis[i]) dfs(i);
  reverse(topo.begin(), topo.end());
  rep(i, topo.size()) num[topo[i]] = i;
  rep(i, topo.size()) {
    rep(j, dg[topo[i]].size()) dp[i] = max(dp[i], dp[num[dg[topo[i]][j]]] + 1);
  }
  int ans = 0;
  rep(i, n) ans = max(ans, dp[i]);
  cout << ans - 1 << endl;
  rt0;
}

Fixed submission:
/***********************
 *  Author: xuziyuan    *
 ***********************/

#include <bits/stdc++.h>

#define rt0 return 0
#define rep(i, n) for (int i = 0; i < n; i++)
#define repn(i, n) for (int i = 1; i <= n; i++)
#define replet(c) for (char c = 'a'; c <= 'z'; c++)
#define LL long long
#define pii pair<int, int>
#define pb push_back
#define fi first
#define se second
#define mpr make_pair
#define sqr(a) ((a) * (a))

using namespace std;

const LL MOD = 1e9 + 7;

int n, m, dp[100010], num[100010];
vector<int> g[100010], dg[100010], topo;
bool vis[100010];

void dfs(int pos) {
  if (vis[pos])
    return;
  vis[pos] = true;
  rep(i, g[pos].size()) dfs(g[pos][i]);
  topo.pb(pos);
}

int main() {
  rep(i, 100005) dp[i] = 1;
  cin >> n >> m;
  rep(i, m) {
    int x, y;
    scanf("%d%d", &x, &y);
    g[x].pb(y);
    dg[y].pb(x);
  }
  repn(i, n) if (!vis[i]) dfs(i);
  reverse(topo.begin(), topo.end());
  rep(i, topo.size()) num[topo[i]] = i;
  rep(i, topo.size()) {
    rep(j, dg[topo[i]].size()) dp[i] = max(dp[i], dp[num[dg[topo[i]][j]]] + 1);
  }
  int ans = 0;
  rep(i, n) ans = max(ans, dp[i]);
  cout << ans - 1 << endl;
  rt0;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
using ll = long long;

int f(vector<int> &dp, vector<vector<int>> graph, int vertex) {
  if (dp[vertex] != -1)
    return dp[vertex];
  if (graph[vertex].size() == 0) {
    dp[vertex] = 0;
    return 0;
  }
  for (int i = 0; i < graph[vertex].size(); i++) {
    dp[vertex] = max(dp[vertex], f(dp, graph, graph[vertex][i]) + 1);
  }
  return dp[vertex];
}

int main() {
  int N, M;
  cin >> N >> M;
  vector<vector<int>> graph(N, vector<int>());
  for (int i = 0; i < M; i++) {
    int _a, _b;
    cin >> _a >> _b;
    graph[_a - 1].push_back(_b - 1);
  }
  int res = 0;
  vector<int> dp(N, -1);
  for (int i = 0; i < N; i++) {
    res = max(res, f(dp, graph, i));
  }
  cout << res << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
using ll = long long;

int f(vector<int> &dp, vector<vector<int>> &graph, int vertex) {
  if (dp[vertex] != -1)
    return dp[vertex];
  if (graph[vertex].size() == 0) {
    dp[vertex] = 0;
    return 0;
  }
  for (int i = 0; i < graph[vertex].size(); i++) {
    dp[vertex] = max(dp[vertex], f(dp, graph, graph[vertex][i]) + 1);
  }
  return dp[vertex];
}

int main() {
  int N, M;
  cin >> N >> M;
  vector<vector<int>> graph(N, vector<int>());
  for (int i = 0; i < M; i++) {
    int _a, _b;
    cin >> _a >> _b;
    graph[_a - 1].push_back(_b - 1);
  }
  int res = 0;
  vector<int> dp(N, -1);
  for (int i = 0; i < N; i++) {
    res = max(res, f(dp, graph, i));
  }
  cout << res << endl;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<int64_t, int64_t> pll;
typedef vector<int64_t> vl;
int dx[] = {1, -1, 0, 0, 1, -1, 1, -1};
int dy[] = {0, 0, 1, -1, 1, -1, -1, 1};
const long double PI = acos(-1);
#define INF (2147483647)
#define mod (1000000007)
#define limit (7368791)
#define rep(i, a, b) for (int64_t i = (a); i < (b); i++)
#define REP(i, n) rep(i, 0, n)
#define sz(s) (s).size()
#define ALL(a) begin(a), end(a)
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define fi first
#define se second

vector<int> G[100010];
vector<int> dp(100010, 0);
int r() {
  int x;
  cin >> x;
  return x;
}
int dfs(int v) {
  if (dp[v])
    return dp[v];
  int mx = 0;
  for (auto it : G[v])
    mx = max(mx, dfs(it) + 1);
  return mx;
}

void solve() {
  int n, m, ans = 0;
  cin >> n >> m;
  REP(i, m) G[r() - 1].pb(r() - 1);
  REP(i, n) ans = max(ans, dfs(i));
  cout << ans << endl;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  solve();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<int64_t, int64_t> pll;
typedef vector<int64_t> vl;
int dx[] = {1, -1, 0, 0, 1, -1, 1, -1};
int dy[] = {0, 0, 1, -1, 1, -1, -1, 1};
const long double PI = acos(-1);
#define INF (2147483647)
#define mod (1000000007)
#define limit (7368791)
#define rep(i, a, b) for (int64_t i = (a); i < (b); i++)
#define REP(i, n) rep(i, 0, n)
#define sz(s) (s).size()
#define ALL(a) begin(a), end(a)
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define fi first
#define se second

vector<int> G[100010];
vector<int> dp(100010, 0);
int r() {
  int x;
  cin >> x;
  return x;
}
int dfs(int v) {
  if (dp[v])
    return dp[v];
  int mx = 0;
  for (auto it : G[v])
    mx = max(mx, dfs(it) + 1);
  dp[v] = mx;
  return mx;
}

void solve() {
  int n, m, ans = 0;
  cin >> n >> m;
  REP(i, m) G[r() - 1].pb(r() - 1);
  REP(i, n) ans = max(ans, dfs(i));
  cout << ans << endl;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  solve();
  return 0;
}
Problem p03166 (C++)

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

using namespace std;
using ll = long long;

int n, m;
vector<vector<int>> adj;
vector<int> in_dig, s_path;
queue<int> Q;

void bfs() {
  while (!Q.empty()) {
    auto node = Q.front();
    Q.pop();
    for (auto child : adj[node]) {
      s_path[child] = max(s_path[child], s_path[node] + 1);
      // if(!vis[child]){
      Q.push(child);
      // vis[child]=1;
      // }
    }
  }
}

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

  cin >> n >> m;
  adj.resize(n + 1);
  in_dig.resize(n + 1);
  s_path.resize(n + 1);
  // vis.resize(n+1);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    adj[a].push_back(b);
    in_dig[b]++;
  }

  for (int node = 1; node <= n; node++) {
    if (in_dig[node] == 0)
      Q.push(node);
  }

  bfs();

  int ans = 0;
  for (int node = 1; node <= n; node++)
    ans = max(ans, s_path[node]);
  cout << ans << endl;

  return 0;
}

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

using namespace std;
using ll = long long;

int n, m;
vector<vector<int>> adj;
vector<int> in_dig, s_path;
queue<int> Q;

void bfs() {
  while (!Q.empty()) {
    auto node = Q.front();
    Q.pop();
    for (auto child : adj[node]) {
      s_path[child] = max(s_path[child], s_path[node] + 1);
      in_dig[child]--;
      if (!in_dig[child]) {
        Q.push(child);
      }
    }
  }
}

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

  cin >> n >> m;
  adj.resize(n + 1);
  in_dig.resize(n + 1);
  s_path.resize(n + 1);
  // vis.resize(n+1);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    adj[a].push_back(b);
    in_dig[b]++;
  }

  for (int node = 1; node <= n; node++) {
    if (in_dig[node] == 0)
      Q.push(node);
  }

  bfs();

  int ans = 0;
  for (int node = 1; node <= n; node++)
    ans = max(ans, s_path[node]);
  cout << ans << endl;

  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)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FORD(i, b, a) for (int i = b; i >= a; --i)

#define sz(v) ((int)v.size())
#define all(v) v.begin(), v.end()
#define uni(v) v.erase(unique(all(v)), v.end())

using namespace std;

const int maxn = 1e5 + 10;

int n, m, timer;
int dp[maxn];
vector<int> adj[maxn];

void dfs(int u) {
  for (int v : adj[u]) {
    dfs(v);
    dp[u] = max(dp[u], dp[v] + 1);
  }
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n >> m;
  REP(i, m) {
    int u, v;
    cin >> u >> v;
    adj[u].push_back(v);
  }
  int answer = 0;
  FOR(i, 1, n) {
    dfs(i);
    answer = max(answer, dp[i]);
  }
  cout << answer << "\n";
  return 0;
}


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

#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FORD(i, b, a) for (int i = b; i >= a; --i)

#define sz(v) ((int)v.size())
#define all(v) v.begin(), v.end()
#define uni(v) v.erase(unique(all(v)), v.end())

using namespace std;

const int maxn = 1e5 + 10;

int n, m, timer;
int dp[maxn];
vector<int> adj[maxn];

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

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n >> m;
  REP(i, m) {
    int u, v;
    cin >> u >> v;
    adj[u].push_back(v);
  }
  int answer = 0;
  FOR(i, 1, n) {
    dfs(i);
    answer = max(answer, dp[i]);
  }
  cout << answer << "\n";
  return 0;
}
Problem p03166 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
#define IO                                                                     \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)

int n, m;
int dp[10005];
vector<int> adj[100005];

int solve(int node) {
  int &ret = dp[node];
  if (~ret)
    return ret;
  ret = 0;
  for (auto &e : adj[node])
    ret = max(ret, 1 + solve(e));
  return ret;
}

int main() {
  IO;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    adj[a].push_back(b);
  }
  int ans = 0;
  memset(dp, -1, sizeof dp);
  for (int i = 1; i <= n; i++)
    ans = max(ans, solve(i));
  cout << ans;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
#define IO                                                                     \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)

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

int solve(int node) {
  int &ret = dp[node];
  if (~ret)
    return ret;
  ret = 0;
  for (auto &e : adj[node])
    ret = max(ret, 1 + solve(e));
  return ret;
}

int main() {
  IO;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    adj[a].push_back(b);
  }
  int ans = 0;
  memset(dp, -1, sizeof dp);
  for (int i = 1; i <= n; i++)
    ans = max(ans, solve(i));
  cout << ans;
  return 0;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using vi = std::vector<int>;
template <class X, class Y> X &remax(X &x, Y y) { return x = (y > x) ? y : x; }
const int NMAX = 100100;
int nV, nE;
vi next[NMAX];
int cnt[NMAX];
int dfs(int x) {
  for (int y : next[x]) {
    remax(cnt[x], dfs(y) + 1);
  }
  remax(cnt[x], 0);
  return cnt[x];
}
int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(0);
  while (std::cin >> nV >> nE) {
    std::fill(cnt, cnt + NMAX, -1);
    std::fill(next, next + NMAX, vi{});
    for (int i = 0; i < nE; i++) {
      int x, y;
      std::cin >> x >> y;
      next[x].push_back(y);
    }
    int answ = 0;
    for (int i = 1; i <= nV; i++) {
      if (cnt[i] == -1) {
        remax(answ, dfs(i));
      }
    }
    std::cout << answ << std::endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using vi = std::vector<int>;
template <class X, class Y> X &remax(X &x, Y y) { return x = (y > x) ? y : x; }
const int NMAX = 100100;
int nV, nE;
vi next[NMAX];
int cnt[NMAX];
int dfs(int x) {
  if (cnt[x] != -1) {
    return cnt[x];
  }
  for (int y : next[x]) {
    remax(cnt[x], dfs(y) + 1);
  }
  remax(cnt[x], 0);
  return cnt[x];
}
int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(0);
  while (std::cin >> nV >> nE) {
    std::fill(cnt, cnt + NMAX, -1);
    std::fill(next, next + NMAX, vi{});
    for (int i = 0; i < nE; i++) {
      int x, y;
      std::cin >> x >> y;
      next[x].push_back(y);
    }
    int answ = 0;
    for (int i = 1; i <= nV; i++) {
      if (cnt[i] == -1) {
        remax(answ, dfs(i));
      }
    }
    std::cout << answ << std::endl;
  }
  return 0;
}
Problem p03166 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define vi vector<int>
#define all(x) (x).begin(), (x).end()
#define F first
#define S second
#define pb push_back
#define pp pair<int, int>
#define rep(i, l, r) for (int i = l; i < r; i++)
vi g[100005];
vi indeg(100005, 0);
vi lp(100005, 0);
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt.txt", "r", stdin);
  freopen("output.txt.txt", "w", stdout);
#endif
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  set<pp> vert;
  while (m--) {
    int u, v;
    cin >> u >> v;
    g[u].pb(v);
    indeg[v]++;
  }
  for (int i = 1; i <= n; i++) {
    vert.insert({indeg[i], i});
  }
  int done = 0;
  while (done != n) {

    pp vv = *vert.begin();
    int v = vv.second;
    for (auto &x : g[v]) {
      lp[x] = max(lp[v] + 1, lp[x]);
      vert.erase({indeg[x], x});
      indeg[x]--;
      vert.insert({indeg[x], x});
    }
    vert.erase({indeg[v], v});

    done++;
  }
  int ans = 0;
  rep(i, 1, n + 1) ans = max(ans, lp[i]);
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define vi vector<int>
#define all(x) (x).begin(), (x).end()
#define F first
#define S second
#define pb push_back
#define pp pair<int, int>
#define rep(i, l, r) for (int i = l; i < r; i++)
vi g[100005];
vi indeg(100005, 0);
vi lp(100005, 0);
int main() {
  // #ifndef ONLINE_JUDGE
  // freopen("input.txt.txt","r",stdin);
  // freopen("output.txt.txt","w",stdout);
  // #endif
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  set<pp> vert;
  while (m--) {
    int u, v;
    cin >> u >> v;
    g[u].pb(v);
    indeg[v]++;
  }
  for (int i = 1; i <= n; i++) {
    vert.insert({indeg[i], i});
  }
  int done = 0;
  while (done != n) {

    pp vv = *vert.begin();
    int v = vv.second;
    for (auto &x : g[v]) {
      lp[x] = max(lp[v] + 1, lp[x]);
      vert.erase({indeg[x], x});
      indeg[x]--;
      vert.insert({indeg[x], x});
    }
    vert.erase({indeg[v], v});

    done++;
  }
  int ans = 0;
  rep(i, 1, n + 1) ans = max(ans, lp[i]);
  cout << ans << endl;
}
Problem p03166 (C++)

Buggy submission (status: Runtime Error):
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
vector<int> g[10005];
int d[100005];
int dfs(int x) {
  if (d[x] > 0)
    return d[x];
  d[x] = 0;
  for (int y : g[x]) {
    d[x] = max(d[x], dfs(y) + 1);
  }
  return d[x];
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    g[--x].push_back(--y);
  }
  fill(d, d + n, -1);
  for (int i = 0; i < n; i++) {
    if (d[i] < 0) {
      d[i] = dfs(i);
    }
  }
  cout << *max_element(d, d + n) << endl;
  return 0;
}

Fixed submission:
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
vector<int> g[100005];
int d[100005];
int dfs(int x) {
  if (d[x] > 0)
    return d[x];
  d[x] = 0;
  for (int y : g[x]) {
    d[x] = max(d[x], dfs(y) + 1);
  }
  return d[x];
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    g[--x].push_back(--y);
  }
  fill(d, d + n, -1);
  for (int i = 0; i < n; i++) {
    if (d[i] < 0) {
      d[i] = dfs(i);
    }
  }
  cout << *max_element(d, d + n) << endl;
  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++)
#define all(x) (x).begin(), (x).end()
using ll = long long;
using namespace std;

int N, M;
vector<vector<int>> G;
vector<int> dp;

int f(int now) {
  if (dp[now] != 1e9)
    return dp[now];
  else if (G[now].size() == 0) {
    dp[now] = 0;
    return 0;
  } else {
    int mxcount = 0;
    for (auto e : G[now])
      mxcount = max(mxcount, f(e) + 1);
    dp[now] = max(dp[now], mxcount);
    return mxcount;
  }
}

int main() {
  cin >> N >> M;
  G.resize(N);
  dp.resize(N, 1e9);
  rep(i, M) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    G[x].push_back(y);
  }

  int ans = 0;
  rep(i, N) { ans = max(ans, f(i)); }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(x) (x).begin(), (x).end()
using ll = long long;
using namespace std;

int N, M;
vector<vector<int>> G;
vector<int> dp;

int f(int now) {
  if (dp[now] != 1e9)
    return dp[now];
  int mxcount = 0;
  for (auto e : G[now])
    mxcount = max(mxcount, f(e) + 1);
  dp[now] = mxcount;
  return mxcount;
}

int main() {
  cin >> N >> M;
  G.resize(N);
  dp.resize(N, 1e9);
  rep(i, M) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    G[x].push_back(y);
  }

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

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

typedef vector<int> vi;
typedef pair<int, int> pi;
typedef long long ll;
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define REP(i, a, b) for (int i = a; i <= b; i++)

vector<ll> dp;
vector<vector<ll>> s;

bitset<100010> can;

void see(ll here) {

  can[here] = 0;

  ll f = s[here].size();

  for (ll i = 0; i < f; i++) {

    if (!can[s[here][i]]) {
      see(s[here][i]);
    }

    dp[here] = max(dp[here], dp[s[here][i]] + 1);
  }
}

int main() {

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

  ll n, m, a, b;

  cin >> n >> m;

  dp.assign(n + 1, 0);
  s.assign(n + 1, vector<ll>(0, 0));

  for (ll i = 0; i < m; i++) {

    cin >> a >> b;
    s[a].PB(b);
  }

  for (ll i = 1; i <= n; i++) {
    if (!can[i])
      see(i);
  }

  ll maxi = 0;

  for (ll i = 1; i <= n; i++) {
    maxi = max(maxi, dp[i]);
  }

  cout << maxi << endl;

  return 0;
}

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

typedef vector<int> vi;
typedef pair<int, int> pi;
typedef long long ll;
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define REP(i, a, b) for (int i = a; i <= b; i++)

vector<ll> dp;
vector<vector<ll>> s;

bitset<100010> can;

void see(ll here) {

  can[here] = 1;

  ll f = s[here].size();

  for (ll i = 0; i < f; i++) {

    if (!can[s[here][i]]) {
      see(s[here][i]);
    }

    dp[here] = max(dp[here], dp[s[here][i]] + 1);
  }
}

int main() {

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

  ll n, m, a, b;

  cin >> n >> m;

  dp.assign(n + 1, 0);
  s.assign(n + 1, vector<ll>(0, 0));

  for (ll i = 0; i < m; i++) {

    cin >> a >> b;
    s[a].PB(b);
  }

  for (ll i = 1; i <= n; i++) {
    if (!can[i])
      see(i);
  }

  ll maxi = 0;

  for (ll i = 1; i <= n; i++) {
    maxi = max(maxi, dp[i]);
  }

  cout << maxi << endl;

  return 0;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define mod 1000000007
#define pb emplace_back
#define endl "\n"
#define fastio                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0)

lli ans;

lli dfs(lli v, vector<vector<lli>> edges, vector<lli> &dist,
        vector<bool> &visited) {
  visited[v] = true;
  lli val = 0;
  for (auto x : edges[v]) {
    if (visited[x]) {
      val = max(val, dist[x] + 1);
    } else {
      val = max(dfs(x, edges, dist, visited), val);
    }
  }
  dist[v] = val;
  ans = max(ans, dist[v]);
  return val + 1;
}

int main() {
  fastio;

  lli n, m;
  cin >> n >> m;
  vector<lli> dist(n + 1, -1);
  vector<vector<lli>> edges(n + 1);
  ans = 0;

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

  vector<bool> visited(n + 1, false);
  for (int i = 1; i <= n; i++) {
    if (!visited[i]) {
      // cout << i << " ";
      dfs(i, edges, dist, visited);
    }
  }
  // cout << endl;

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


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define mod 1000000007
#define pb emplace_back
#define endl "\n"
#define fastio                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0)

lli ans;

lli dfs(lli v, vector<vector<lli>> &edges, vector<lli> &dist,
        vector<bool> &visited) {
  visited[v] = true;
  lli val = 0;
  for (auto x : edges[v]) {
    if (visited[x]) {
      val = max(val, dist[x] + 1);
    } else {
      val = max(dfs(x, edges, dist, visited), val);
    }
  }
  dist[v] = val;
  ans = max(ans, dist[v]);
  return val + 1;
}

int main() {
  fastio;

  lli n, m;
  cin >> n >> m;
  vector<lli> dist(n + 1, -1);
  vector<vector<lli>> edges(n + 1);
  ans = 0;

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

  vector<bool> visited(n + 1, false);
  for (int i = 1; i <= n; i++) {
    if (!visited[i]) {
      // cout << i << " ";
      dfs(i, edges, dist, visited);
    }
  }
  // cout << endl;

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