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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define FORN(i, j, k) for (int i = j; i < k; i++)
#define FORR(i, j, k) for (int i = j; i >= k; i--)
#define REP(i, n) FORN(i, 0, n)
#define int long long
#define pii pair<int, int>
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define endl '\n'

const int MODO = 1e+9 + 7;
const int MAX = 1e+5;

int n, m;
int dp[MAX];
vector<int> adj[MAX];
vector<bool> visit(MAX, false);

void dfs(int node) {
  visit[node] = true;
  dp[node] = 0;
  for (int child : adj[node]) {
    if (!visit[child]) {
      dfs(child);
    }
    dp[node] = max(dp[node], dp[child] + 1);
  }
}

void solve() {
  int x, y;
  REP(i, m) {
    cin >> x >> y;
    adj[x].pb(y);
  }

  int res = 0;
  FORN(i, 1, n + 1) {
    if (!visit[i]) {
      dfs(i);
    }
  }

  FORN(i, 1, n + 1) { res = max(res, dp[i]); }

  cout << res << endl;
  return;
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> m;
  solve();
  return 0;
}


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

#define FORN(i, j, k) for (int i = j; i < k; i++)
#define FORR(i, j, k) for (int i = j; i >= k; i--)
#define REP(i, n) FORN(i, 0, n)
#define int long long
#define pii pair<int, int>
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define endl '\n'

const int MODO = 1e+9 + 7;
const int MAX = 1e+5 + 5;

int n, m;
int dp[MAX];
vector<int> adj[MAX];
vector<bool> visit(MAX, false);

void dfs(int node) {
  visit[node] = true;
  dp[node] = 0;
  for (int child : adj[node]) {
    if (!visit[child]) {
      dfs(child);
    }
    dp[node] = max(dp[node], dp[child] + 1);
  }
}

void solve() {
  int x, y;
  REP(i, m) {
    cin >> x >> y;
    adj[x].pb(y);
  }

  int res = 0;
  FORN(i, 1, n + 1) {
    if (!visit[i]) {
      dfs(i);
    }
  }

  FORN(i, 1, n + 1) { res = max(res, dp[i]); }

  cout << res << endl;
  return;
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> m;
  solve();
  return 0;
}
Problem p03166 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define MAX 10010
vector<int> g[MAX];
int main() {
  int n, m;
  cin >> n >> m;
  int in[MAX] = {0};
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    in[v]++;
    g[u].push_back(v);
  }
  queue<int> q;
  int d[MAX] = {0};
  for (int i = 0; i < n; i++)
    if (in[i] == 0)
      q.push(i);

  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (auto v : g[u]) {
      in[v]--;
      if (!in[v]) {
        d[v] = d[u] + 1;
        q.push(v);
      }
    }
  }
  int r = -1;
  for (int i = 0; i < n; i++)
    r = max(r, d[i]);
  cout << r << '\n';
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define MAX 100010
vector<int> g[MAX];
int main() {
  int n, m;
  cin >> n >> m;
  int in[MAX] = {0};
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    in[v]++;
    g[u].push_back(v);
  }
  queue<int> q;
  int d[MAX] = {0};
  for (int i = 0; i < n; i++)
    if (in[i] == 0)
      q.push(i);

  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (auto v : g[u]) {
      in[v]--;
      if (!in[v]) {
        d[v] = d[u] + 1;
        q.push(v);
      }
    }
  }
  int r = -1;
  for (int i = 0; i < n; i++)
    r = max(r, d[i]);
  cout << r << '\n';
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
// #define IOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define max(a, b) (a > b ? a : b)
#define min(a, b) (a < b ? a : b)
#define forn(i, o, n) for (int i = o; i < n; i++)
#define int long long
// #define endl "\n"
#define mod 1000000007
#define double long double
#define pb push_back
#define swap(x, y) (x ^= y ^= x ^= y)
using namespace std;
void dfs(vector<vector<int>> g, vector<int> &visit, vector<int> &dist, int i) {
  visit[i] = 1;
  for (auto j : g[i]) {
    if (!visit[j])
      dfs(g, visit, dist, j);

    dist[i] = max(dist[i], dist[j] + 1);
  }
}

int32_t main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int n, m;
  cin >> n >> m;
  vector<vector<int>> g(n);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].push_back(b);
  }

  std::vector<int> visit(n, 0);
  vector<int> dist(n, 0);
  for (int i = 0; i < n; i++) {
    if (!visit[i])
      dfs(g, visit, dist, i);
  }
  int ans = 0;
  for (auto j : dist)
    ans = max(ans, j);

  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
// #define IOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define max(a, b) (a > b ? a : b)
#define min(a, b) (a < b ? a : b)
#define forn(i, o, n) for (int i = o; i < n; i++)
#define int long long
// #define endl "\n"
#define mod 1000000007
#define double long double
#define pb push_back
#define swap(x, y) (x ^= y ^= x ^= y)
using namespace std;
void dfs(vector<vector<int>> &g, vector<int> &visit, vector<int> &dist, int i) {
  visit[i] = 1;
  for (auto j : g[i]) {
    if (!visit[j])
      dfs(g, visit, dist, j);

    dist[i] = max(dist[i], dist[j] + 1);
  }
}

int32_t main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int n, m;
  cin >> n >> m;
  vector<vector<int>> g(n);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].push_back(b);
  }

  std::vector<int> visit(n, 0);
  vector<int> dist(n, 0);
  for (int i = 0; i < n; i++) {
    if (!visit[i])
      dfs(g, visit, dist, i);
  }
  int ans = 0;
  for (auto j : dist)
    ans = max(ans, j);

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

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

using namespace std;

typedef long long ll;
unordered_map<int, vector<int>> edges;
vector<ll> dp(10002, 0);
bool visited[10002] = {false};

