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

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

void dfs(vector<vector<int>> edge, int start) {

  vis[start] = 1;
  for (auto b : edge[start]) {
    in_degree[b]--;
    dist[b] = max(dist[b], dist[start] + 1);
    if (in_degree[b] == 0)
      dfs(edge, b);
  }
}

int main() {

  int n, m;
  cin >> n >> m;
  vector<vector<int>> edge(n + 1);
  in_degree.resize(n + 1, 0);
  vis.resize(n + 1, 0);
  dist.resize(n + 1, 0);
  int aa, bb;
  for (int i = 0; i < m; i++) {
    cin >> aa >> bb;
    edge[aa].push_back(bb);
    in_degree[bb]++;
  }

  for (int i = 1; i <= n; i++) {
    if (vis[i] == 0 && in_degree[i] == 0) {
      dfs(edge, i);
    }
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    ans = max(ans, dist[i]);
  }
  cout << ans;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
vector<int> in_degree;
vector<int> vis;
vector<int> dist;

void dfs(vector<vector<int>> &edge, int start) {

  vis[start] = 1;
  for (auto b : edge[start]) {
    in_degree[b]--;
    dist[b] = max(dist[b], dist[start] + 1);
    if (in_degree[b] == 0)
      dfs(edge, b);
  }
}

int main() {

  int n, m;
  cin >> n >> m;
  vector<vector<int>> edge(n + 1);
  in_degree.resize(n + 1, 0);
  vis.resize(n + 1, 0);
  dist.resize(n + 1, 0);
  int aa, bb;
  for (int i = 0; i < m; i++) {
    cin >> aa >> bb;
    edge[aa].push_back(bb);
    in_degree[bb]++;
  }

  for (int i = 1; i <= n; i++) {
    if (vis[i] == 0 && in_degree[i] == 0) {
      dfs(edge, i);
    }
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    ans = max(ans, dist[i]);
  }
  cout << ans;

  return 0;
}
Problem p03166 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (ll i = ll(a); i < ll(b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define all(x) (x).begin(), (x).end()
#define PRINT(V) cout << V << "\n"
#define SORT(V) sort((V).begin(), (V).end())
#define RSORT(V) sort((V).rbegin(), (V).rend())
using namespace std;
using ll = long long int;
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;
}
const ll INF = 1e11;
const ll MOD = 1000000007;
typedef pair<ll, ll> P;
vector<vector<int>> to(10005);
int n, m;
int d[100005];
bool done[100005] = {0};
int len(int x) {
  int res = 0;
  if (done[x])
    return d[x];
  if (to[x].empty()) {
    d[x] = 0;
    done[x] = true;
    return 0;
  } else {
    for (int i : to[x]) {
      chmax(res, len(i) + 1);
    }
  }
  d[x] = res;
  done[x] = true;
  return res;
}
int main() {
  cin >> n >> m;
  int x, y;
  rep(i, m) {
    cin >> x >> y;
    to[x - 1].push_back(y - 1);
  }
  int ans = 0;
  rep(i, n) { chmax(ans, len(i)); }
  PRINT(ans);
}

Fixed submission:
#include <bits/stdc++.h>
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (ll i = ll(a); i < ll(b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define all(x) (x).begin(), (x).end()
#define PRINT(V) cout << V << "\n"
#define SORT(V) sort((V).begin(), (V).end())
#define RSORT(V) sort((V).rbegin(), (V).rend())
using namespace std;
using ll = long long int;
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;
}
const ll INF = 1e11;
const ll MOD = 1000000007;
typedef pair<ll, ll> P;
vector<vector<int>> to(100005);
int n, m;
int d[100005];
bool done[100005] = {0};
int len(int x) {
  int res = 0;
  if (done[x])
    return d[x];
  if (to[x].empty()) {
    d[x] = 0;
    done[x] = true;
    return 0;
  } else {
    for (int i : to[x]) {
      chmax(res, len(i) + 1);
    }
  }
  d[x] = res;
  done[x] = true;
  return res;
}
int main() {
  cin >> n >> m;
  int x, y;
  rep(i, m) {
    cin >> x >> y;
    to[x - 1].push_back(y - 1);
  }
  int ans = 0;
  rep(i, n) { chmax(ans, len(i)); }
  PRINT(ans);
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
const int nax =
    1e5 + 5; // this should be const for the below values to be initialised
vector<int> edges[nax];
int indegree[nax];
int dist[nax];
bool vis[nax];

void dfs(int i) {
  vis[i] = true;
  for (int j : edges[i]) {
    dist[j] = max(dist[j], dist[i] + 1);
    dfs(j);
  }
}
int main() {

  int n, m, a, b;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    edges[a].push_back(b);
    indegree[b]++;
  }
  for (int i = 1; i <= n; i++) {
    if (!vis[i] and indegree[i] == 0) {
      dfs(i);
    }
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    ans = max(ans, dist[i]);
  }
  cout << ans;
  return 0;
}
// Errichto's code


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int nax =
    1e5 + 5; // this should be const for the below values to be initialised
vector<int> edges[nax];
int indegree[nax];
int dist[nax];
bool vis[nax];

void dfs(int i) {
  vis[i] = true;
  for (int j : edges[i]) {
    dist[j] = max(dist[j], dist[i] + 1);
    indegree[j]--; // responsible for dp else tle
    if (indegree[j] == 0)
      dfs(j);
  }
}
int main() {

  int n, m, a, b;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    edges[a].push_back(b);
    indegree[b]++;
  }
  for (int i = 1; i <= n; i++) {
    if (!vis[i] and indegree[i] == 0) {
      dfs(i);
    }
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    ans = max(ans, dist[i]);
  }
  cout << ans;
  return 0;
}
// Errichto's code
Problem p03166 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("-O3")
const int maxn = 10e7;

vector<bool> visited(maxn, false);
vector<vector<int>> edges(maxn);
vector<long long int> dp(maxn);

void dfs(int u) {
  visited[u] = 1;
  for (int v : edges[u]) {
    if (!visited[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);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    edges[x].push_back(y);
  }
  for (int i = 1; i <= n; i++)
    edges[0].push_back(i);
  dfs(0);
  cout << dp[0] - 1 << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("-O3")
const int maxn = 1 + 10e5;

vector<bool> visited(maxn, false);
vector<vector<int>> edges(maxn);
vector<long long int> dp(maxn);

void dfs(int u) {
  visited[u] = 1;
  for (int v : edges[u]) {
    if (!visited[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);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    edges[x].push_back(y);
  }
  for (int i = 1; i <= n; i++)
    edges[0].push_back(i);
  dfs(0);
  cout << dp[0] - 1 << endl;
  return 0;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define DEBUG 0
#define nl '\n'
#if DEBUG
#define trace(var)                                                             \
  { cerr << "Line " << __LINE__ << ": " << #var << "=" << var << nl; }
#else
#define trace(var)                                                             \
  {}
#endif
#define fori(n) for (int i = 0; i < n; ++i)
#define forj(n) for (int j = 0; j < n; ++j)
#define fork(n) for (int k = 0; k < n; ++k)
#define forl(n) for (int l = 0; l < n; ++l)

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<bool> vbool;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef long double ld;
typedef vector<vector<int>> vvi;

const ll N = 10010;
const ll INF = 0x3f3f3f3f;
const ll MOD = 1e9 + 7;

int n, m;

void dfs(vvi &adj, vint &dist, vbool &seen, int u) {
  seen[u] = true;
  for (auto v : adj[u]) {
    dfs(adj, dist, seen, v);
    dist[u] = max(dist[u], 1 + dist[v]);
  }
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  int x, y;
  vvi adj(n + 1);
  fori(m) {
    cin >> x >> y;
    adj[x].push_back(y);
  }
  vbool seen(n + 1, false);
  vint dist(n + 1, 0);
  fori(n) if (!seen[i + 1]) dfs(adj, dist, seen, i + 1);

  int ans = 0;
  fori(n) ans = max(ans, dist[i + 1]);

  cout << ans << nl;
#if DEBUG
  cerr << "Time: " << (ld)clock() / CLOCKS_PER_SEC << " s" << nl;
#endif
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define DEBUG 0
#define nl '\n'
#if DEBUG
#define trace(var)                                                             \
  { cerr << "Line " << __LINE__ << ": " << #var << "=" << var << nl; }
#else
#define trace(var)                                                             \
  {}
#endif
#define fori(n) for (int i = 0; i < n; ++i)
#define forj(n) for (int j = 0; j < n; ++j)
#define fork(n) for (int k = 0; k < n; ++k)
#define forl(n) for (int l = 0; l < n; ++l)

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<bool> vbool;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef long double ld;
typedef vector<vector<int>> vvi;

const ll N = 10010;
const ll INF = 0x3f3f3f3f;
const ll MOD = 1e9 + 7;

int n, m;

void dfs(vvi &adj, vint &dist, vbool &seen, int u) {
  seen[u] = true;
  for (auto v : adj[u]) {
    if (!seen[v])
      dfs(adj, dist, seen, v);
    dist[u] = max(dist[u], 1 + dist[v]);
  }
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  int x, y;
  vvi adj(n + 1);
  fori(m) {
    cin >> x >> y;
    adj[x].push_back(y);
  }
  vbool seen(n + 1, false);
  vint dist(n + 1, 0);
  fori(n) if (!seen[i + 1]) dfs(adj, dist, seen, i + 1);

  int ans = 0;
  fori(n) ans = max(ans, dist[i + 1]);

  cout << ans << nl;
#if DEBUG
  cerr << "Time: " << (ld)clock() / CLOCKS_PER_SEC << " s" << nl;
#endif
  return 0;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
void pre() {}
long long int n, m, a, b;
vector<long long int> fov[100001];
long long int dp[100001]{0};
bool flag[100001]{0};
long long int dfs(long long int i) {
  if (flag[i]) {
    return dp[i];
  }
  if (fov[i].size() == 0) {
    dp[i] = -1;
    return dp[i];
  }
  dp[i] = 0;
  for (auto j : fov[i]) {
    dp[i] = max(dp[i], dfs(j) + 1);
  }
  return dp[i];
}
void solve() {
  cin >> n >> m;
  for (long long int i = 0; i < m; i++) {
    cin >> a >> b;
    fov[a].push_back(b);
  }
  for (long long int i = 1; i <= n; i++) {
    fov[0].push_back(i);
  }
  cout << dfs(0) << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  pre();
  long long int num = 1;
  // cin>>num;
  for (long long int i = 0; i < num; i++) {
    solve();
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
void pre() {}
long long int n, m, a, b;
vector<long long int> fov[100001];
long long int dp[100001]{0};
bool flag[100001]{0};
long long int dfs(long long int i) {
  if (flag[i]) {
    return dp[i];
  }
  if (fov[i].size() == 0) {
    dp[i] = -1;
    return dp[i];
  }
  flag[i] = 1;
  dp[i] = 0;
  for (auto j : fov[i]) {
    dp[i] = max(dp[i], dfs(j) + 1);
  }
  return dp[i];
}
void solve() {
  cin >> n >> m;
  for (long long int i = 0; i < m; i++) {
    cin >> a >> b;
    fov[a].push_back(b);
  }
  for (long long int i = 1; i <= n; i++) {
    fov[0].push_back(i);
  }
  cout << dfs(0) << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  pre();
  long long int num = 1;
  // cin>>num;
  for (long long int i = 0; i < num; i++) {
    solve();
  }
}
Problem p03166 (C++)

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

using namespace std;

typedef long long ll;

ll N, M;

vector<vector<ll>> to;

vector<ll> dp; // 初期値はマイナス1に設定

ll rec(ll i) {
  if (dp[i] != -1) {
    return dp[i];
  }

  ll res = 0; // まだrec(i)がもとまっていなかったら、今から求める
  for (auto a : to[i]) {
    res = max(res, rec(a) + 1);
  }

  return dp[i] = rec(i);
}

int main() {
  cin >> N >> M;
  to = vector<vector<ll>>(N);
  dp = vector<ll>(N, (ll)-1);
  for (ll i = 0; i < M; i++) {
    ll x, y;
    cin >> x >> y;
    x--;
    y--;
    to[x].push_back(y);
  } // 隣接リスト作成
  ll res = 0;
  for (ll i = 0; i < N; i++) {
    res = max(res, rec(i));
  }

  cout << res << endl;
  return 0;
}

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

using namespace std;

typedef long long ll;

ll N, M;

vector<vector<ll>> to;

vector<ll> dp; // 初期値はマイナス1に設定

ll rec(ll i) {
  if (dp[i] != -1) {
    return dp[i];
  }

  ll res = 0; // まだrec(i)がもとまっていなかったら、今から求める
  for (auto a : to[i]) {
    res = max(res, rec(a) + 1);
  }

  return dp[i] = res;
}

int main() {
  cin >> N >> M;
  to = vector<vector<ll>>(N);
  dp = vector<ll>(N, (ll)-1);
  for (ll i = 0; i < M; i++) {
    ll x, y;
    cin >> x >> y;
    x--;
    y--;
    to[x].push_back(y);
  } // 隣接リスト作成
  ll res = 0;
  for (ll i = 0; i < N; i++) {
    res = max(res, rec(i));
  }

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
int dp[1000];

void dfs(vector<int> adj[], int vis[], int node) {
  if (vis[node])
    return;
  vis[node] = 1;
  for (int i = 0; i < adj[node].size(); i++) {
    int x = adj[node][i];
    if (!vis[x])
      dfs(adj, vis, x);
    dp[node] = max(dp[node], dp[x] + 1);
  }
}
int main() {
  int n, m;
  cin >> n >> m;
  vector<int> adj[n + 1];
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    adj[x].push_back(y);
  }
  memset(dp, 0, sizeof(dp));
  int vis[n + 1];
  memset(vis, 0, sizeof(vis));
  for (int i = 1; i <= n; i++) {
    if (!vis[i])
      dfs(adj, vis, i);
  }
  int mx = 0;
  for (int i = 1; i <= n; i++)
    mx = max(dp[i], mx);
  cout << mx << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
int dp[1000000];

void dfs(vector<int> adj[], int vis[], int node) {
  if (vis[node])
    return;
  vis[node] = 1;
  for (int i = 0; i < adj[node].size(); i++) {
    int x = adj[node][i];
    if (!vis[x])
      dfs(adj, vis, x);
    dp[node] = max(dp[node], dp[x] + 1);
  }
}
int main() {
  int n, m;
  cin >> n >> m;
  vector<int> adj[n + 1];
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    adj[x].push_back(y);
  }
  memset(dp, 0, sizeof(dp));
  int vis[n + 1];
  memset(vis, 0, sizeof(vis));
  for (int i = 1; i <= n; i++) {
    if (!vis[i])
      dfs(adj, vis, i);
  }
  int mx = 0;
  for (int i = 1; i <= n; i++)
    mx = max(dp[i], mx);
  cout << mx << endl;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> to(100000);
vector<int> done;
vector<int> memo;
int dp(int i) {
  if (done.at(i)) {
    return memo.at(i);
  } else {
    int res = 0;
    for (int b : to.at(i)) {
      res = max(res, dp(b) + 1);
    }
    return res;
  }
}
/*i=0  3
1     2
2     0
3     1
 4    4

0(3 2)
1(2 3)
2(0)
3(2)
4(2 1 0)*/

int main() {
  // 入力は同じでいい
  int N, M;
  cin >> N >> M;
  done.resize(N);
  memo.resize(N); // これはdp(i)の値をmemo.at(i)に入れる。

  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    // 0番目からのスタートにしたいから。
    to.at(a).push_back(b);
    // これで木の入力は済んだはず
  }
  priority_queue<int> q;
  for (int i = 0; i < N; i++) {
    // cout <<dp(i)<<endl;//dpを出力させる
    q.push(dp(i)); // ここでdp(i)はi番目出発の最長経路を表す数値 or
                   // 戻り値でそれを返す関数。
  }
  cout << q.top() << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> to(100000);
vector<int> done;
vector<int> memo;
int dp(int i) {
  if (done.at(i)) {
    return memo.at(i);
  } else {
    int res = 0;
    for (int b : to.at(i)) {
      res = max(res, dp(b) + 1);
    }
    // 当たり前やんか、一回触ったらそれをdoneに書き込まないと
    done.at(i) = 1;
    memo.at(i) = res;
    return res;
  }
}
/*i=0  3
1     2
2     0
3     1
 4    4

0(3 2)
1(2 3)
2(0)
3(2)
4(2 1 0)*/

int main() {
  // 入力は同じでいい
  int N, M;
  cin >> N >> M;
  done.resize(N);
  memo.resize(N); // これはdp(i)の値をmemo.at(i)に入れる。

  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    // 0番目からのスタートにしたいから。
    to.at(a).push_back(b);
    // これで木の入力は済んだはず
  }
  priority_queue<int> q;
  for (int i = 0; i < N; i++) {
    // cout <<dp(i)<<endl;//dpを出力させる
    q.push(dp(i)); // ここでdp(i)はi番目出発の最長経路を表す数値 or
                   // 戻り値でそれを返す関数。
  }
  cout << q.top() << endl;
}
Problem p03166 (C++)

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

unordered_map<int, list<int>> l;
int dp[10001];

int lp(int src) {
  if (dp[src] != -1)
    return dp[src];
  bool leaf = 1;
  int ans = INT_MIN;
  for (auto it : l[src]) {
    leaf = 0;
    ans = max(ans, lp(it));
  }
  if (leaf == true)
    return dp[src] = 0;
  return dp[src] = ans + 1;
}

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

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

unordered_map<int, list<int>> l;
int dp[100001];

int lp(int src) {
  if (dp[src] != -1)
    return dp[src];
  bool leaf = 1;
  int ans = INT_MIN;
  for (auto it : l[src]) {
    leaf = 0;
    ans = max(ans, lp(it));
  }
  if (leaf == true)
    return dp[src] = 0;
  return dp[src] = ans + 1;
}

int main() {
  int n, m;
  cin >> n >> m;
  memset(dp, -1, sizeof(dp));
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    l[x].push_back(y);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++)
    ans = max(ans, lp(i));
  cout << ans;
}
Problem p03166 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define vi vector<int>
#define vb vector<bool>
#define lli long long int
#define vvi vector<vector<int>>
#define loop(n) for (int i = 0; i < n; i++)
using namespace std;

auto SpeedUp = []() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  std::ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  return 0;
}();

lli dfs(int src, vvi &graph, vector<lli> &dp) {
  if (dp[src] != 0)
    return dp[src];
  lli myans = 0;
  for (int e : graph[src])
    myans = max(myans, dfs(e, graph, dp) + 1);
  dp[src] = myans;
}

lli solve() {
  int n, m, a, b;
  cin >> n >> m;
  vvi graph(n + 1); // crating graph
  while (m--) {
    cin >> a >> b;
    graph[a].push_back(b);
  }

  vector<lli> dp(n + 1, 0);
  lli ans = 0;
  for (int i = 1; i <= n; i++) {
    ans = max(ans, dfs(i, graph, dp));
  }
  return ans;
}

int main() {
  cout << solve() << '\n';
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define vi vector<int>
#define vb vector<bool>
#define lli long long int
#define vvi vector<vector<int>>
#define loop(n) for (int i = 0; i < n; i++)
using namespace std;

auto SpeedUp = []() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  std::ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  return 0;
}();

lli dfs(int src, vvi &graph, vector<lli> &dp) {
  if (dp[src] != 0)
    return dp[src];
  lli myans = 0;
  for (int e : graph[src])
    myans = max(myans, dfs(e, graph, dp) + 1);
  return dp[src] = myans;
}

lli solve() {
  int n, m, a, b;
  cin >> n >> m;
  vvi graph(n + 1); // crating graph
  while (m--) {
    cin >> a >> b;
    graph[a].push_back(b);
  }

  vector<lli> dp(n + 1, 0);
  lli ans = 0;
  for (int i = 1; i <= n; i++) {
    ans = max(ans, dfs(i, graph, dp));
  }
  return ans;
}

int main() {
  cout << solve() << '\n';
  return 0;
}
Problem p03166 (C++)

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

bool used[101010];
int dp[101010];
std::deque<int> g[101010];

int dfs(int v) {
  if (used[v])
    return dp[v];

  int ret = 0;
  for (auto c : g[v]) {
    ret = std::max(ret, dfs(c) + 1);
  }

  return dp[v] = ret;
}

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

  int ret = 0;
  for (int v = 0; v < n; v++)
    ret = std::max(ret, dfs(v));
  std::cout << ret << std::endl;

  return 0;
}

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

bool used[101010];
int dp[101010];
std::deque<int> g[101010];

int dfs(int v) {
  if (used[v])
    return dp[v];

  used[v] = true;

  int ret = 0;
  for (auto c : g[v]) {
    ret = std::max(ret, dfs(c) + 1);
  }

  return dp[v] = ret;
}

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

  int ret = 0;
  for (int v = 0; v < n; v++)
    ret = std::max(ret, dfs(v));
  std::cout << ret << std::endl;

  return 0;
}
Problem p03166 (C++)

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

const int MOD = 1e9 + 7;
const int MAXN = 101010;

typedef struct def {
  int a, b, c;

  def() {}
  def(int a2, int b2, int c2) {
    a = a2;
    b = b2;
    c = c2;
  }
  bool operator<(def comp) const {
    if (a != comp.a)
      return a < comp.a;
    else
      return b < comp.b;
  }
} generico;

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

#define INF 0x3f3f3f3f
#define pb push_back
#define mp make_pair

int v, e;
int memo[MAXN];
vector<int> g[MAXN];

int dfs(int x) {
  if (memo[x] != -1)
    return memo[x];

  int ans = 0;

  for (int i = 0; i < g[x].size(); i++) {
    int y = g[x][i];
    ans = max(ans, dfs(y) + 1);
  }

  return ans;
}

int main() {

  memset(memo, -1, sizeof memo);

  scanf("%d %d", &v, &e);

  int a, b;
  for (int i = 0; i < e; i++) {
    scanf("%d %d", &a, &b);
    g[a].pb(b);
  }

  int ans = 0;

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

  printf("%d\n", ans);

  return 0;
}

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

const int MOD = 1e9 + 7;
const int MAXN = 101010;

typedef struct def {
  int a, b, c;

  def() {}
  def(int a2, int b2, int c2) {
    a = a2;
    b = b2;
    c = c2;
  }
  bool operator<(def comp) const {
    if (a != comp.a)
      return a < comp.a;
    else
      return b < comp.b;
  }
} generico;

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

#define INF 0x3f3f3f3f
#define pb push_back
#define mp make_pair

int v, e;
int memo[MAXN];
vector<int> g[MAXN];

int dfs(int x) {
  if (memo[x] != -1)
    return memo[x];

  int ans = 0;

  for (int i = 0; i < g[x].size(); i++) {
    int y = g[x][i];
    ans = max(ans, dfs(y) + 1);
  }

  return memo[x] = ans;
}

int main() {

  memset(memo, -1, sizeof memo);

  scanf("%d %d", &v, &e);

  int a, b;
  for (int i = 0; i < e; i++) {
    scanf("%d %d", &a, &b);
    g[a].pb(b);
  }

  int ans = 0;

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

  printf("%d\n", ans);

  return 0;
}
Problem p03166 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
int dp[3001];

class Graph {
  int v;
  list<int> *l;

public:
  Graph(int v) {
    this->v = v;
    l = new list<int>[v];
  }
  void addedge(int x, int y) { l[x].push_back(y); }
  void printgraph() {
    for (int i = 0; i < v; i++) {
      cout << i << " --- ";
      for (auto it : l[i]) {
        cout << it << ", ";
      }
      cout << endl;
    }
  }

  int longestpath(int src) {
    bool leaf = true;
    int bestchild = 0;
    if (dp[src] != -1) {
      return dp[src];
    }
    for (auto child : l[src]) {
      leaf = false;
      bestchild = max(bestchild, longestpath(child));
    }
    return dp[src] = leaf ? 0 : 1 + bestchild;
  }
};

signed main() {

  int n, e;
  cin >> n >> e;
  Graph g(n + 1);
  memset(dp, -1, sizeof dp);
  for (int i = 0; i < e; i++) {
    int x, y;
    cin >> x >> y;
    g.addedge(x, y);
  }
  // g.printgraph();
  int ans = 0;
  for (int i = 1; i <= n; i++)
    ans = max(ans, g.longestpath(i));
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
int dp[100001];

class Graph {
  int v;
  list<int> *l;

public:
  Graph(int v) {
    this->v = v;
    l = new list<int>[v];
  }
  void addedge(int x, int y) { l[x].push_back(y); }
  void printgraph() {
    for (int i = 0; i < v; i++) {
      cout << i << " --- ";
      for (auto it : l[i]) {
        cout << it << ", ";
      }
      cout << endl;
    }
  }

  int longestpath(int src) {
    bool leaf = true;
    int bestchild = 0;
    if (dp[src] != -1) {
      return dp[src];
    }
    for (auto child : l[src]) {
      leaf = false;
      bestchild = max(bestchild, longestpath(child));
    }
    return dp[src] = leaf ? 0 : 1 + bestchild;
  }
};

signed main() {

  int n, e;
  cin >> n >> e;
  Graph g(n + 1);
  memset(dp, -1, sizeof dp);
  for (int i = 0; i < e; i++) {
    int x, y;
    cin >> x >> y;
    g.addedge(x, y);
  }
  // g.printgraph();
  int ans = 0;
  for (int i = 1; i <= n; i++)
    ans = max(ans, g.longestpath(i));
  cout << ans << endl;

  return 0;
}
Problem p03166 (C++)

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

using namespace std;

const int maxn = 100001;

vector<int> G[maxn], tsort;

bool vis[maxn];
int dp[maxn];

void dfs(int u) {
  vis[u] = 1;
  for (int v : G[u])
    dfs(v);
  tsort.push_back(u);
}

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

  int V, E;
  cin >> V >> E;
  while (E--) {
    int u, v;
    cin >> u >> v;
    G[u].push_back(v);
  }

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

  int ans = 0;
  for (int u : tsort) {
    for (int v : G[u])
      dp[u] = max(dp[v] + 1, dp[u]);
    ans = max(ans, dp[u]);
  }

  cout << ans;

  return 0;
}


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

using namespace std;

const int maxn = 100001;

vector<int> G[maxn], tsort;

bool vis[maxn];
int dp[maxn];

void dfs(int u) {
  vis[u] = 1;
  for (int v : G[u])
    if (!vis[v])
      dfs(v);
  tsort.push_back(u);
}

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

  int V, E;
  cin >> V >> E;
  while (E--) {
    int u, v;
    cin >> u >> v;
    G[u].push_back(v);
  }

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

  int ans = 0;
  for (int u : tsort) {
    for (int v : G[u])
      dp[u] = max(dp[v] + 1, dp[u]);
    ans = max(ans, dp[u]);
  }

  cout << ans;

  return 0;
}
Problem p03166 (C++)

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

const int MAXN = 1e2 + 5;
const int MAXNN = 1e5 + 7;
const int INF = 1e9 + 7;
#define ll long long
#define pb push_back
#define endl '\n'
#define fast_io                                                                \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define F first
#define S second

ll poww(ll a, ll b, ll md) {
  return (!b ? 1
             : (b & 1 ? a * poww(a * a % md, b / 2, md) % md
                      : poww(a * a % md, b / 2, md) % md));
}

ll MOD(ll a) { return ((a % INF) + INF) % INF; }

ll inv(ll a) { return poww(a, INF - 2, INF); }

vector<int> adjList[MAXN];
int dp[MAXN], deg[MAXN];

int main() {
  fast_io;
  // cout << fixed << setprecision(15);

  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    int c, d;
    cin >> c >> d;
    adjList[c].pb(d);
    ++deg[d];
  }
  deque<int> dq;
  for (int i = 1; i <= n; ++i)
    if (!deg[i])
      dq.pb(i);

  while (!dq.empty()) {
    int now = dq.front();
    dq.pop_front();
    for (auto u : adjList[now]) {
      dp[u] = max(dp[u], dp[now] + 1);
      --deg[u];
    }
    for (auto u : adjList[now])
      if (deg[u] == 0)
        dq.pb(u);
  }
  int maxs = 0;
  for (int i = 1; i <= n; ++i)
    maxs = max(maxs, dp[i]);
  cout << maxs;
}


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

const int MAXN = 1e5 + 5;
const int MAXNN = 1e5 + 7;
const int INF = 1e9 + 7;
#define ll long long
#define pb push_back
#define endl '\n'
#define fast_io                                                                \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define F first
#define S second

ll poww(ll a, ll b, ll md) {
  return (!b ? 1
             : (b & 1 ? a * poww(a * a % md, b / 2, md) % md
                      : poww(a * a % md, b / 2, md) % md));
}

ll MOD(ll a) { return ((a % INF) + INF) % INF; }

ll inv(ll a) { return poww(a, INF - 2, INF); }

vector<int> adjList[MAXN];
int dp[MAXN], deg[MAXN];

int main() {
  fast_io;
  // cout << fixed << setprecision(15);

  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    int c, d;
    cin >> c >> d;
    adjList[c].pb(d);
    ++deg[d];
  }
  deque<int> dq;
  for (int i = 1; i <= n; ++i)
    if (!deg[i])
      dq.pb(i);

  while (!dq.empty()) {
    int now = dq.front();
    dq.pop_front();
    for (auto u : adjList[now]) {
      dp[u] = max(dp[u], dp[now] + 1);
      --deg[u];
    }
    for (auto u : adjList[now])
      if (deg[u] == 0)
        dq.pb(u);
  }
  int maxs = 0;
  for (int i = 1; i <= n; ++i)
    maxs = max(maxs, dp[i]);
  cout << maxs;
}
Problem p03166 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)
typedef long long ll;
#define test                                                                   \
  ll t;                                                                        \
  cin >> t;                                                                    \
  while (t--)
#define f first
#define s second
#define pb push_back
#define pop pop_back
#define int long long int

vector<int> graph[10000];
int dp[1000];

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

  bool leaf = 1;
  int bestchild = 0;

  for (int child : graph[src]) {
    leaf = 0;

    bestchild = max(bestchild, solve(child));
  }

  return dp[src] = leaf ? 0 : 1 + bestchild;
}

int32_t main() {
  fast;
  int n, m;
  cin >> n >> m;
  memset(dp, -1, sizeof dp);
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    graph[u].pb(v);
  }
  int ans = 0;

  for (int i = 1; i <= n; i++) {
    ans = max(ans, solve(i));
  }
  cout << ans << "\n";

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)
typedef long long ll;
#define test                                                                   \
  ll t;                                                                        \
  cin >> t;                                                                    \
  while (t--)
#define f first
#define s second
#define pb push_back
#define pop pop_back
#define int long long int

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

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

  bool leaf = 1;
  int bestchild = 0;

  for (int child : graph[src]) {
    leaf = 0;

    bestchild = max(bestchild, solve(child));
  }

  return dp[src] = leaf ? 0 : 1 + bestchild;
}

int32_t main() {
  fast;
  int n, m;
  cin >> n >> m;
  memset(dp, -1, sizeof dp);
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    graph[u].pb(v);
  }
  int ans = 0;

  for (int i = 1; i <= n; i++) {
    ans = max(ans, solve(i));
  }
  cout << ans << "\n";

  return 0;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define ll long long
#define endl "\n"
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template <typename T>
using ordered_set = tree<T, null_type, std::less<T>, rb_tree_tag,
                         tree_order_statistics_node_update>;
const int N = 1e5 + 10, inf = 1e9 + 10;
vector<int> g[N];
int d[N], ans, u[N];
void dfs(int v) {
  u[v] = 1;
  if (g[v].size() == 0)
    d[v] = 0;
  for (int i = 0; i < g[v].size(); i++) {
    int to = g[v][i];
    dfs(to);
    d[v] = max(d[v], d[to] + 1);
  }
}
int main() {
  // ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  // freopen("prices.in", "r", stdin);
  // freopen("prices.out", "w", stdout);
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int a, b;
    cin >> a >> b;
    g[a].pb(b);
  }
  for (int i = 1; i <= n; i++) {
    if (!u[i])
      dfs(i);
    ans = max(ans, d[i]);
  }
  cout << ans;
}


