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

Buggy submission (status: Time Limit Exceeded):

#include <bits/stdc++.h>
using namespace std;
typedef long long int lli;
typedef unsigned long long int ulli;
#define vec(s) vector
;
#define vvec(s) vector<vector
>;
typedef vector<lli> vi;
typedef vector<vi> vvi;
typedef pair<lli, lli> pii;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define YN(x) cout << (bool x ? "Yes" : "No") << endl;
#define out(s) cout << s << endl;
#define pb(s) push_back(s);
#define sp " ";
#define INF 10000000000
#define all(s) s.begin(), s.end()
void vout(vi v) {
  for (int i = 0; i < v.size(); i++)
    cout << v.at(i) << endl;
}
lli f(lli x, vvi edge, vi &dp) {
  if (dp[x] != -1) {
    return dp[x];
  }
  lli res = 0;
  if (edge[x].size() == 0)
    return dp[x] = res;
  rep(i, edge[x].size()) { res = max(res, f(edge[x][i], edge, dp)); }
  dp[x] = res + 1;
  return dp[x];
}
int main() {
  lli n, m, ans = 0;
  cin >> n >> m;
  vvi edge(n);
  vi dp(n, -1);
  vector<bool> s(n, true);
  rep(i, m) {
    lli x, y;
    cin >> x >> y;
    x--;
    y--;
    edge[x].push_back(y);
    s[y] = false;
  }
  rep(i, n) {
    if (!s[i])
      continue;
    else {
      ans = max(ans, f(i, edge, dp));
    }
  }
  out(ans);
}

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int lli;
typedef unsigned long long int ulli;
#define vec(s) vector
;
#define vvec(s) vector<vector
>;
typedef vector<lli> vi;
typedef vector<vi> vvi;
typedef pair<lli, lli> pii;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define YN(x) cout << (bool x ? "Yes" : "No") << endl;
#define out(s) cout << s << endl;
#define pb(s) push_back(s);
#define sp " ";
#define INF 10000000000
#define all(s) s.begin(), s.end()
void vout(vi v) {
  for (int i = 0; i < v.size(); i++)
    cout << v.at(i) << endl;
}
lli f(lli x, vvi &edge, vi &dp) {
  if (dp[x] != -1) {
    return dp[x];
  }
  lli res = 0;
  if (edge[x].size() == 0)
    return dp[x] = res;
  rep(i, edge[x].size()) { res = max(res, f(edge[x][i], edge, dp)); }
  dp[x] = res + 1;
  return dp[x];
}
int main() {
  lli n, m, ans = 0;
  cin >> n >> m;
  vvi edge(n);
  vi dp(n, -1);
  vector<bool> s(n, true);
  rep(i, m) {
    lli x, y;
    cin >> x >> y;
    x--;
    y--;
    edge[x].push_back(y);
    s[y] = false;
  }
  rep(i, n) {
    if (!s[i])
      continue;
    else {
      ans = max(ans, f(i, edge, dp));
    }
  }
  out(ans);
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define REP(i, m, n) for (int i = (m); i < (n); i++)
#define rep(i, n) REP(i, 0, n)
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int inf = 1e9 + 7;
vector<vector<int>> G(1000010);
int dp[100010];
int dfs(int n) {
  if (G[n].size()) {
    int ma = 0;
    for (auto e : G[n]) {
      ma = max(ma, dfs(e));
    }
    return dp[n] = ma + 1;
  } else {
    return dp[n] = 0;
  }
}
int main() {
  int n, m;
  cin >> n >> m;
  REP(i, 1, n + 1) dp[i] = -1;
  rep(i, m) {
    int x, y;
    cin >> x >> y;
    G[x].push_back(y);
  }
  REP(i, 1, n + 1) {
    if (dp[i] >= 0)
      continue;
    dfs(i);
  }
  int k = 0;
  REP(i, 1, n + 1) { k = max(k, dp[i]); }
  cout << k << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define REP(i, m, n) for (int i = (m); i < (n); i++)
#define rep(i, n) REP(i, 0, n)
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int inf = 1e9 + 7;
vector<vector<int>> G(1000010);
int dp[100010];
int dfs(int n) {
  if (G[n].size()) {
    int ma = 0;
    for (auto e : G[n]) {
      if (dp[e] >= 0)
        ma = max(ma, dp[e]);
      else
        ma = max(ma, dfs(e));
    }
    return dp[n] = ma + 1;
  } else {
    return dp[n] = 0;
  }
}
int main() {
  int n, m;
  cin >> n >> m;
  REP(i, 1, n + 1) dp[i] = -1;
  rep(i, m) {
    int x, y;
    cin >> x >> y;
    G[x].push_back(y);
  }
  REP(i, 1, n + 1) {
    if (dp[i] >= 0)
      continue;
    dfs(i);
  }
  int k = 0;
  REP(i, 1, n + 1) { k = max(k, dp[i]); }
  cout << k << endl;
}
Problem p03166 (C++)

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

int dfs(int node, vector<vector<int>> &adj, vector<bool> &vis,
        vector<int> &dist) {
  if (!vis[node]) {
    for (auto &x : adj[node]) {
      dist[node] = max(dist[node], dfs(x, adj, vis, dist) + 1);
    }
  }
  return dist[node];
}

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> dist(N + 1);
  vector<bool> vis(N + 1);
  vector<vector<int>> adj(N + 1);

  int sta, sto;
  for (int i = 0; i < M; i++) {
    cin >> sta >> sto;
    adj[sta].push_back(sto);
  }

  for (int i = 1; i <= N; i++) {
    if (!vis[i]) {
      dfs(i, adj, vis, dist);
    }
  }

  int ans = 0;

  for (auto &x : dist) {
    ans = max(ans, x);
  }
  cout << ans;
}

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

int dfs(int node, vector<vector<int>> &adj, vector<bool> &vis,
        vector<int> &dist) {
  if (!vis[node]) {
    vis[node] = 1;
    for (auto &x : adj[node]) {
      dist[node] = max(dist[node], dfs(x, adj, vis, dist) + 1);
    }
  }
  return dist[node];
}

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> dist(N + 1);
  vector<bool> vis(N + 1);
  vector<vector<int>> adj(N + 1);

  int sta, sto;
  for (int i = 0; i < M; i++) {
    cin >> sta >> sto;
    adj[sta].push_back(sto);
  }

  for (int i = 1; i <= N; i++) {
    if (!vis[i]) {
      dfs(i, adj, vis, dist);
    }
  }

  int ans = 0;

  for (auto &x : dist) {
    ans = max(ans, x);
  }
  cout << ans;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;

  int dp[n + 1];
  for (int i = 0; i <= n; i++) {
    dp[i] = 0;
  }
  vector<int> v[n + 1];
  int indegree[n + 1];
  for (int i = 0; i <= n; i++) {
    indegree[i] = 0;
  }

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

    int x, y;
    cin >> x >> y;
    v[x].push_back(y);
    indegree[y]++;
  }

  queue<int> q;
  for (int i = 1; i <= n; i++) {
    if (indegree[i] == 0) {
      q.push(i);
      dp[i] = 0;
    }
  }

  while (!q.empty()) {

    int k = q.size();
    while (k--) {

      int curr = q.front();
      q.pop();

      for (int i = 0; i < v[curr].size(); i++) {
        q.push(v[curr][i]);
        dp[v[curr][i]] = max(dp[v[curr][i]], dp[curr] + 1);
      }
    }
  }

  int ans = INT_MIN;
  for (int i = 0; i <= n; i++) {
    ans = max(ans, dp[i]);
  }

  cout << ans;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;

  int dp[n + 1];
  for (int i = 0; i <= n; i++) {
    dp[i] = 0;
  }
  vector<int> v[n + 1];
  int indegree[n + 1];
  for (int i = 0; i <= n; i++) {
    indegree[i] = 0;
  }

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

    int x, y;
    cin >> x >> y;
    v[x].push_back(y);
    indegree[y]++;
  }

  queue<int> q;
  for (int i = 1; i <= n; i++) {
    if (indegree[i] == 0) {
      q.push(i);
      dp[i] = 0;
    }
  }

  while (!q.empty()) {

    int k = q.size();
    while (k--) {

      int curr = q.front();
      q.pop();
      int s = v[curr].size();
      for (int i = 0; i < s; i++) {
        indegree[v[curr][i]]--;
        if (indegree[v[curr][i]] == 0) {
          q.push(v[curr][i]);
          dp[v[curr][i]] = max(dp[v[curr][i]], dp[curr] + 1);
        }
      }
    }
  }

  int ans = INT_MIN;
  for (int i = 0; i <= n; i++) {
    ans = max(ans, dp[i]);
  }

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

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