ll helper(int i) {
  if (visited[i])
    return dp[i]; // 访问过的节点，直接返回i节点的Longest Path
  else {
    dp[i] = 0;
    visited[i] = true; // 访问过当前这个节点
    auto next = edges[i];
    for (auto j : next) {
      dp[i] = max(dp[i], helper(j) + 1);
    }
    return dp[i];
  }
}

int main(int argc, char *argv[]) {
  int N, M;
  cin >> N >> M;
  int a, b;
  for (int i = 0; i < M; i++) {
    cin >> a >> b;
    edges[a].push_back(b);
  }
  ll res = 0;
  for (int i = 1; i <= N; i++) {
    res = max(res, helper(i));
  }
  cout << res;
  return 0;
}


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

using namespace std;

typedef long long ll;
unordered_map<int, vector<int>> edges;
vector<ll> dp(100002, 0);
bool visited[100002] = {false};

ll helper(int i) {
  if (visited[i])
    return dp[i]; // 访问过的节点，直接返回i节点的Longest Path
  else {
    dp[i] = 0;
    visited[i] = true; // 访问过当前这个节点
    auto next = edges[i];
    for (auto j : next) {
      dp[i] = max(dp[i], helper(j) + 1);
    }
    return dp[i];
  }
}

int main(int argc, char *argv[]) {
  int N, M;
  cin >> N >> M;
  int a, b;
  for (int i = 0; i < M; i++) {
    cin >> a >> b;
    edges[a].push_back(b);
  }
  ll res = 0;
  for (int i = 1; i <= N; i++) {
    res = max(res, helper(i));
  }
  cout << res;
  return 0;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll int
using namespace __gnu_pbds;
using namespace std;
#define mod 1000000007
#define endl "\n"
#define pb push_back
#define ff first
#define ss second
#define mp make_pair
#define lb lower_bound
#define f(i, p, n) for (ll i = p; i < n; i++)
#define debug(x) cout << "[" << #x << ": " << x << "]" << endl
typedef tree<ll, null_type, less<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

ll exp(ll a, ll b, ll m);

ll dp[100005] = {0};
vector<ll> tr[100005];

ll dfs(ll a) {
  if (dp[a] != 0)
    return dp[a];
  ll ma = 0;
  f(i, 0, tr[a].size()) { ma = max(ma, dfs(tr[a][i])); }
  return ma + 1;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  /*
#ifndef ONLINE_JUDGE
freopen("/home/savish/Desktop/2019/io/input.txt", "r", stdin);
freopen("/home/savish/Desktop/2019/io/output.txt", "w", stdout);
#endif
*/
  ll n, m;
  cin >> n >> m;
  f(i, 1, n + 1) dp[i] = 0;
  ll x, y;
  f(i, 0, m) {
    cin >> x >> y;
    tr[x].pb(y);
  }
  ll ans = 0;
  f(i, 1, n + 1) if (dp[i] == 0) ans = max(ans, dfs(i));
  cout << ans - 1 << endl;

  return 0;
}

ll exp(ll a, ll b, ll m) {
  if (b == 0) {
    return 1;
  }
  ll temp = exp(a, b / 2, m);
  temp = (temp * temp) % m;
  if (b & 1) {
    return (temp * (a % m)) % m;
  }
  return temp;
}


Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll int
using namespace __gnu_pbds;
using namespace std;
#define mod 1000000007
#define endl "\n"
#define pb push_back
#define ff first
#define ss second
#define mp make_pair
#define lb lower_bound
#define f(i, p, n) for (ll i = p; i < n; i++)
#define debug(x) cout << "[" << #x << ": " << x << "]" << endl
typedef tree<ll, null_type, less<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

ll exp(ll a, ll b, ll m);

ll dp[100005] = {0};
vector<ll> tr[100005];

ll dfs(ll a) {
  if (dp[a] != 0)
    return dp[a];
  ll ma = 0;
  f(i, 0, tr[a].size()) { ma = max(ma, dfs(tr[a][i])); }
  dp[a] = ma + 1;
  return ma + 1;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  /*
#ifndef ONLINE_JUDGE
freopen("/home/savish/Desktop/2019/io/input.txt", "r", stdin);
freopen("/home/savish/Desktop/2019/io/output.txt", "w", stdout);
#endif
*/
  ll n, m;
  cin >> n >> m;
  f(i, 1, n + 1) dp[i] = 0;
  ll x, y;
  f(i, 0, m) {
    cin >> x >> y;
    tr[x].pb(y);
  }
  ll ans = 0;
  f(i, 1, n + 1) if (dp[i] == 0) ans = max(ans, dfs(i));
  cout << ans - 1 << endl;

  return 0;
}

ll exp(ll a, ll b, ll m) {
  if (b == 0) {
    return 1;
  }
  ll temp = exp(a, b / 2, m);
  temp = (temp * temp) % m;
  if (b & 1) {
    return (temp * (a % m)) % m;
  }
  return temp;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
using namespace std;
vector<vector<int>> g;
vector<int> dp;
int dfs(int root) {
  for (int i = 0; i < g[root].size(); i++) {
    int u = g[root][i];
    int x = dfs(u);
    if (dp[u] == -1)
      dp[root] = max(dp[root], 1 + x);
    else
      dp[root] = max(dp[root], 1 + dp[u]);
  }
  if (dp[root] == -1)
    dp[root] = 0;
  return dp[root];
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m;
  cin >> n >> m;
  g.resize(n + 1);
  dp.resize(n + 1, -1);
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    g[x].pb(y);
  }
  for (int i = 1; i <= n; i++)
    g[0].pb(i);
  int ans = dfs(0);
  cout << ans - 1;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
using namespace std;
vector<vector<int>> g;
vector<int> dp;
int dfs(int root) {
  if (dp[root] != -1)
    return dp[root];
  for (int i = 0; i < g[root].size(); i++) {
    int u = g[root][i];
    int x = dfs(u);
    if (dp[u] == -1)
      dp[root] = max(dp[root], 1 + x);
    else
      dp[root] = max(dp[root], 1 + dp[u]);
  }
  if (dp[root] == -1)
    dp[root] = 0;
  return dp[root];
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m;
  cin >> n >> m;
  g.resize(n + 1);
  dp.resize(n + 1, -1);
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    g[x].pb(y);
  }
  for (int i = 1; i <= n; i++)
    g[0].pb(i);
  int ans = dfs(0);
  cout << ans - 1;

  return 0;
}
Problem p03166 (C++)

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

const int nax = 10010;
vector<int> adj[nax];
int in_degree[nax], dist[nax];
bool vis[nax];

void dfs(int a) {
  vis[a] = true;
  for (int x : adj[a]) {
    dist[x] = max(dist[x], dist[a] + 1);
    --in_degree[x];
    if (in_degree[x] == 0)
      dfs(x);
  }
}

int main() {
  int n, m, x, y;
  cin >> n >> m;
  while (m--) {
    cin >> x >> y;
    adj[x].push_back(y);
    ++in_degree[y];
  }
  for (int i = 1; i <= n; i++) {
    if (!vis[i] && in_degree[i] == 0)
      dfs(i);
  }
  int answer = 0;
  for (int i = 1; i <= n; i++) {
    answer = max(answer, dist[i]);
  }
  cout << answer;
}


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

const int nax = 1e5 + 5;
vector<int> adj[nax];
int in_degree[nax], dist[nax];
bool vis[nax];

void dfs(int a) {
  vis[a] = true;
  for (int x : adj[a]) {
    dist[x] = max(dist[x], dist[a] + 1);
    --in_degree[x];
    if (in_degree[x] == 0)
      dfs(x);
  }
}

int main() {
  int n, m, x, y;
  cin >> n >> m;
  while (m--) {
    cin >> x >> y;
    adj[x].push_back(y);
    ++in_degree[y];
  }
  for (int i = 1; i <= n; i++) {
    if (!vis[i] && in_degree[i] == 0)
      dfs(i);
  }
  int answer = 0;
  for (int i = 1; i <= n; i++) {
    answer = max(answer, dist[i]);
  }
  cout << answer;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define endl '\n'
#define N 1000001
#define mod 1e9 + 7
using namespace std;
typedef long long ll;
vector<vector<ll>> v(100001);
ll dp[100001];
ll sol(ll a) {
  if (dp[a] != -1)
    return dp[a];
  ll res = 0;
  for (ll i = 0; i < v[a].size(); i++) {
    res = max(res, sol(v[a][i]) + 1);
  }
  return res;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll n, m;
  cin >> n >> m;
  ll x, y;
  for (ll i = 0; i < m; i++) {
    cin >> x >> y;
    v[x].push_back(y);
  }
  ll ans = 0;
  for (ll i = 1; i <= n; i++)
    dp[i] = -1;
  for (ll i = 1; i <= n; i++) {
    ans = max(ans, sol(i));
  }
  cout << ans;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define endl '\n'
#define N 1000001
#define mod 1e9 + 7
using namespace std;
typedef long long ll;
vector<vector<ll>> v(100001);
ll dp[100001];
ll sol(ll a) {
  if (dp[a] != -1)
    return dp[a];
  ll res = 0;
  for (ll i = 0; i < v[a].size(); i++) {
    res = max(res, sol(v[a][i]) + 1);
  }
  return dp[a] = res;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll n, m;
  cin >> n >> m;
  ll x, y;
  for (ll i = 0; i < m; i++) {
    cin >> x >> y;
    v[x].push_back(y);
  }
  ll ans = 0;
  for (ll i = 1; i <= n; i++)
    dp[i] = -1;
  for (ll i = 1; i <= n; i++) {
    ans = max(ans, sol(i));
  }
  cout << ans;
  return 0;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <vector>
using namespace std;
vector<vector<int>> adj;
vector<int> ans, visited;

void dfs(int cur) {
  int amax = -1;
  visited[cur] = 1;
  for (int i : adj[cur]) {
    dfs(i);
    if (amax < ans[i])
      amax = ans[i];
  }
  ans[cur] = amax + 1;
}

int main() {
  int n, m, x, y;
  cin >> n >> m;
  adj.resize(n + 1);
  ans.resize(n + 1);
  visited.resize(n + 1, 0);

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

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

Fixed submission:
#include <iostream>
#include <vector>
using namespace std;
vector<vector<int>> adj;
vector<int> ans, visited;

void dfs(int cur) {
  int amax = -1;
  visited[cur] = 1;
  for (int i : adj[cur]) {
    if (!visited[i])
      dfs(i);
    if (amax < ans[i])
      amax = ans[i];
  }
  ans[cur] = amax + 1;
}

int main() {
  int n, m, x, y;
  cin >> n >> m;
  adj.resize(n + 1);
  ans.resize(n + 1);
  visited.resize(n + 1, 0);

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

  int amax = 0;
  for (int i = 1; i <= n; i++) {
    if (!visited[i])
      dfs(i);
    if (amax < ans[i])
      amax = ans[i];
  }
  cout << amax;
}
Problem p03166 (C++)

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

using namespace std;

const int M = 1e3 + 3;
int n, k, vis[M], ans, dp[M];
vector<int> adj[M], g[M], lf;

int dfs(int u) {
  int &ret = dp[u];
  if (ret != -1)
    return ret;
  ret = 1;
  for (int i : adj[u]) {
    ret = max(ret, dfs(i) + 1);
  }
  return ret;
}

int main() {
  scanf("%d %d", &n, &k);
  while (k--) {
    int x, y;
    cin >> x >> y;
    adj[x].push_back(y);
  }
  memset(dp, -1, sizeof dp);
  for (int i = 1; i <= n; i++) {
    ans = max(ans, dfs(i));
  }
  cout << ans - 1;
}


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

using namespace std;

const int M = 1e5 + 5;
int n, k, vis[M], ans, dp[M];
vector<int> adj[M], g[M], lf;

int dfs(int u) {
  int &ret = dp[u];
  if (ret != -1)
    return ret;
  ret = 1;
  for (int i : adj[u]) {
    ret = max(ret, dfs(i) + 1);
  }
  return ret;
}

int main() {
  scanf("%d %d", &n, &k);
  while (k--) {
    int x, y;
    cin >> x >> y;
    adj[x].push_back(y);
  }
  memset(dp, -1, sizeof dp);
  for (int i = 1; i <= n; i++) {
    ans = max(ans, dfs(i));
  }
  cout << ans - 1;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
int const MAXN = 100100;
int n, m, dp[MAXN];
vector<int> g[MAXN];
int dfs(int node) {
  if (dp[node] != -1)
    return dp[node];
  int best = 0;
  for (int x : g[node])
    best = max(best, 1 + dfs(x));
  return best;
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    g[a].push_back(b);
  }
  memset(dp, -1, sizeof(dp));
  int ans = 0;
  for (int i = 1; i <= n; i++)
    if (dp[i] == -1)
      ans = max(ans, dfs(i));
  cout << ans;
}


Fixed submission:
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
int const MAXN = 100100;
int n, m, dp[MAXN];
vector<int> g[MAXN];
int dfs(int node) {
  if (dp[node] != -1)
    return dp[node];
  int best = 0;
  for (int x : g[node])
    best = max(best, 1 + dfs(x));
  return dp[node] = best;
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    g[a].push_back(b);
  }
  memset(dp, -1, sizeof(dp));
  int ans = 0;
  for (int i = 1; i <= n; i++)
    if (dp[i] == -1)
      ans = max(ans, dfs(i));
  cout << ans;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ALL(a) (a).begin(), (a).end()
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<long long, long long> Pll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<long long> vll;
typedef vector<vector<long long>> vvll;
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const long long INF = 1LL << 60;

vvi G(100100, vi(0));
vi dp(100100, -1);

int func(int v) {
  if (dp.at(v) != -1)
    return dp.at(v);

  int res = 0;
  for (auto x : G.at(v)) {
    chmax(res, func(x) + 1);
  }
  return res;
}

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

  int result = 0;
  for (int i = 0; i < N; i++) {
    chmax(result, func(i));
  }
  cout << result << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define ALL(a) (a).begin(), (a).end()
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<long long, long long> Pll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<long long> vll;
typedef vector<vector<long long>> vvll;
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const long long INF = 1LL << 60;

vvi G(100100, vi(0));
vi dp(100100, -1);

int func(int v) {
  if (dp.at(v) != -1)
    return dp.at(v);

  int res = 0;
  for (auto x : G.at(v)) {
    chmax(res, func(x) + 1);
  }
  return dp.at(v) = res;
}

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

  int result = 0;
  for (int i = 0; i < N; i++) {
    chmax(result, func(i));
  }
  cout << result << endl;
}
Problem p03166 (C++)

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

using namespace std;
const int maxn = 3e3 + 5;
int n, m;
vector<int> a[maxn];
int f[maxn];

int dp(int u) {
  if (f[u] != -1)
    return f[u];
  f[u] = 0;
  for (int v : a[u])
    f[u] = max(f[u], dp(v) + 1);
  return f[u];
}

int main() {
#ifdef NTMA
  freopen("abc.inp", "r", stdin);
#endif
  memset(f, 255, sizeof(f));
  cin >> n >> m;
  while (m--) {
    int u, v;
    cin >> u >> v;
    a[u].push_back(v);
  }
  int ans = 0;
  for (int i = 1; i <= n; ++i)
    ans = max(ans, dp(i));
  cout << ans;
  return 0;
}

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

using namespace std;
const int maxn = 1e5 + 5;
int n, m;
vector<int> a[maxn];
int f[maxn];

int dp(int u) {
  if (f[u] != -1)
    return f[u];
  f[u] = 0;
  for (int v : a[u])
    f[u] = max(f[u], dp(v) + 1);
  return f[u];
}

int main() {
#ifdef NTMA
  freopen("abc.inp", "r", stdin);
#endif
  memset(f, 255, sizeof(f));
  cin >> n >> m;
  while (m--) {
    int u, v;
    cin >> u >> v;
    a[u].push_back(v);
  }
  int ans = 0;
  for (int i = 1; i <= n; ++i)
    ans = max(ans, dp(i));
  cout << ans;
  return 0;
}
Problem p03166 (C++)

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

const int INF = 0x3f3f3f3f;
const int MAXN = 112345;

int n, m;
vector<int> adj[MAXN];
int pd[MAXN];

int rec(int cur) {
  if (pd[cur] != -1)
    return pd[cur];

  int rtn = 0;
  for (int i = 0; i < adj[cur].size(); i++) {
    rtn = max(1 + rec(adj[cur][i]), rtn);
  }

  return rtn;
}

int main() {
  memset(pd, -1, sizeof(pd));

  cin >> n >> m;

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

  int resp = 0;
  for (int i = 1; i <= n; i++)
    resp = max(resp, rec(i));
  cout << resp << endl;

  return 0;
}

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

const int INF = 0x3f3f3f3f;
const int MAXN = 112345;

int n, m;
vector<int> adj[MAXN];
int pd[MAXN];

int rec(int cur) {
  if (pd[cur] != -1)
    return pd[cur];

  int rtn = 0;
  for (int i = 0; i < adj[cur].size(); i++) {
    rtn = max(1 + rec(adj[cur][i]), rtn);
  }

  return pd[cur] = rtn;
}

int main() {
  memset(pd, -1, sizeof(pd));

  cin >> n >> m;

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

  int resp = 0;
  for (int i = 1; i <= n; i++)
    resp = max(resp, rec(i));
  cout << resp << endl;

  return 0;
}
Problem p03166 (C++)

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

using namespace std;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

#define hash sodnf9843bsd
#define ll long long
#define INF (1 << 29)
#define INFF 0x7fffffff
#define LINF (1ll << 60)
#define LINFF 0x7fffffffffffffff
#define PI 3.14159265359
#define _fi(n) for (int i = 0; i < n; i++)
#define _fj(n) for (int j = 0; j < n; j++)
#define _fk(n) for (int k = 0; k < n; k++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define YESNO(ans) cout << (ans ? "YES" : "NO") << endl
#define prec(x) cout << fixed << setprecision(x)
#define pqueue priority_queue
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define endl '\n'

const int maxn = 1e5;
vector<int> adj[maxn];
int dp[maxn + 7] = {};

int dfs(int a) {
  if (dp[a] == 0) {
    dp[a] = 1;
    for (int b : adj[a]) {
      dp[a] = max(dp[a], dfs(b) + 1);
    }
  }
  return dp[a];
}

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

  int n, m;
  cin >> n >> m;
  _fi(m) {
    int a, b;
    cin >> a >> b;
    adj[a].pb(b);
  }

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

  return 0;
}


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

using namespace std;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

#define hash sodnf9843bsd
#define ll long long
#define INF (1 << 29)
#define INFF 0x7fffffff
#define LINF (1ll << 60)
#define LINFF 0x7fffffffffffffff
#define PI 3.14159265359
#define _fi(n) for (int i = 0; i < n; i++)
#define _fj(n) for (int j = 0; j < n; j++)
#define _fk(n) for (int k = 0; k < n; k++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define YESNO(ans) cout << (ans ? "YES" : "NO") << endl
#define prec(x) cout << fixed << setprecision(x)
#define pqueue priority_queue
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define endl '\n'

const int maxn = 1e5;
vector<int> adj[maxn + 7];
int dp[maxn + 7] = {};

int dfs(int a) {
  if (dp[a] == 0) {
    dp[a] = 1;
    for (int b : adj[a]) {
      dp[a] = max(dp[a], dfs(b) + 1);
    }
  }
  return dp[a];
}

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

  int n, m;
  cin >> n >> m;
  _fi(m) {
    int a, b;
    cin >> a >> b;
    adj[a].pb(b);
  }

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

  return 0;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
vector<int> A[100001];
int dp[100001];
bool visit[100001];
int dfs(int x) {
  int mx = 0;
  visit[x] = false;
  if (A[x].size() == 0) {
    dp[x] = 0;
    return 0;
  }
  for (int i = 0; i < A[x].size(); i++)
    if (visit[A[x][i]] == false)
      mx = max(mx, dfs(A[x][i]));
    else
      mx = max(mx, dp[A[x][i]]);
  dp[x] = mx + 1;
  return mx + 1;
}
int main() {
  int n, m, a, b;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    A[a].push_back(b);
  }
  memset(visit, false, sizeof visit);
  memset(dp, -1, sizeof dp);
  for (int i = 0; i <= n; i++) {
    if (visit[i] == false)
      dfs(i);
  }
  sort(dp, dp + n + 1, greater<int>());
  cout << dp[0];
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
vector<int> A[100001];
int dp[100001];
bool visit[100001];
int dfs(int x) {
  int mx = 0;
  visit[x] = true;
  if (A[x].size() == 0) {
    dp[x] = 0;
    return 0;
  }
  for (int i = 0; i < A[x].size(); i++)
    if (visit[A[x][i]] == false)
      mx = max(mx, dfs(A[x][i]));
    else
      mx = max(mx, dp[A[x][i]]);
  dp[x] = mx + 1;
  return mx + 1;
}
int main() {
  int n, m, a, b;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    A[a].push_back(b);
  }
  memset(visit, false, sizeof visit);
  memset(dp, -1, sizeof dp);
  for (int i = 0; i <= n; i++) {
    if (visit[i] == false)
      dfs(i);
  }
  sort(dp, dp + n + 1, greater<int>());
  cout << dp[0];
  return 0;
}
Problem p03166 (C++)

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

#define int long long
#define pb push_back
#define mod 1000000007
#define endl '\n'
#define w(x)                                                                   \
  int x;                                                                       \
  cin >> x;                                                                    \
  while (x-- > 0)

void fast() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}
vector<vector<int>> v(100011);
// bool visited[10001]={false};
int dp[100011];
int n, m;
int ans = 0;
int dfs(int id) {
  if (v[id].size() == 0)
    return 0;
  if (dp[id] != -1)
    return dp[id];
  int t = -1;
  for (int i : v[id])
    t = max(t, dfs(i) + 1);
  ans = max(ans, t);
  return dp[id] = t;
}
void mysol() {
  memset(dp, -1, sizeof dp);
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    v[a].pb(b);
  }
  for (int i = 1; i <= n; i++) {

    dfs(i);
  }
  cout << ans;
}