Fixed submission:
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define ll long long
#define endl "\n"
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template <typename T>
using ordered_set = tree<T, null_type, std::less<T>, rb_tree_tag,
                         tree_order_statistics_node_update>;
const int N = 1e5 + 10, inf = 1e9 + 10;
vector<int> g[N];
int d[N], ans, u[N];
void dfs(int v) {
  u[v] = 1;
  if (g[v].size() == 0)
    d[v] = 0;
  for (int i = 0; i < g[v].size(); i++) {
    int to = g[v][i];
    if (!u[to])
      dfs(to);
    d[v] = max(d[v], d[to] + 1);
  }
}
int main() {
  // ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  // freopen("prices.in", "r", stdin);
  // freopen("prices.out", "w", stdout);
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int a, b;
    cin >> a >> b;
    g[a].pb(b);
  }
  for (int i = 1; i <= n; i++) {
    if (!u[i])
      dfs(i);
    ans = max(ans, d[i]);
  }
  cout << ans;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr int INF = 1e9;
template <class T> inline void chmax(T &a, T b) {
  if (a < b)
    a = b;
}
template <class T> inline void chmin(T &a, T b) {
  if (a > b)
    a = b;
}

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

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  int N, M, x, y;
  cin >> N >> M;
  G.resize(N);

  for (int i = 0; i < M; ++i) {
    cin >> x >> y;
    --x;
    --y;
    G[x].push_back(y);
    ++deg[y];
  }
  queue<int> que;
  for (int i = 0; i < N; ++i)
    if (deg[i] == 0)
      que.push(i);
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    for (int nv : G[v]) {
      chmax(dp[nv], dp[v] + 1);
      que.push(nv);
    }
  }
  int ans = 0;
  for (int i = 0; i < N; ++i)
    chmax(ans, dp[i]);
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr int INF = 1e9;
template <class T> inline void chmax(T &a, T b) {
  if (a < b)
    a = b;
}
template <class T> inline void chmin(T &a, T b) {
  if (a > b)
    a = b;
}

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

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  int N, M, x, y;
  cin >> N >> M;
  G.resize(N);

  for (int i = 0; i < M; ++i) {
    cin >> x >> y;
    --x;
    --y;
    G[x].push_back(y);
    ++deg[y];
  }
  queue<int> que;
  for (int i = 0; i < N; ++i)
    if (deg[i] == 0)
      que.push(i);
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    for (int nv : G[v]) {
      chmax(dp[nv], dp[v] + 1);
      if (--deg[nv] == 0)
        que.push(nv);
    }
  }
  int ans = 0;
  for (int i = 0; i < N; ++i)
    chmax(ans, dp[i]);
  cout << ans << endl;
  return 0;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int memo[100010];