void dfs(int i, std::vector<std::vector<int>> graph, std::vector<int> &in,
         std::vector<long> &dp, std::vector<bool> &visited) {
  visited[i] = true;
  for (auto n : graph[i]) {
    dp[n] = std::max(dp[n], dp[i] + 1);
    --in[n];
    if (in[n] == 0) {
      dfs(n, graph, in, dp, visited);
    }
  }
}

int main() {
  int N, M;
  std::cin >> N >> M;
  std::vector<std::vector<int>> graph(N, std::vector<int>{});
  std::vector<int> in(N, 0);
  for (int i = 0; i < M; ++i) {
    int x, y;
    std::cin >> x >> y;
    graph[x - 1].push_back(y - 1);
    ++in[y - 1];
  }
  std::vector<bool> visited(N, false);
  std::vector<long> dp(N, 0);
  for (int i = 0; i < N; ++i) {
    if (!visited[i] && in[i] == 0) {
      dfs(i, graph, in, dp, visited);
    }
  }
  long max = 0;
  for (int i = 0; i < N; ++i) {
    max = std::max(max, dp[i]);
  }
  std::cout << max << "\n";
}

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

void dfs(int i, std::vector<std::vector<int>> &graph, std::vector<int> &in,
         std::vector<long> &dp, std::vector<bool> &visited) {
  visited[i] = true;
  for (auto n : graph[i]) {
    dp[n] = std::max(dp[n], dp[i] + 1);
    --in[n];
    if (in[n] == 0) {
      dfs(n, graph, in, dp, visited);
    }
  }
}

int main() {
  int N, M;
  std::cin >> N >> M;
  std::vector<std::vector<int>> graph(N, std::vector<int>{});
  std::vector<int> in(N, 0);
  for (int i = 0; i < M; ++i) {
    int x, y;
    std::cin >> x >> y;
    graph[x - 1].push_back(y - 1);
    ++in[y - 1];
  }
  std::vector<bool> visited(N, false);
  std::vector<long> dp(N, 0);
  for (int i = 0; i < N; ++i) {
    if (!visited[i] && in[i] == 0) {
      dfs(i, graph, in, dp, visited);
    }
  }
  long max = 0;
  for (int i = 0; i < N; ++i) {
    max = std::max(max, dp[i]);
  }
  std::cout << max << "\n";
}
Problem p03166 (C++)

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

using ll = long long;

const ll inf = 1e18;
const int N = 2 * 1e5 + 10;

ll res;
ll a[N], vis[N];

vector<ll> v[N];

ll Dfs(ll node) {
  for (auto i : v[node]) {
    if (!vis[i])
      Dfs(i);
    a[node] = max(a[node], 1 + a[i]);
  }
  return a[node];
}

void solve() {
  ll n, m, x, y;
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    cin >> x >> y;
    v[x].emplace_back(y);
  }
  for (int i = 1; i <= n; ++i) {
    if (!vis[i]) {
      res = max(res, Dfs(i));
    }
  }
  cout << res;
}

int main(int argc, char const *argv[]) {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  ll t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
}
// nEro

Fixed submission:
//                                      KiSmAt
#include "bits/stdc++.h"
using namespace std;

using ll = long long;

const ll inf = 1e18;
const int N = 2 * 1e5 + 10;

ll res;
ll a[N], vis[N];

vector<ll> v[N];

ll Dfs(ll node) {
  vis[node] = 1;
  for (auto i : v[node]) {
    if (!vis[i])
      Dfs(i);
    a[node] = max(a[node], 1 + a[i]);
  }
  return a[node];
}