int32_t main() {
  fast();
  // w(t)
  { mysol(); }
  return 0;
}


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

#define int long long
#define pb push_back
#define mod 1000000007
#define endl '\n'
#define w(x)                                                                   \
  int x;                                                                       \
  cin >> x;                                                                    \
  while (x-- > 0)

void fast() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}
vector<vector<int>> v(100011);
// bool visited[10001]={false};
int dp[100011];
int n, m;
int ans = 0;
int dfs(int id) {
  if (v[id].size() == 0)
    return 0;
  if (dp[id] != -1)
    return dp[id];
  int t = -1;
  for (int i : v[id])
    t = max(t, dfs(i) + 1);
  ans = max(ans, t);
  return dp[id] = t;
}
void mysol() {
  memset(dp, -1, sizeof dp);
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    v[a].pb(b);
  }
  for (int i = 1; i <= n; i++) {

    dfs(i);
  }
  cout << ans;
}

int32_t main() {
  // fast();
  //  w(t)
  { mysol(); }
  return 0;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int child[100001];
int marker[100001];
int maximumm = 0;
int dfs(int head, vector<int> v[]) {
  child[head] = 1;
  int maxi = 0;
  for (int i = 0; i < v[head].size(); i++) {
    // maxi=0;
    if (marker[v[head][i]] == 0) {
      int x = dfs(v[head][i], v);
      if (x > maxi)
        maxi = x;
    } else {
      int k = child[v[head][i]];
      if (k > maxi)
        maxi = k;
    }
  }
  child[head] += maxi;
  return child[head];
}

int main() {
  for (int i = 0; i <= 100001; i++) {
    child[i] = 0;
    marker[i] = 0;
  }
  int n, m;
  cin >> n;
  cin >> m;
  vector<int> v[n + 1];
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a;
    cin >> b;
    v[a].push_back(b);
  }
  for (int i = 1; i <= n; i++) {
    if (marker[i] == 0) {
      marker[i] = 1;
      int y = dfs(i, v);
      if (y > maximumm)
        maximumm = y;
    }
  }
  cout << maximumm - 1 << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int child[100001];
int marker[100001];
int maximumm = 0;
int dfs(int head, vector<int> v[]) {
  child[head] = 1;
  int maxi = 0;
  for (int i = 0; i < v[head].size(); i++) {
    // maxi=0;
    if (marker[v[head][i]] == 0) {
      marker[v[head][i]] = 1;
      int x = dfs(v[head][i], v);
      if (x > maxi)
        maxi = x;
    } else {
      int k = child[v[head][i]];
      if (k > maxi)
        maxi = k;
    }
  }
  child[head] += maxi;
  return child[head];
}

int main() {
  for (int i = 0; i <= 100001; i++) {
    child[i] = 0;
    marker[i] = 0;
  }
  int n, m;
  cin >> n;
  cin >> m;
  vector<int> v[n + 1];
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a;
    cin >> b;
    v[a].push_back(b);
  }
  for (int i = 1; i <= n; i++) {
    if (marker[i] == 0) {
      marker[i] = 1;
      int y = dfs(i, v);
      if (y > maximumm)
        maximumm = y;
    }
  }
  cout << maximumm - 1 << endl;
  return 0;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
/*author:anshumanc007
   created on:08/01/2019
            */
// EMBRACE THE STRUGGLE!!!
#include <bits/stdc++.h>
#include <queue>
#include <stack>
using namespace std;

// typedef pair<long long int,long long int> pa;
#define MAX 100005
long long int MOD = 1e9 + 7;
long long int INF = 1e18;
stack<long long int> st;
bool visited[MAX];
long long int dp[MAX];
void dfs(vector<long long int> adj[], long long int i) {
  visited[i] = true;
  long long int j;
  for (auto j : adj[i]) {
    dfs(adj, j);
  }
  st.push(i);
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int i, j, k, l, m, n, o, p, q, r, x, y, a, b, mini = INF, maxi = 0,
                                                          cnt = 0, W, ans;
  cin >> n >> m;
  vector<long long int> adj[n];
  vector<long long int> adj1[n];
  for (i = 0; i < m; i++) {
    cin >> x >> y;
    x--;
    y--;
    adj[x].push_back(y);
    adj1[y].push_back(x);
  }
  for (i = 0; i < n; i++) {
    visited[i] = false;
    dp[i] = 0;
  }
  for (i = 0; i < n; i++) {
    if (!visited[i]) {
      dfs(adj, i);
    }
  }
  vector<long long int> V;
  while (!st.empty()) {
    j = st.top();
    st.pop();
    V.push_back(j);
  }

  for (j = V.size() - 1; j >= 0; j--) {
    i = V[j];
    //  st.pop();
    for (auto k : adj1[i]) {
      dp[k] = max(dp[k], dp[i] + 1);
      if (dp[k] > maxi) {
        maxi = dp[k];
      }
    }
  }
  cout << maxi;
}

Fixed submission:
/*author:anshumanc007
   created on:08/01/2019
            */
// EMBRACE THE STRUGGLE!!!
#include <bits/stdc++.h>
#include <queue>
#include <stack>
using namespace std;

// typedef pair<long long int,long long int> pa;
#define MAX 100005
long long int MOD = 1e9 + 7;
long long int INF = 1e18;
stack<long long int> st;
bool visited[MAX];
long long int dp[MAX];
void dfs(vector<long long int> adj[], long long int i) {
  visited[i] = true;
  long long int j;
  for (auto j : adj[i]) {
    if (!visited[j])
      dfs(adj, j);
  }
  st.push(i);
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int i, j, k, l, m, n, o, p, q, r, x, y, a, b, mini = INF, maxi = 0,
                                                          cnt = 0, W, ans;
  cin >> n >> m;
  vector<long long int> adj[n];
  vector<long long int> adj1[n];
  for (i = 0; i < m; i++) {
    cin >> x >> y;
    x--;
    y--;
    adj[x].push_back(y);
    adj1[y].push_back(x);
  }
  for (i = 0; i < n; i++) {
    visited[i] = false;
    dp[i] = 0;
  }
  for (i = 0; i < n; i++) {
    if (!visited[i]) {
      dfs(adj, i);
    }
  }
  vector<long long int> V;
  while (!st.empty()) {
    j = st.top();
    st.pop();
    V.push_back(j);
  }

  for (j = V.size() - 1; j >= 0; j--) {
    i = V[j];
    //  st.pop();
    for (auto k : adj1[i]) {
      dp[k] = max(dp[k], dp[i] + 1);
      if (dp[k] > maxi) {
        maxi = dp[k];
      }
    }
  }
  cout << maxi;
}
Problem p03166 (C++)

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

using namespace std;

const int N = 1e5;
const int INF = 1e9 + 5;

int dp[N];
bool vis[N];
vector<int> adj[N];
int mx = 0;

int calc(int x) {

  if (x < 1)
    return -1 * INF;
  if (vis[x])
    return dp[x];

  vis[x] = 1;

  int mx_dist = 0;
  if (adj[x].size())
    for (auto i : adj[x]) {
      mx_dist = max(mx_dist, calc(i) + 1);
    }

  return dp[x] = max(dp[x], mx_dist);
}

int main() {

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

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

  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;

    adj[x].push_back(y);
  }

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

    mx = max(mx, dp[i]);
  }

  cout << mx << '\n';
}


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