vector<int> v[100010];
int sol(int pos) {
  if (memo[pos])
    return memo[pos];
  int ma = 0;
  for (int i : v[pos])
    ma = max(ma, sol(i) + 1);
  return ma;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  int N, M;
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    v[a - 1].push_back(b - 1);
  }
  int ma = 0;
  for (int i = 0; i < N; i++)
    ma = max(ma, sol(i));
  cout << ma;
  return 0;
}

Fixed submission:
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int memo[100010];
vector<int> v[100010];
int sol(int pos) {
  if (memo[pos])
    return memo[pos];
  int ma = 0;
  for (int i : v[pos])
    ma = max(ma, sol(i) + 1);
  return memo[pos] = ma;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  int N, M;
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    v[a - 1].push_back(b - 1);
  }
  int ma = 0;
  for (int i = 0; i < N; i++)
    ma = max(ma, sol(i));
  cout << ma;
  return 0;
}
Problem p03166 (C++)

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

#define F first
#define S second
#define PB push_back

using namespace std;

typedef long long ll;

ll n, m;
vector<int> graph[100005];
int memo[100005];

int findpath(int node) {
  int maxlen = 0;
  for (auto x : graph[node]) {
    if (memo[x] != -1)
      maxlen = max(maxlen, 1 + memo[x]);
    else
      maxlen = max(maxlen, 1 + findpath(x));
  }
  return maxlen;
}