void solve() {
  ll n, m, x, y;
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    cin >> x >> y;
    v[x].emplace_back(y);
  }
  for (int i = 1; i <= n; ++i) {
    if (!vis[i]) {
      res = max(res, Dfs(i));
    }
  }
  cout << res;
}

int main(int argc, char const *argv[]) {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  ll t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
}
// nEro
Problem p03166 (C++)

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

map<int, vector<int>> adj;
int n, m;
bool visited[10005];
int dp[10005];

void dfs(int n) {
  visited[n] = true;
  for (auto neigh : adj[n]) {
    if (!visited[neigh])
      dfs(neigh);
    dp[n] = max(dp[n], 1 + dp[neigh]);
  }
}

int solve(int n, int m, int *x, int *y) {
  for (int i = 0; i < m; i++) {
    adj[x[i]].push_back(y[i]);
  }
  for (int i = 1; i <= n; i++)
    if (!visited[i])
      dfs(i);
  int r = 0;
  for (int i = 1; i <= n; i++)
    r = max(r, dp[i]);
  return r;
}

int main() {
  cin >> n >> m;
  int x[m];
  int y[m];
  for (int i = 0; i < m; i++) {
    cin >> x[i] >> y[i];
  }
  fill(visited, visited + n, false);
  int r = solve(n, m, x, y);
  cout << r << endl;
  return 0;
}


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

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

void dfs(int n) {
  visited[n] = true;
  for (auto neigh : adj[n]) {
    if (!visited[neigh])
      dfs(neigh);
    dp[n] = max(dp[n], 1 + dp[neigh]);
  }
}

int solve(int n, int m, int *x, int *y) {
  for (int i = 0; i < m; i++) {
    adj[x[i]].push_back(y[i]);
  }
  for (int i = 1; i <= n; i++)
    if (!visited[i])
      dfs(i);
  int r = 0;
  for (int i = 1; i <= n; i++)
    r = max(r, dp[i]);
  return r;
}

int main() {
  cin >> n >> m;
  int x[m];
  int y[m];
  for (int i = 0; i < m; i++) {
    cin >> x[i] >> y[i];
  }
  fill(visited, visited + n, false);
  int r = solve(n, m, x, y);
  cout << r << endl;
  return 0;
}
Problem p03166 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <string.h>
#define ll long long
#include <vector>
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
using namespace std;
const int mod = 1e9 + 7;
int n, m;
vector<int> adj[100005];
int dp[100005];
int longpath(int node) {
  if (dp[node] != -1)
    return dp[node];
  int ans = 0;
  for (auto i : adj[node]) {
    ans = max(ans, 1 + longpath(node));
  }
  return dp[node] = ans;
}
int main() {
  IOS;

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

  cout << longpath(0) - 1;
}

Fixed submission:
#include <bits/stdc++.h>
#include <string.h>
#define ll long long
#include <vector>
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
using namespace std;
const int mod = 1e9 + 7;
int n, m;
vector<int> adj[100005];
int dp[100005];
int longpath(int node) {
  if (dp[node] != -1)
    return dp[node];
  int ans = 0;
  for (auto i : adj[node]) {
    ans = max(ans, 1 + longpath(i));
  }
  return dp[node] = ans;
}
int main() {
  IOS;

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

  cout << longpath(0) - 1;
}
Problem p03166 (C++)

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

using namespace std;

#define FaSt ios::sync_with_stdio(0), cin.tie(0);
#define random uniform_int_distribution<long long>(1, 2000000000000000LL)(rng)

const int N = 100005;
vector<vector<int>> g;
int n, m, dp[N], ans;
bool solved[N];

int dfs(int u) {
  if (solved[u]) {
    return dp[u];
  }

  for (auto v : g[u]) {
    dp[u] = max(dp[u], 1 + dfs(v));
  }
  return dp[u];
}

int main() {
  FaSt cin >> n >> m;
  g.resize(n);
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    g[x].push_back(y);
  }

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

  cout << ans;
}


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

using namespace std;

#define FaSt ios::sync_with_stdio(0), cin.tie(0);
#define random uniform_int_distribution<long long>(1, 2000000000000000LL)(rng)

const int N = 100005;
vector<vector<int>> g;
int n, m, dp[N], ans;
bool solved[N];

int dfs(int u) {
  if (solved[u]) {
    return dp[u];
  }

  for (auto v : g[u]) {
    dp[u] = max(dp[u], 1 + dfs(v));
  }
  solved[u] = 1;
  return dp[u];
}

int main() {
  FaSt cin >> n >> m;
  g.resize(n);
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    g[x].push_back(y);
  }

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

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

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

vector<int> adj[100005];
vector<int> memo(100005, -1);

int dfs(int u) {
  if (memo[u] != -1)
    return memo[u];
  int ans = 0;
  for (auto it : adj[u])
    ans = max(ans, 1 + dfs(it));
  return ans;
}

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  while (m--) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    adj[u].pb(v);
  }
  int ans = 0;
  for (int i = 0; i < n; i++)
    ans = max(ans, dfs(i));
  cout << ans;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define mod 1000000007

vector<int> adj[100005];
vector<int> memo(100005, -1);

int dfs(int u) {
  if (memo[u] != -1)
    return memo[u];
  int ans = 0;
  for (auto it : adj[u])
    ans = max(ans, 1 + dfs(it));
  return memo[u] = ans;
}

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  while (m--) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    adj[u].pb(v);
  }
  int ans = 0;
  for (int i = 0; i < n; i++)
    ans = max(ans, dfs(i));
  cout << ans;
  return 0;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int n, m, dp[100010];
vector<vector<int>> g;
int rec(int v) {
  if (dp[v] != -1) {
    return dp[v];
  } else {
    int ma = 0;
    for (auto i : g[v]) {
      ma = max(ma, rec(i) + 1);
    }
    return ma;
  }
}