using namespace std;

const int N = 1e5 + 5;
const int INF = 1e9 + 5;

int dp[N];
bool vis[N];
vector<int> adj[N];
int mx = 0;

int calc(int x) {

  if (x < 1)
    return -1 * INF;
  if (vis[x])
    return dp[x];

  vis[x] = 1;

  int mx_dist = 0;
  if (adj[x].size())
    for (auto i : adj[x]) {
      mx_dist = max(mx_dist, calc(i) + 1);
    }

  return dp[x] = max(dp[x], mx_dist);
}

int main() {

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

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

  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;

    adj[x].push_back(y);
  }

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

    mx = max(mx, dp[i]);
  }

  cout << mx << '\n';
}
Problem p03166 (C++)

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

using namespace std;

int dfs(vector<int> g[], int dp[], int x) {
  int temp = 0;
  for (auto it : g[x]) {
    temp = max(temp, dfs(g, dp, it) + 1);
  }

  dp[x] = temp;
  return temp;
}

int longest_path(vector<int> g[], int n) {

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

  for (int i = 0; i < n; i++) {
    if (dp[i] == -1) {
      dfs(g, dp, i);
    }
  }

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

  return ans;
}

void input() {
  int n, m;
  cin >> n >> m;

  vector<int> g[n];

  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;

    x -= 1;
    y -= 1;

    g[x].push_back(y);
  }

  int ans = longest_path(g, n);

  cout << ans;
}