int main() {

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

  cin >> n >> m;
  while (m--) {
    int a, b;
    cin >> a >> b;
    graph[a].PB(b);
  }

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

  int mmax = -1;
  for (int i = 1; i <= n; i++) {
    if (memo[i] != -1) {
      mmax = max(mmax, memo[i]);
    } else {
      mmax = max(mmax, findpath(i));
    }
  }

  cout << mmax << "\n";

  return 0;
}

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

#define F first
#define S second
#define PB push_back

using namespace std;

typedef long long ll;

ll n, m;
vector<int> graph[100005];
int memo[100005];

int findpath(int node) {
  int maxlen = 0;
  for (auto x : graph[node]) {
    if (memo[x] != -1)
      maxlen = max(maxlen, 1 + memo[x]);
    else
      maxlen = max(maxlen, 1 + findpath(x));
  }
  return memo[node] = maxlen;
}

int main() {

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

  cin >> n >> m;
  while (m--) {
    int a, b;
    cin >> a >> b;
    graph[a].PB(b);
  }

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

  int mmax = -1;
  for (int i = 1; i <= n; i++) {
    if (memo[i] != -1) {
      mmax = max(mmax, memo[i]);
    } else {
      mmax = max(mmax, findpath(i));
    }
  }

  cout << mmax << "\n";

  return 0;
}
Problem p03166 (C++)

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