int main() {
  int ans = 0;
  cin >> n >> m;
  g.assign(n, vector<int>());
  for (int j = 0; j < m; j++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    g[x].push_back(y); // xからyに行く辺
  }

  // 初期化
  for (int i = 0; i < n; i++) {
    dp[i] = -1;
  }

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

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int n, m, dp[100010];
vector<vector<int>> g;
int rec(int v) {
  if (dp[v] != -1) {
    return dp[v];
  } else {
    int ma = 0;
    for (auto i : g[v]) {
      ma = max(ma, rec(i) + 1);
    }
    return dp[v] = ma;
  }
}

int main() {
  int ans = 0;
  cin >> n >> m;
  g.assign(n, vector<int>());
  for (int j = 0; j < m; j++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    g[x].push_back(y); // xからyに行く辺
  }

  // 初期化
  for (int i = 0; i < n; i++) {
    dp[i] = -1;
  }

  for (int v = 0; v < n; v++) {
    ans = max(ans, rec(v));
  }
  cout << ans << endl;
}
Problem p03166 (C++)

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

#define FIN                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define ll long long
#define pb push_back
#define fore(i, a, b) for (int i = a; i < b; i++)
#define rfor(i, a, b) for (int i = a - 1; i >= b; i--)

ll n, m;
ll dp[100010];
bool visited[100010];
vector<int> g[100010];

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

int main() {
  cin >> n >> m;
  fore(i, 0, m) {
    int v, w;
    cin >> v >> w;
    g[v].push_back(w);
    visited[w] = true;
  }
  fore(i, 1, n + 1) dp[i] = -1;
  fore(i, 1, n + 1) {
    if (!visited[i])
      dfs(i);
  }
  ll ma = 0;
  // fore(i,1,n+1)cout<<dp[i]<<" ";
  // cout<<endl;
  fore(i, 1, n + 1) ma = max(ma, dp[i]);
  cout << ma << endl;
  return 0;
}

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

#define FIN                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define ll long long
#define pb push_back
#define fore(i, a, b) for (int i = a; i < b; i++)
#define rfor(i, a, b) for (int i = a - 1; i >= b; i--)

ll n, m;
ll dp[100010];
bool visited[100010];
vector<int> g[100010];

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

int main() {
  cin >> n >> m;
  fore(i, 0, m) {
    int v, w;
    cin >> v >> w;
    g[v].push_back(w);
    visited[w] = true;
  }
  fore(i, 1, n + 1) dp[i] = -1;
  fore(i, 1, n + 1) {
    if (!visited[i])
      dfs(i);
  }
  ll ma = 0;
  // fore(i,1,n+1)cout<<dp[i]<<" ";
  // cout<<endl;
  fore(i, 1, n + 1) ma = max(ma, dp[i]);
  cout << ma << endl;
  return 0;
}
Problem p03166 (C++)

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

using namespace std;
vector<int> adj[1000];
int dp[100005];
int n, m;

int lp(int n) {
  int &ret = dp[n];
  if (ret != -1)
    return ret;

  ret = 0;

  for (int i = 0; i < adj[n].size(); i++) {
    ret = max(ret, lp(adj[n][i]) + 1);
  }
  return ret;
}
int main() {
  cin >> n >> m;

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

  memset(dp, -1, sizeof dp);
  int mx = -1;

  for (int i = 1; i <= n; i++) {
    mx = max(mx, lp(i));
  }

  cout << mx << endl;
}


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

using namespace std;
vector<int> adj[100010];
int dp[100005];
int n, m;

int lp(int n) {
  int &ret = dp[n];
  if (ret != -1)
    return ret;

  ret = 0;

  for (int i = 0; i < adj[n].size(); i++) {
    ret = max(ret, lp(adj[n][i]) + 1);
  }
  return ret;
}
int main() {
  cin >> n >> m;

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

  memset(dp, -1, sizeof dp);
  int mx = -1;

  for (int i = 1; i <= n; i++) {
    mx = max(mx, lp(i));
  }

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

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

using namespace std;
typedef long long ll;

const int maxn = 3e3 + 10;
vector<int> g[maxn], topol;
bool visited[maxn];
int dp[maxn];

void dfs(int v) {
  visited[v] = 1;
  for (auto u : g[v])
    if (!visited[u])
      dfs(u);
  topol.push_back(v);
}

int main() {
  ios_base::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int v, u;
    cin >> v >> u;
    g[v].push_back(u);
  }
  for (int v = 1; v <= n; v++)
    if (!visited[v])
      dfs(v);
  for (auto v : topol) {
    for (auto u : g[v]) {
      dp[v] = max(dp[v], dp[u] + 1);
    }
  }
  cout << *max_element(dp + 1, dp + n + 1) << endl;
}


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

using namespace std;
typedef long long ll;

const int maxn = 1e5 + 10;
vector<int> g[maxn], topol;
bool visited[maxn];
int dp[maxn];

void dfs(int v) {
  visited[v] = 1;
  for (auto u : g[v])
    if (!visited[u])
      dfs(u);
  topol.push_back(v);
}

int main() {
  ios_base::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int v, u;
    cin >> v >> u;
    g[v].push_back(u);
  }
  for (int v = 1; v <= n; v++)
    if (!visited[v])
      dfs(v);
  for (auto v : topol) {
    for (auto u : g[v]) {
      dp[v] = max(dp[v], dp[u] + 1);
    }
  }
  cout << *max_element(dp + 1, dp + n + 1) << endl;
}
Problem p03166 (C++)

Buggy submission (status: Runtime Error):
#include "algorithm"
#include "bitset"
#include "climits"
#include "cmath"
#include "cstdio"
#include "functional"
#include "iomanip"
#include "iostream"
#include "list"
#include "map"
#include "numeric"
#include "queue"
#include "random"
#include "set"
#include "stack"
#include "string"
#include "unordered_map"
#include "unordered_set"

using namespace std;

const long long int MOD = 1000000007;

long long int N, M, K, H, W, L, R;

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

  cin >> N >> M;
  vector<vector<int>> edge(N);
  vector<set<int>> ie(N);
  vector<int> dp(N);
  for (int i = 0; i < M; i++) {
    cin >> L >> R;
    L--;
    R--;
    edge[L].push_back(R);
    ie[R].insert(L);
  }
  queue<int> Q;
  for (int i = 0; i < M; i++) {
    if (ie[i].empty()) {
      Q.push(i);
    }
  }
  while (!Q.empty()) {
    int cn = Q.front();
    Q.pop();
    for (auto i : edge[cn]) {
      dp[i] = max(dp[i], dp[cn] + 1);
      ie[i].erase(cn);
      if (ie[i].empty())
        Q.push(i);
    }
  }
  cout << *max_element(dp.begin(), dp.end()) << endl;
  return 0;
}