int main() {

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

  input();

  return 0;
}


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

using namespace std;

int dfs(vector<int> g[], int dp[], int x) {

  if (dp[x] != -1) {
    return dp[x];
  }

  int temp = 0;
  for (auto it : g[x]) {
    temp = max(temp, dfs(g, dp, it) + 1);
  }

  dp[x] = temp;
  return temp;
}

int longest_path(vector<int> g[], int n) {

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

  for (int i = 0; i < n; i++) {
    if (dp[i] == -1) {
      dfs(g, dp, i);
    }
  }

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

  return ans;
}

void input() {
  int n, m;
  cin >> n >> m;

  vector<int> g[n];

  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;

    x -= 1;
    y -= 1;

    g[x].push_back(y);
  }

  int ans = longest_path(g, n);

  cout << ans;
}

int main() {

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

  input();

  return 0;
}
Problem p03166 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define repr(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repr(i, 0, n)
#define INF 2e9
#define MOD 1000000007
#define LINF (long long)4e18

using ll = long long;

vector<vector<int>> G;
int N, M;
int dp[100010];

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

int main() {
  cin >> N >> M;
  G = vector<vector<int>>(M);
  rep(i, N) { dp[i] = -1; }
  rep(i, M) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    G[x].push_back(y);
  }
  int res = 0;
  rep(i, N) { res = max(res, rec(i)); }
  cout << res << endl;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define repr(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repr(i, 0, n)
#define INF 2e9
#define MOD 1000000007
#define LINF (long long)4e18

using ll = long long;

vector<vector<int>> G;
int N, M;
int dp[100010];

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

int main() {
  cin >> N >> M;
  G.resize(N);
  rep(i, N) { dp[i] = -1; }
  rep(i, M) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    G[x].push_back(y);
  }
  int res = 0;
  rep(i, N) { res = max(res, rec(i)); }
  cout << res << endl;
}
Problem p03166 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < n; i++)
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}