// 入力
int N, M;
int dp[100100];        // メモ化再帰
vector<vector<int>> G; // グラフ

int rec(int v) {
  if (dp[v] != 0)
    return dp[v]; // 既に更新済み

  int res = 0;
  for (auto nv : G[v]) {
    res = max(res, rec(nv) + 1);
  }
  return dp[v] = res; // メモしながらリターン
}

int main() {
  // 入力受け取り
  cin >> N >> M;
  G.resize(M);
  // G.assign(N, vector<int>());
  for (int i = 0; i < M; ++i) {
    int x, y;
    cin >> x >> y;
    --x, --y; // 0-indexed にする
    G[x].push_back(y);
  }

  // 全ノードを一通り更新しながら答えを求める
  int res = 0;
  for (int v = 0; v < N; ++v) {
    res = max(res, rec(v));
  }
  cout << res << endl;
}

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

// 入力
int N, M;
int dp[100100];        // メモ化再帰
vector<vector<int>> G; // グラフ

int rec(int v) {
  if (dp[v] != 0)
    return dp[v]; // 既に更新済み

  int res = 0;
  for (auto nv : G[v]) {
    res = max(res, rec(nv) + 1);
  }
  return dp[v] = res; // メモしながらリターン
}

int main() {
  // 入力受け取り
  cin >> N >> M;
  G.resize(N);
  // G.assign(N, vector<int>());
  for (int i = 0; i < M; ++i) {
    int x, y;
    cin >> x >> y;
    --x, --y; // 0-indexed にする
    G[x].push_back(y);
  }

  // 全ノードを一通り更新しながら答えを求める
  int res = 0;
  for (int v = 0; v < N; ++v) {
    res = max(res, rec(v));
  }
  cout << res << endl;
}
Problem p03166 (C++)

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

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> g(n);
  vector<int> deg(n, 0);
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    x--, y--;
    g[x].push_back(y);
    deg[y]++;
  }
  vector<int> dp(n, 0);
  queue<int> q;
  for (int i = 0; i < n; i++) {
    if (deg[i] == 0)
      q.push(i);
  }
  while (!q.empty()) {
    int s = q.front();
    q.pop();
    for (auto t : g[s]) {
      dp[t] = max(dp[t], dp[s] + 1);
      q.push(t);
    }
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    ans = max(ans, dp[i]);
  }
  cout << ans << endl;
}


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

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> g(n);
  vector<int> deg(n, 0);
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    x--, y--;
    g[x].push_back(y);
    deg[y]++;
  }
  vector<int> dp(n, 0);
  queue<int> q;
  for (int i = 0; i < n; i++) {
    if (deg[i] == 0)
      q.push(i);
  }
  while (!q.empty()) {
    int s = q.front();
    q.pop();
    for (auto t : g[s]) {
      dp[t] = max(dp[t], dp[s] + 1);
      deg[t]--;
      if (deg[t] == 0)
        q.push(t);
    }
  }
  int ans = 0;
  for (int i = 0; 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;

#define int long long

int dp[100001];

int solve(vector<vector<int>> v, int i) {
  if (dp[i] != -1)
    return dp[i];

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

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

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

  for (int i = 0; i < n; i++)
    dp[i] = -1;

  vector<vector<int>> v;
  for (int i = 0; i < n; i++) {
    vector<int> temp;
    v.push_back(temp);
  }
  for (int i = 0; i < m; i++) {
    int x;
    cin >> x;
    int y;
    cin >> y;
    x--;
    y--;
    v[x].push_back(y);
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (dp[i] == -1) {
      dp[i] = solve(v, i);
    }
    ans = max(ans, dp[i]);
  }
  cout << ans << endl;
}


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

#define int long long

int dp[100001];

int solve(vector<vector<int>> &v, int i) {
  if (dp[i] != -1)
    return dp[i];

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

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

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

  for (int i = 0; i < n; i++)
    dp[i] = -1;

  vector<vector<int>> v;
  for (int i = 0; i < n; i++) {
    vector<int> temp;
    v.push_back(temp);
  }
  for (int i = 0; i < m; i++) {
    int x;
    cin >> x;
    int y;
    cin >> y;
    x--;
    y--;
    v[x].push_back(y);
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (dp[i] == -1) {
      dp[i] = solve(v, 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;

int n, m, a, b;
int bio[100010];
vector<int> v[100010];

int dfs(int x) {
  for (int i = 0; i < v[x].size(); i++) {
    bio[x] = max(bio[x], dfs(v[x][i]) + 1);
  }

  return bio[x];
}

int main() {

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

  int maxi = 0;
  for (int i = 1; i <= n; i++) {
    if (bio[i] == 0) {
      dfs(i);
    }

    maxi = max(maxi, bio[i]);
  }

  cout << maxi;

  return 0;
}

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

int n, m, a, b;
int bio[100010];
vector<int> v[100010];

int dfs(int x) {
  for (int i = 0; i < v[x].size(); i++) {
    if (bio[v[x][i]] == 0)
      bio[x] = max(bio[x], dfs(v[x][i]) + 1);
    else {
      bio[x] = max(bio[x], bio[v[x][i]] + 1);
    }
  }

  return bio[x];
}

int main() {

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

  int maxi = 0;
  for (int i = 1; i <= n; i++) {
    if (bio[i] == 0) {
      dfs(i);
    }

    maxi = max(maxi, bio[i]);
  }

  cout << maxi;

  return 0;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
/*@author Vipen Loka*/
#include <bits/stdc++.h>
#define endl '\n'
#define ff first
#define ss second
#define ll long long int
#define vi vector<int>
#define vll vector<ll>
#define vvi vector<vi>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define mod 1000000007
#define inf 1000000000000000001;
#define deb(x) cout << #x << ':' << x << '\n';

using namespace std;
class Graph {
  int n;
  list<int> *graph;
  vector<int> dp;

public:
  int ans = 0;
  Graph(int n) {
    this->n = n;
    graph = new list<int>[n];
    dp.assign(n, 0);
  }
  void addEdge(int a, int b) {
    a--;
    b--;
    graph[a].push_back(b);
  }

  void dfs(int s, bool *&visited) {
    visited[s] = 1;

    for (auto &x : graph[s]) {
      // if(visited[x])continue;
      dfs(x, visited);
      dp[s] = max(dp[s], dp[x] + 1);
      ans = max(dp[s], ans);
    }
  }

  void dfs() {
    bool *visited = new bool[n];
    for (int i = 0; i < n; i++) {
      visited[i] = 0;
    }
    for (int i = 0; i < n; ++i) {
      if (visited[i])
        continue;
      dfs(i, visited);
    }
  }
};
void solve() {
  int i, j;
  int n, e;
  cin >> n >> e;
  Graph g(n);
  for (int i = 0; i < e; ++i) {
    int a, b;
    cin >> a >> b;
    g.addEdge(a, b);
  }

  g.dfs();

  printf("%d\n", g.ans);
}

int main() {
  int T = 1;
  // cin >> T;
  while (T--) {
    solve();
  }
}


Fixed submission:
/*@author Vipen Loka*/
#include <bits/stdc++.h>
#define endl '\n'
#define ff first
#define ss second
#define ll long long int
#define vi vector<int>
#define vll vector<ll>
#define vvi vector<vi>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define mod 1000000007
#define inf 1000000000000000001;
#define deb(x) cout << #x << ':' << x << '\n';

using namespace std;
class Graph {
  int n;
  list<int> *graph;
  vector<int> dp;

public:
  int ans = 0;
  Graph(int n) {
    this->n = n;
    graph = new list<int>[n];
    dp.assign(n, 0);
  }
  void addEdge(int a, int b) {
    a--;
    b--;
    graph[a].push_back(b);
  }

  void dfs(int s, bool *&visited) {
    visited[s] = 1;

    for (auto &x : graph[s]) {
      if (!visited[x])
        dfs(x, visited);
      // dfs(x,visited);
      dp[s] = max(dp[s], dp[x] + 1);
      ans = max(dp[s], ans);
    }
  }

  void dfs() {
    bool *visited = new bool[n];
    for (int i = 0; i < n; i++) {
      visited[i] = 0;
    }
    for (int i = 0; i < n; ++i) {
      if (visited[i])
        continue;
      dfs(i, visited);
    }
  }
};
void solve() {
  int i, j;
  int n, e;
  cin >> n >> e;
  Graph g(n);
  for (int i = 0; i < e; ++i) {
    int a, b;
    cin >> a >> b;
    g.addEdge(a, b);
  }

  g.dfs();

  printf("%d\n", g.ans);
}

int main() {
  int T = 1;
  // cin >> T;
  while (T--) {
    solve();
  }
}