Fixed submission:
#include "algorithm"
#include "bitset"
#include "climits"
#include "cmath"
#include "cstdio"
#include "functional"
#include "iomanip"
#include "iostream"
#include "list"
#include "map"
#include "numeric"
#include "queue"
#include "random"
#include "set"
#include "stack"
#include "string"
#include "unordered_map"
#include "unordered_set"

using namespace std;

const long long int MOD = 1000000007;

long long int N, M, K, H, W, L, R;

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

  cin >> N >> M;
  vector<vector<int>> edge(N);
  vector<set<int>> ie(N);
  vector<int> dp(N);
  for (int i = 0; i < M; i++) {
    cin >> L >> R;
    L--;
    R--;
    edge[L].push_back(R);
    ie[R].insert(L);
  }
  queue<int> Q;
  for (int i = 0; i < N; i++) {
    if (ie[i].empty()) {
      Q.push(i);
    }
  }
  while (!Q.empty()) {
    int cn = Q.front();
    Q.pop();
    for (auto i : edge[cn]) {
      dp[i] = max(dp[i], dp[cn] + 1);
      ie[i].erase(cn);
      if (ie[i].empty())
        Q.push(i);
    }
  }
  cout << *max_element(dp.begin(), dp.end()) << endl;
  return 0;
}
Problem p03166 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<ll> g[11000];
ll dp[11000];
ll solve(ll u) {
  if (dp[u] != -1)
    return dp[u];
  ll ret = 0;
  for (ll i = 0; i < g[u].size(); i++) {
    ret = max(ret, solve(g[u][i]) + 1);
  }
  return dp[u] = ret;
}
int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n, m;
  cin >> n >> m;
  for (ll i = 0; i < m; i++) {
    ll a, b;
    cin >> a >> b;
    --a;
    --b;
    g[a].push_back(b);
  }
  for (ll i = 0; i < n; i++) {
    dp[i] = -1;
  }
  ll ret = 0;
  for (ll i = 0; i < n; i++) {
    ret = max(ret, solve(i));
  }
  cout << ret << '\n';

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<ll> g[110000];
ll dp[110000];
ll solve(ll u) {
  if (dp[u] != -1)
    return dp[u];
  ll ret = 0;
  for (ll i = 0; i < g[u].size(); i++) {
    ret = max(ret, solve(g[u][i]) + 1);
  }
  return dp[u] = ret;
}
int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n, m;
  cin >> n >> m;
  for (ll i = 0; i < m; i++) {
    ll a, b;
    cin >> a >> b;
    --a;
    --b;
    g[a].push_back(b);
  }
  for (ll i = 0; i < n; i++) {
    dp[i] = -1;
  }
  ll ret = 0;
  for (ll i = 0; i < n; i++) {
    ret = max(ret, solve(i));
  }
  cout << ret << '\n';

  return 0;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define mi map<int, int>
#define mii map<pii, int>
#define all(a) (a).begin(), (a).end()
#define ff first
#define ss second
#define sz(x) (int)x.size()
#define endl '\n'
#define hell 1000000007
#define rep(i, a, b) for (int i = a; i < b; i++)
using namespace std;

// Most important thing in CP - Have Fun :)
vi adj[100005];
int depth[100005];
int n, m;
int mx = 0;

int dfs(int u) {
  if (depth[u] != -1)
    return depth[u];
  int ans = 0;
  for (int v : adj[u]) {
    ans = max(ans, 1 + dfs(v));
  }
  mx = max(ans, mx);
  return ans;
}
void solve() {
  // write here
  memset(depth, -1, sizeof(depth));
  cin >> n >> m;
  rep(i, 0, m) {
    int u, v;
    cin >> u >> v;
    adj[u].pb(v);
  }
  for (int i = 1; i <= n; i++) {
    dfs(i);
  }
  cout << mx << endl;
}