ll n, m;
vector<vector<ll>> G(n);

ll dp[100100];
ll rec(ll v) {
  if (dp[v] != -1)
    return dp[v];

  ll res = 0;
  for (auto nv : G[v]) {
    chmax(res, rec(nv) + 1);
  }
  return dp[v] = res;
}

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

  rep(v, n) dp[v] = -1;

  ll res = 0;
  rep(v, n) chmax(res, rec(v));
  cout << res << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < n; i++)
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}

ll n, m;
vector<vector<ll>> G(100100);

ll dp[100100];
ll rec(ll v) {
  if (dp[v] != -1)
    return dp[v];

  ll res = 0;
  for (auto nv : G[v]) {
    chmax(res, rec(nv) + 1);
  }
  return dp[v] = res;
}

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

  rep(v, n) dp[v] = -1;

  ll res = 0;
  rep(v, n) chmax(res, rec(v));
  cout << res << endl;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using vc = vector<char>;
using vvc = vector<vc>;
using pll = pair<ll, ll>;
using vpll = vector<pll>;
const ll INF = 1LL << 60;
const ll MOD = 1e9 + 7;
#define rep(i, n) for (ll i = 0; i < (n); i++)
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}
#ifndef ONLINE_JUDGE
#define debug(x) cerr << #x << ": " << x << endl;
#else
#define debug(x)
#endif

vvll g;
ll dp[110000];
bool check[110000];

void dfs(ll x) {
  if (check[x])
    return;

  ll tmp = 0;
  for (auto nx : g[x]) {
    dfs(nx);
    chmax(tmp, dp[nx] + 1);
  }
  dp[x] = tmp;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);

  ll N, M;
  cin >> N >> M;
  g.resize(N);
  rep(_, M) {
    ll x, y;
    cin >> x >> y;
    x--, y--;
    g[x].push_back(y);
  }

  rep(i, N) dfs(i);
  ll ans = 0;
  rep(i, N) chmax(ans, dp[i]);
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using vc = vector<char>;
using vvc = vector<vc>;
using pll = pair<ll, ll>;
using vpll = vector<pll>;
const ll INF = 1LL << 60;
const ll MOD = 1e9 + 7;
#define rep(i, n) for (ll i = 0; i < (n); i++)
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}
#ifndef ONLINE_JUDGE
#define debug(x) cerr << #x << ": " << x << endl;
#else
#define debug(x)
#endif

vvll g;
ll dp[110000];
bool check[110000];

void dfs(ll x) {
  if (check[x])
    return;

  ll tmp = 0;
  for (auto nx : g[x]) {
    dfs(nx);
    chmax(tmp, dp[nx] + 1);
  }
  check[x] = true;
  dp[x] = tmp;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);

  ll N, M;
  cin >> N >> M;
  g.resize(N);
  rep(_, M) {
    ll x, y;
    cin >> x >> y;
    x--, y--;
    g[x].push_back(y);
  }

  rep(i, N) dfs(i);
  ll ans = 0;
  rep(i, N) chmax(ans, dp[i]);
  cout << ans << endl;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