int main() {
  cin.tie(NULL);
  ios::sync_with_stdio(false);
  // insert code

  solve();
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define mi map<int, int>
#define mii map<pii, int>
#define all(a) (a).begin(), (a).end()
#define ff first
#define ss second
#define sz(x) (int)x.size()
#define endl '\n'
#define hell 1000000007
#define rep(i, a, b) for (int i = a; i < b; i++)
using namespace std;

// Most important thing in CP - Have Fun :)
vi adj[100005];
int depth[100005];
int n, m;
int mx = 0;

int dfs(int u) {
  if (depth[u] != -1)
    return depth[u];
  int ans = 0;
  for (int v : adj[u]) {
    ans = max(ans, 1 + dfs(v));
  }
  mx = max(ans, mx);
  return depth[u] = ans;
}
void solve() {
  // write here
  memset(depth, -1, sizeof(depth));
  cin >> n >> m;
  rep(i, 0, m) {
    int u, v;
    cin >> u >> v;
    adj[u].pb(v);
  }
  for (int i = 1; i <= n; i++) {
    dfs(i);
  }
  cout << mx << endl;
}

int main() {
  cin.tie(NULL);
  ios::sync_with_stdio(false);
  // insert code

  solve();
  return 0;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <math.h>
#include <stdio.h>
using namespace std;
using vi = vector<int>;
using vll = vector<long long int>;
using vb = vector<bool>;
using vvi = vector<vector<int>>;
using vvll = vector<vector<long long int>>;
using ll = long long int;

ll dp[1000000];
vb ald(1000000, false);

ll mem(ll i, const vvll &e) {
  if (ald[i]) {
    return dp[i];
  }
  if (e[i].size() == 0) {
    return 0;
  }
  ll k = 0;
  for (auto x : e[i]) {
    dp[x] = mem(x, e);
    if (dp[x] > k) {
      k = dp[x];
    }
  }
  dp[i] = k + 1;
  return k + 1;
}

int main() {
  ll N, M;
  cin >> N >> M;
  vvll edge(N + 1, vll(0));
  for (int i = 0; i < M; i++) {
    ll a, b;
    cin >> a >> b;
    edge[a].emplace_back(b);
  }

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

  for (int i = 1; i < N + 1; i++) {
    dp[i] = mem(i, edge);
  }

  ll max = 0;

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

  cout << max;
}

Fixed submission:
#include <bits/stdc++.h>
#include <math.h>
#include <stdio.h>
using namespace std;
using vi = vector<int>;
using vll = vector<long long int>;
using vb = vector<bool>;
using vvi = vector<vector<int>>;
using vvll = vector<vector<long long int>>;
using ll = long long int;

ll dp[1000000];
vb ald(1000000, false);

ll mem(ll i, const vvll &e) {
  if (ald[i]) {
    return dp[i];
  }
  if (e[i].size() == 0) {
    return 0;
  }
  ll k = 0;
  for (auto x : e[i]) {
    dp[x] = mem(x, e);
    ald[x] = true;
    if (dp[x] > k) {
      k = dp[x];
    }
  }
  dp[i] = k + 1;
  return k + 1;
}

int main() {
  ll N, M;
  cin >> N >> M;
  vvll edge(N + 1, vll(0));
  for (int i = 0; i < M; i++) {
    ll a, b;
    cin >> a >> b;
    edge[a].emplace_back(b);
  }

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

  for (int i = 1; i < N + 1; i++) {
    dp[i] = mem(i, edge);
  }

  ll max = 0;

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

  cout << max;
}
Problem p03166 (C++)

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

typedef vector<vector<int>> Graph;

int dfs(int node, Graph &g, vector<int> &values) {
  int answer = 0;
  for (auto i : g[node]) {
    answer = max(answer, dfs(i, g, values) + 1);
  }
  return values[node] = answer;
}

int main() {
  int n, m, x, y;
  while (cin >> n >> m) {
    Graph G(n);
    vector<int> in(n);
    vector<int> answer(n, -1);

    for (int i = 0; i < m; ++i) {
      cin >> x >> y;
      x--;
      y--;
      G[x].push_back(y);
      in[y]++;
    }

    for (int i = 0; i < n; ++i) {
      if (in[i] == 0) {
        dfs(i, G, answer);
      }
    }
    cout << *max_element(answer.begin(), answer.end()) << endl;
  }
  return 0;
}

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

typedef vector<vector<int>> Graph;

int dfs(int node, Graph &g, vector<int> &values) {
  int answer = 0;
  if (values[node] != -1)
    return values[node];
  for (auto i : g[node]) {
    answer = max(answer, dfs(i, g, values) + 1);
  }
  return values[node] = answer;
}

int main() {
  int n, m, x, y;
  while (cin >> n >> m) {
    Graph G(n);
    vector<int> in(n);
    vector<int> answer(n, -1);

    for (int i = 0; i < m; ++i) {
      cin >> x >> y;
      x--;
      y--;
      G[x].push_back(y);
      in[y]++;
    }

    for (int i = 0; i < n; ++i) {
      if (in[i] == 0) {
        dfs(i, G, answer);
      }
    }
    cout << *max_element(answer.begin(), answer.end()) << endl;
  }
  return 0;
}
Problem p03166 (C++)

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

int vis[N];
int dp[N];

int dfs(int u, vector<vector<int>> &adj) {
  if (!vis[u]) {
    for (auto v : adj[u])
      dp[u] = max(dp[u], 1 + dfs(v, adj));
  }
  return dp[u];
}

int main() {

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

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

  cout << ans << endl;
}

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

int vis[N];
int dp[N];

int dfs(int u, vector<vector<int>> &adj) {
  if (!vis[u]) {
    vis[u] = 1;
    for (auto v : adj[u])
      dp[u] = max(dp[u], 1 + dfs(v, adj));
  }
  return dp[u];
}

int main() {

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

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

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

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

using namespace std;
typedef long long int ll;
const int maxv = 5 + 5;

vector<int> arr[maxv];
vector<int> indeg(maxv);
vector<int> vis(maxv);
vector<int> dis(maxv);

void dfs(int i) {
  vis[i] = 1;
  for (auto v : arr[i]) {
    --indeg[v];
    dis[v] = max(dis[v], 1 + dis[i]);
    if (indeg[v] == 0) {
      dfs(v);
    }
  }
}

int main() {

  int v, e;
  cin >> v >> e;

  int x, y;
  for (int i = 0; i < e; i++) {
    cin >> x >> y;
    arr[x].push_back(y);
    ++indeg[y];
  }

  for (int i = 1; i <= v; i++) {
    if (vis[i] == 0 && indeg[i] == 0) { // start with vertices with indegree 0
      dfs(i);
    }
  }
  int ans = 0;
  for (auto v : dis) {
    // cout<<v<<" ";
    ans = max(ans, v);
  }
  cout << ans << "\n";
}

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

using namespace std;
typedef long long int ll;
const int maxv = 1e5 + 5;

vector<int> arr[maxv];
vector<int> indeg(maxv);
vector<int> vis(maxv);
vector<int> dis(maxv);

void dfs(int i) {
  vis[i] = 1;
  for (auto v : arr[i]) {
    --indeg[v];
    dis[v] = max(dis[v], 1 + dis[i]);
    if (indeg[v] == 0) {
      dfs(v);
    }
  }
}

int main() {

  int v, e;
  cin >> v >> e;

  int x, y;
  for (int i = 0; i < e; i++) {
    cin >> x >> y;
    arr[x].push_back(y);
    ++indeg[y];
  }

  for (int i = 1; i <= v; i++) {
    if (vis[i] == 0 && indeg[i] == 0) { // start with vertices with indegree 0
      dfs(i);
    }
  }
  int ans = 0;
  for (auto v : dis) {
    // cout<<v<<" ";
    ans = max(ans, v);
  }
  cout << ans << "\n";
}
Problem p03166 (C++)

Buggy submission (status: Runtime Error):
/*
 * Created by Dipta Das on 29-06-2019
 * Title:
 * Problem Link:
 * Editorial:
 * Source Code:
 * Comments:
 */

#include <bits/stdc++.h>
#include <stdio.h>
#define fin freopen("input", "r", stdin)
#define whatis(x) cerr << #x << ": " << x << endl;

using namespace std;
using ll = long long;

vector<ll> adj[10005];
ll ans[100005];
ll mini;

ll dfs(ll curr) {
  ll &now = ans[curr];
  if (now != -1)
    return now;
  now = 0;
  for (int i = 0; i < (int)adj[curr].size(); ++i) {
    now = max(now, dfs(adj[curr][i]) + 1);
  }

  return now;
}

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

  int n, m;
  cin >> n >> m;
  for (int i = 0, x, y; i < m; ++i) {
    cin >> x >> y;
    adj[x].push_back(y);
  }

  memset(ans, -1, sizeof(ans));

  ll mn = 0;
  for (int i = 1; i <= n; ++i) {
    mn = max(mn, dfs(i));
  }

  cout << mn << endl;

  return 0;
}


Fixed submission:
/*
 * Created by Dipta Das on 29-06-2019
 * Title:
 * Problem Link:
 * Editorial:
 * Source Code:
 * Comments:
 */

#include <bits/stdc++.h>
#include <stdio.h>
#define fin freopen("input", "r", stdin)
#define whatis(x) cerr << #x << ": " << x << endl;

using namespace std;
using ll = long long;

vector<ll> adj[100005];
ll ans[100005];
ll mini;

ll dfs(ll curr) {
  ll &now = ans[curr];
  if (now != -1)
    return now;
  now = 0;
  for (int i = 0; i < (int)adj[curr].size(); ++i) {
    now = max(now, dfs(adj[curr][i]) + 1);
  }

  return now;
}

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

  int n, m;
  cin >> n >> m;
  for (int i = 0, x, y; i < m; ++i) {
    cin >> x >> y;
    adj[x].push_back(y);
  }

  memset(ans, -1, sizeof(ans));

  ll mn = 0;
  for (int i = 1; i <= n; ++i) {
    mn = max(mn, dfs(i));
  }

  cout << mn << endl;

  return 0;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int dfs(vector<vector<int>> &g, vector<bool> &visited, vector<int> &dp, int u) {
  visited[u] = true;
  for (int i = 0; i < g[u].size(); i++) {
    int v = g[u][i];
    dp[u] = max(dp[u], 1 + dfs(g, visited, dp, v));
  }
  return dp[u];
}
int main() {
  int n, m, x, y;
  cin >> n >> m;
  vector<vector<int>> g(n);
  for (int i = 0; i < m; i++) {
    cin >> x >> y;
    x--, y--;
    g[x].push_back(y);
  }
  vector<bool> isvisited(n, false);
  vector<int> dp(n, 0);
  for (int i = 0; i < n; i++) {
    if (!isvisited[i]) {
      dfs(g, isvisited, dp, i);
    }
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    ans = max(dp[i], ans);
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int dfs(vector<vector<int>> &g, vector<bool> &visited, vector<int> &dp, int u) {
  if (visited[u])
    return dp[u];
  visited[u] = true;
  for (int i = 0; i < g[u].size(); i++) {
    int v = g[u][i];
    dp[u] = max(dp[u], 1 + dfs(g, visited, dp, v));
  }
  return dp[u];
}
int main() {
  int n, m, x, y;
  cin >> n >> m;
  vector<vector<int>> g(n);
  for (int i = 0; i < m; i++) {
    cin >> x >> y;
    x--, y--;
    g[x].push_back(y);
  }
  vector<bool> isvisited(n, false);
  vector<int> dp(n, 0);
  for (int i = 0; i < n; i++) {
    if (!isvisited[i]) {
      dfs(g, isvisited, dp, i);
    }
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    ans = max(dp[i], ans);
  }
  cout << ans << endl;
  return 0;
}
Problem p03166 (C++)

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

int DFS(vector<vector<int>> Graph, vector<int> DP, int node) {
  if (DP[node] != 0) {
    return DP[node];
  }
  int result = 0;
  unsigned int i;
  for (i = 0; i < Graph[node].size(); i++) {
    result = max(result, 1 + DFS(Graph, DP, Graph[node][i]));
  }
  DP[node] = result;
  // cout<<result<<endl;
  return result;
}

int main() {
  int N, M;
  cin >> N >> M;
  int i;
  vector<vector<int>> Graph(N + 1);
  vector<int> DP(N + 1, 0);
  int x, y;
  for (i = 0; i < M; i++) {
    cin >> x >> y;
    Graph[x].push_back(y);
  }
  int result = 0;
  for (i = 1; i <= N; i++) {
    result = max(result, DFS(Graph, DP, i));
  }
  cout << result;
  return 0;
}


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

int DFS(vector<vector<int>> &Graph, vector<int> &DP, int node) {
  if (DP[node] != 0) {
    return DP[node];
  }
  int result = 0;
  unsigned int i;
  for (i = 0; i < Graph[node].size(); i++) {
    result = max(result, 1 + DFS(Graph, DP, Graph[node][i]));
  }
  DP[node] = result;
  // cout<<result<<endl;
  return result;
}

int main() {
  int N, M;
  cin >> N >> M;
  int i;
  vector<vector<int>> Graph(N + 1);
  vector<int> DP(N + 1, 0);
  int x, y;
  for (i = 0; i < M; i++) {
    cin >> x >> y;
    Graph[x].push_back(y);
  }
  int result = 0;
  for (i = 1; i <= N; i++) {
    result = max(result, DFS(Graph, DP, i));
  }
  cout << result;
  return 0;
}
Problem p03166 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
typedef long long lint;
using namespace std;
const int MAXN = 1e5 + 1;
const int INF = 1e9 + 7;
vector<int> dp;
vector<vector<int>> adj;
vector<int> ind;
vector<int> outd;
int N, M;
int f(int s) {
  int &ret = dp[s];
  if (ret != -1)
    return ret;
  if (outd[s] == 0)
    return ret = 0;
  ret = 1;
  for (int i : adj[s]) {
    ret = max(ret, f(i) + 1);
  }
  return ret;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> N >> M;
  dp.resize(N + 1, -1), ind.resize(N + 1, 0), outd.resize(N + 1, 0),
      adj.resize(N + 1, vector<int>(N + 1));
  for (int i = 0; i < M; ++i) {
    int u, v;
    cin >> u >> v;
    outd[u]++, ind[v]++, adj[u].push_back(v);
  }
  int ans = 0;
  for (int i = 1; i <= N; ++i) {
    if (ind[i])
      continue;
    ans = max(ans, f(i));
  }
  cout << ans << '\n';
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
typedef long long lint;
using namespace std;
const int MAXN = 1e5 + 1;
const int INF = 1e9 + 7;
vector<int> dp;
vector<vector<int>> adj;
vector<int> ind;
vector<int> outd;
int N, M;
int f(int s) {
  int &ret = dp[s];
  if (ret != -1)
    return ret;
  if (outd[s] == 0)
    return ret = 0;
  ret = 1;
  for (int i : adj[s]) {
    ret = max(ret, f(i) + 1);
  }
  return ret;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> N >> M;
  dp.resize(N + 1, -1), ind.resize(N + 1, 0), outd.resize(N + 1, 0),
      adj.resize(N + 1);
  for (int i = 0; i < M; ++i) {
    int u, v;
    cin >> u >> v;
    outd[u]++, ind[v]++, adj[u].push_back(v);
  }
  int ans = 0;
  for (int i = 1; i <= N; ++i) {
    if (ind[i])
      continue;
    ans = max(ans, f(i));
  }
  cout << ans << '\n';
  return 0;
}
Problem p03166 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
/* typedef */
typedef long long ll;
typedef pair<int, int> pii;
/* constant */
const ll LINF = 1LL << 50;
// const int MAX = 1e+5;
const int MAX = 6;
const int NIL = -1;
/* global variables */
int N, M;
vector<vector<int>> G(MAX, vector<int>());
vector<int> indeg(MAX, 0);
vector<int> dist(MAX, NIL);
/* function */
void input() {
  for (int i = 0; i < M; i++) {
    int s, t;
    cin >> s >> t;
    s--, t--;
    G[s].push_back(t);
    indeg[t]++;
  }
}
void bfs(int st) {
  queue<int> q;
  q.push(st);
  dist[st] = 0;
  while (q.size()) {
    int from = q.front();
    q.pop();
    for (int to : G[from]) {
      indeg[to]--;
      dist[to] = max(dist[to], dist[from] + 1);
      if (indeg[to] == 0) {
        q.push(to);
      }
    }
  }
}
void topologicalSort() {
  for (int i = 0; i < N; i++)
    if (indeg[i] == 0 && dist[i] == NIL)
      bfs(i);
}
void printAns() { cout << *max_element(dist.begin(), dist.end()) << '\n'; }
/* main */
int main() {
  cin >> N >> M;
  input();
  topologicalSort();
  printAns();
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
/* typedef */
typedef long long ll;
typedef pair<int, int> pii;
/* constant */
const ll LINF = 1LL << 50;
const int MAX = 1e+5;
const int NIL = -1;
/* global variables */
int N, M;
vector<vector<int>> G(MAX, vector<int>());
vector<int> indeg(MAX, 0);
vector<int> dist(MAX, NIL);
/* function */
void input() {
  for (int i = 0; i < M; i++) {
    int s, t;
    cin >> s >> t;
    s--, t--;
    G[s].push_back(t);
    indeg[t]++;
  }
}
void bfs(int st) {
  queue<int> q;
  q.push(st);
  dist[st] = 0;
  while (q.size()) {
    int from = q.front();
    q.pop();
    for (int to : G[from]) {
      indeg[to]--;
      dist[to] = max(dist[to], dist[from] + 1);
      if (indeg[to] == 0) {
        q.push(to);
      }
    }
  }
}
void topologicalSort() {
  for (int i = 0; i < N; i++)
    if (indeg[i] == 0 && dist[i] == NIL)
      bfs(i);
}
void printAns() { cout << *max_element(dist.begin(), dist.end()) << '\n'; }
/* main */
int main() {
  cin >> N >> M;
  input();
  topologicalSort();
  printAns();
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>

using namespace std;

using i64 = int64_t;
using P = pair<i64, i64>;
// i64 mod = 1000000007;
// int int_max = 2147483647;
i64 INF = 1e12;
int max_n = 1e5;

struct edge {
  edge() : link_to(0), cost(0) {}
  edge(int t, int c) : link_to(t), cost(c) {}
  int link_to;
  int cost;
};

vector<int> memo(100010, -1);

int rec(vector<vector<edge>> &graph, int v) {
  if (memo[v] != -1)
    return memo[v];
  int ret{0};

  for (auto e : graph[v]) {
    ret = max(ret, rec(graph, e.link_to) + e.cost);
  }
  return ret;
}

int main(int argc, char **argv) {
  i64 n, m;
  cin >> n >> m;

  vector<vector<edge>> graph(n);

  for (int i = 0; i < m; ++i) {
    int x, y;
    cin >> x >> y;
    --x;
    --y;
    graph[x].push_back(edge(y, 1));
  }

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

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>

using namespace std;

using i64 = int64_t;
using P = pair<i64, i64>;
// i64 mod = 1000000007;
// int int_max = 2147483647;
i64 INF = 1e12;
int max_n = 1e5;

struct edge {
  edge() : link_to(0), cost(0) {}
  edge(int t, int c) : link_to(t), cost(c) {}
  int link_to;
  int cost;
};

vector<int> memo(100010, -1);

int rec(vector<vector<edge>> &graph, int v) {
  if (memo[v] != -1)
    return memo[v];
  int ret{0};

  for (auto e : graph[v]) {
    ret = max(ret, rec(graph, e.link_to) + e.cost);
  }
  return memo[v] = ret;
}

int main(int argc, char **argv) {
  i64 n, m;
  cin >> n >> m;

  vector<vector<edge>> graph(n);

  for (int i = 0; i < m; ++i) {
    int x, y;
    cin >> x >> y;
    --x;
    --y;
    graph[x].push_back(edge(y, 1));
  }

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