vector<int> a[100005];
int dp[100005], n, m;
int dfs(int i) {
  if (dp[i] != -1)
    return dp[i];
  int ans = 0;
  for (int x : a[i])
    ans = max(ans, dfs(x) + 1);
  return ans;
}
int main() {
  memset(dp, -1, sizeof(dp));
  cin >> n >> m;
  while (m--) {
    int x, y;
    cin >> x >> y;
    a[x].push_back(y);
  }
  int ans = 0, i;
  for (i = 1; i <= n; i++)
    ans = max(ans, dfs(i));
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
vector<int> a[100005];
int dp[100005], n, m;
int dfs(int i) {
  if (dp[i] != -1)
    return dp[i];
  int ans = 0;
  for (int x : a[i])
    ans = max(ans, dfs(x) + 1);
  return dp[i] = ans;
}
int main() {
  memset(dp, -1, sizeof(dp));
  cin >> n >> m;
  while (m--) {
    int x, y;
    cin >> x >> y;
    a[x].push_back(y);
  }
  int ans = 0, i;
  for (i = 1; i <= n; i++)
    ans = max(ans, dfs(i));
  cout << ans << endl;
}
Problem p03166 (C++)

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

typedef long long ll;
typedef unsigned long long ull;

constexpr size_t MAXN = 1e5;
constexpr size_t MAXM = 1e5;
constexpr int MOD = 1e9 + 7;

std::vector<std::vector<int>> graph;
std::vector<int> dp;
std::vector<bool> traversed;

void traverse(int to) {
  for (int from : graph[to]) {
    if (!traversed[from]) {
      traverse(from);
    }
    dp[to] = std::max(dp[to], dp[from] + 1);
  }
}

void solve() {
  int N, M;
  std::cin >> N >> M;
  graph.resize(N + 1);
  dp.resize(N + 1);
  traversed.resize(N + 1);
  for (int i = 0; i < M; ++i) {
    int from, to;
    std::cin >> from >> to;
    graph[to].push_back(from);
  }

  for (int to = 1; to <= N; to++) {
    if (!graph[to].empty()) {
      traverse(to);
    }
  }

  int mx = dp[0];
  for (int to : dp) {
    mx = std::max(to, mx);
  }

  std::cout << mx << "\n";
}

void warp() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(0);
  std::cout.tie(0);
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
}

int main() {
  warp();
  solve();
  return 0;
}


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

typedef long long ll;
typedef unsigned long long ull;

constexpr size_t MAXN = 1e5;
constexpr size_t MAXM = 1e5;
constexpr int MOD = 1e9 + 7;

std::vector<std::vector<int>> graph;
std::vector<int> dp;
std::vector<bool> traversed;

void traverse(int to) {
  for (int from : graph[to]) {
    if (!traversed[from]) {
      traverse(from);
      traversed[from] = true;
    }
    dp[to] = std::max(dp[to], dp[from] + 1);
  }
}

void solve() {
  int N, M;
  std::cin >> N >> M;
  graph.resize(N + 1);
  dp.resize(N + 1);
  traversed.resize(N + 1);
  for (int i = 0; i < M; ++i) {
    int from, to;
    std::cin >> from >> to;
    graph[to].push_back(from);
  }

  for (int to = 1; to <= N; to++) {
    if (!graph[to].empty()) {
      traverse(to);
    }
  }

  int mx = dp[0];
  for (int to : dp) {
    mx = std::max(to, mx);
  }

  std::cout << mx << "\n";
}

void warp() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(0);
  std::cout.tie(0);
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
}

int main() {
  warp();
  solve();
  return 0;
}