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

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

using namespace std;

void usain_bolt() {
  ios::sync_with_stdio(false);
  cin.tie(0);
}

const int N = 1e5 + 5;

vector<int> a[N];
queue<int> q;

int in[N], mx, d[N];

void bfs() {
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    mx = max(mx, d[x]);
    if (in[x] > 0)
      continue;
    for (auto v : a[x]) {
      if (d[v] <= d[x] + 1)
        d[v] = d[x] + 1, q.push(v);
      --in[v];
    }
  }
}

int main() {
  usain_bolt();

  int n, m;

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

    cin >> x >> y;
    a[x].push_back(y);
    ++in[y];
  }
  for (int i = 1; i <= n; ++i)
    if (in[i] == 0)
      q.push(i);
  bfs();
  cout << mx;
  return 0;
}


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

using namespace std;

void usain_bolt() {
  ios::sync_with_stdio(false);
  cin.tie(0);
}

const int N = 1e5 + 5;

vector<int> a[N];
queue<int> q;

int in[N], mx, d[N];

void bfs() {
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    mx = max(mx, d[x]);
    if (in[x] > 0)
      continue;
    for (auto v : a[x]) {
      if (d[v] < d[x] + 1)
        d[v] = d[x] + 1, q.push(v);
      --in[v];
    }
  }
}

int main() {
  usain_bolt();

  int n, m;

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

    cin >> x >> y;
    a[x].push_back(y);
    ++in[y];
  }
  for (int i = 1; i <= n; ++i)
    if (in[i] == 0)
      q.push(i);
  bfs();
  cout << mx;
  return 0;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define mods 1000000007
#define pb push_back
#define mp make_pair
#define st first
#define nd second
typedef long long int lint;
typedef unsigned long long int ulint;
list<lint> gelen[100020], giden[100020];
lint dp[100020];
bool hes[100020];
lint hesap(lint a);
lint n, m;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  lint i, j, cev = 0, a, b, k, l;
  cin >> n >> m;
  for (i = 0; i < m; i++) {
    cin >> a >> b;
    gelen[b].pb(a);
    giden[a].pb(b);
  }
  for (i = 1; i <= n; i++) {
    if (gelen[i].size() == 0) {
      cev = fmax(cev, hesap(i));
    }
  }
  cout << cev;

  return 0;
}
lint hesap(lint a) {
  if (hes[a] == true)
    return dp[a];
  hes[a] == true;
  lint cev = 0;
  if (giden[a].size() == 0) {
    dp[a] = 0;
    return 0;
  }
  list<lint>::iterator it;
  for (it = giden[a].begin(); it != giden[a].end(); it++) {
    cev = fmax(cev, hesap(*it) + 1);
  }
  dp[a] = cev;
  return cev;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define mods 1000000007
#define pb push_back
#define mp make_pair
#define st first
#define nd second
typedef long long int lint;
typedef unsigned long long int ulint;
list<lint> gelen[100020], giden[100020];
lint dp[100020];
bool hes[100020];
lint hesap(lint a);
lint n, m;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  lint i, j, cev = 0, a, b, k, l;
  cin >> n >> m;
  for (i = 0; i < m; i++) {
    cin >> a >> b;
    gelen[b].pb(a);
    giden[a].pb(b);
  }
  for (i = 1; i <= n; i++) {
    if (gelen[i].size() == 0) {
      cev = fmax(cev, hesap(i));
    }
  }
  cout << cev;

  return 0;
}
lint hesap(lint a) {
  if (hes[a] == true)
    return dp[a];
  hes[a] = true;
  lint cev = 0;
  if (giden[a].size() == 0) {
    dp[a] = 0;
    return 0;
  }
  list<lint>::iterator it;
  for (it = giden[a].begin(); it != giden[a].end(); it++) {
    cev = fmax(cev, hesap(*it) + 1);
  }
  dp[a] = cev;
  return cev;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
const int inf = 2147483647, dx[] = {-1, 0, 1, 0},
          dy[] = {0, -1, 0, 1}; // 上 左 下 右
const int N = 100005, M = 1000005, mod = 1000000007;
const long long llinf = 9223372036854775807ll;
int n, m, x, y, dp[N], ans;
vector<int> v[N];
bool notroot[N];
int dfs(int x) {
  if (dp[x] != -1)
    return dp[x];
  if (v[x].empty()) {
    dp[x] = 0;
    return 0;
  }
  int res = -666666;
  for (int i = 0; i < v[x].size(); i++)
    res = max(res, dfs(v[x][i]) + 1);
  return res;
}
int main() {
  cin >> n >> m;
  memset(dp, -1, sizeof(dp));
  for (int i = 1; i <= m; i++) {
    cin >> x >> y;
    v[x].push_back(y);
    notroot[y] = true;
  }
  for (int i = 1; i <= n; i++)
    if (!notroot[i])
      ans = max(ans, dfs(i));
  cout << ans;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
const int inf = 2147483647, dx[] = {-1, 0, 1, 0},
          dy[] = {0, -1, 0, 1}; // 上 左 下 右
const int N = 100005, M = 1000005, mod = 1000000007;
const long long llinf = 9223372036854775807ll;
int n, m, x, y, dp[N], ans;
vector<int> v[N];
bool notroot[N];
int dfs(int x) {
  if (dp[x] != -1)
    return dp[x];
  if (v[x].empty()) {
    dp[x] = 0;
    return 0;
  }
  int res = -666666;
  for (int i = 0; i < v[x].size(); i++)
    res = max(res, dfs(v[x][i]) + 1);
  dp[x] = res;
  return res;
}
int main() {
  cin >> n >> m;
  memset(dp, -1, sizeof(dp));
  for (int i = 1; i <= m; i++) {
    cin >> x >> y;
    v[x].push_back(y);
    notroot[y] = true;
  }
  for (int i = 1; i <= n; i++)
    if (!notroot[i])
      ans = max(ans, dfs(i));
  cout << ans;
  return 0;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)n; i++)
typedef long long ll;
const int MAX = 1e5;

vector<int> dp(MAX, -1);
vector<int> g[MAX];

int dfs(int i) {
  if (dp[i] != -1)
    return dp[i];
  int d = 0;
  rep(j, g[i].size()) { d = max(d, dfs(g[i][j]) + 1); }
  return d;
}

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


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)n; i++)
typedef long long ll;
const int MAX = 1e5;

vector<int> dp(MAX, -1);
vector<int> g[MAX];

int dfs(int i) {
  if (dp[i] != -1)
    return dp[i];
  int d = 0;
  rep(j, g[i].size()) { d = max(d, dfs(g[i][j]) + 1); }
  return dp[i] = d;
}

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
vector<int> dp;
int helperfn(int i, vector<vector<int>> &matrix) {
  // cout<<"helperfn"<<"  "<<i<<endl;
  if (matrix[i].size() == 0)
    return 0;

  if (dp[i] != -1)
    return dp[i];
  int maxi = 0;
  for (int j = 0; j <= matrix[i].size(); j++) {
    maxi = max(maxi, 1 + helperfn(matrix[i][j], matrix));
  }

  dp[i] = maxi;
  return maxi;
}

int main() {
  int n, m;
  cin >> n >> m;

  vector<vector<int>> matrix(n + 2);

  for (int i = 0; i <= n; i++)
    dp.push_back(-1);

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

  int ans = 0;

  for (int i = 1; i <= n; i++) {

    int x = helperfn(i, matrix);
  }

  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;
vector<int> dp;
int helperfn(int i, vector<vector<int>> &matrix) {
  // cout<<"helperfn"<<"  "<<i<<endl;
  if (matrix[i].size() == 0)
    return 0;

  if (dp[i] != -1)
    return dp[i];
  int maxi = 0;
  for (int j = 0; j < matrix[i].size(); j++) {
    maxi = max(maxi, 1 + helperfn(matrix[i][j], matrix));
  }

  dp[i] = maxi;
  return maxi;
}

int main() {
  int n, m;
  cin >> n >> m;

  vector<vector<int>> matrix(n + 2);

  for (int i = 0; i <= n; i++)
    dp.push_back(-1);

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

  int ans = 0;

  for (int i = 1; i <= n; i++) {

    int x = helperfn(i, matrix);
  }

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

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

Buggy submission (status: Runtime Error):
// In the Name of ̲A̲l̲l̲a̲h̲

#include <bits/stdc++.h>

using namespace std;

int n, u, v, m;
vector<int> adj[10000];
bool mark[10000];
int mxPath[10000], mxDepth[10000];
void DFS(int v) {
  mark[v] = 1;
  if (!adj[v].size())
    return;
  int mx1 = 0;
  int mx3 = 0;
  int mx2 = 0;
  for (int u : adj[v]) {
    if (!mark[u]) {
      DFS(u);
    }
    if (mx1 < mxDepth[u] + 1) {
      mx1 = mxDepth[u] + 1;
    }
    if (mx3 < mxPath[u]) {
      mx3 = mxPath[u];
    }
  }
  if (adj[v].size() == 0)
    return;
  mxPath[v] = max(mx1, mx3);
  mxDepth[v] = mx1;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> u >> v;
    adj[u].push_back(v);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (!mark[i]) {
      DFS(i);
    }
    if (mxPath[i] > ans)
      ans = mxPath[i];
  }
  cout << ans;
}

/*𝐻𝒶𝒹𝒾*/


Fixed submission:
// In the Name of ̲A̲l̲l̲a̲h̲

#include <bits/stdc++.h>

using namespace std;

int n, u, v, m;
vector<int> adj[100001];
bool mark[100001];
int mxPath[100001], mxDepth[100001];
void DFS(int v) {
  mark[v] = 1;
  if (!adj[v].size())
    return;
  int mx1 = 0;
  int mx3 = 0;
  int mx2 = 0;
  for (int u : adj[v]) {
    if (!mark[u]) {
      DFS(u);
    }
    if (mx1 < mxDepth[u] + 1) {
      mx1 = mxDepth[u] + 1;
    }
    if (mx3 < mxPath[u]) {
      mx3 = mxPath[u];
    }
  }
  if (adj[v].size() == 0)
    return;
  mxPath[v] = max(mx1, mx3);
  mxDepth[v] = mx1;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> u >> v;
    adj[u].push_back(v);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (!mark[i]) {
      DFS(i);
    }
    if (mxPath[i] > ans)
      ans = mxPath[i];
  }
  cout << ans;
}

/*𝐻𝒶𝒹𝒾*/
Problem p03166 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <fstream>
#include <iostream>
#include <vector>
#define f cin
#define NMAX 200010
using namespace std;

// ifstream f("date.in");

int n, m;
vector<int> a[NMAX];
int interior[NMAX], exterior[NMAX];
int nr, top_sort[NMAX];
int dp[NMAX];
int vis[NMAX];

void dfs(int x) {
  for (int i = 0; i < a[x].size(); ++i) {
    if (vis[a[x][i]] == 0) {
      dfs(a[x][i]);
    }
  }
  top_sort[nr++] = x;
}

int main() {

  f >> n >> m;
  for (int i = 0; i < m; ++i) {
    int x, y;
    f >> x >> y;
    x--;
    y--;
    a[x].push_back(y);
    interior[y]++;
    exterior[x]++;
  }

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

  int longest_path = 0;

  for (int i = 0; i < nr; ++i) {
    // cout << top_sort[i] << " ";
    int v_max = 0;
    int current_element = top_sort[i];
    for (int j = 0; j < a[current_element].size(); ++j) {
      int current_neig = a[current_element][j];
      v_max = max(v_max, dp[current_neig]);
    }
    dp[current_element] = v_max + 1;
    longest_path = max(longest_path, dp[current_element]);
  }

  cout << longest_path - 1;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <fstream>
#include <iostream>
#include <vector>
#define f cin
#define NMAX 200010
using namespace std;

// ifstream f("date.in");

int n, m;
vector<int> a[NMAX];
int interior[NMAX], exterior[NMAX];
int nr, top_sort[NMAX];
int dp[NMAX];
int vis[NMAX];

void dfs(int x) {
  vis[x] = 1;
  for (int i = 0; i < a[x].size(); ++i) {
    if (vis[a[x][i]] == 0) {
      dfs(a[x][i]);
    }
  }
  top_sort[nr++] = x;
}

int main() {

  f >> n >> m;
  for (int i = 0; i < m; ++i) {
    int x, y;
    f >> x >> y;
    x--;
    y--;
    a[x].push_back(y);
    interior[y]++;
    exterior[x]++;
  }

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

  int longest_path = 0;

  for (int i = 0; i < nr; ++i) {
    // cout << top_sort[i] << " ";
    int v_max = 0;
    int current_element = top_sort[i];
    for (int j = 0; j < a[current_element].size(); ++j) {
      int current_neig = a[current_element][j];
      v_max = max(v_max, dp[current_neig]);
    }
    dp[current_element] = v_max + 1;
    longest_path = max(longest_path, dp[current_element]);
  }

  cout << longest_path - 1;

  return 0;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
constexpr int n = 1e5 + 5;
int N, M;
vector<vector<int>> edges(n);
vector<int> dp(n, 0);
vector<int> vis(n, 0);
int dfs(int x) {
  if (!x) {
    return 0;
  }
  int ans = 0;
  for (int &nb : edges[x]) {

    ans = max(dfs(nb) + 1, ans);
  }
  vis[x] = 1;
  dp[x] = ans;
  return dp[x];
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int x, y;
  cin >> N >> M;
  for (int i = 1; i <= N; i++) {
    edges[i].push_back(0);
  }
  for (int i = 0; i < M; i++) {
    cin >> x >> y;
    edges[y].push_back(x);
  }
  int ans = 0;
  for (int i = 1; i <= N; i++) {
    if (!vis[i]) {
      ans = max(dfs(i), ans);
    } else {
      ans = max(dp[i], ans);
    }
  }
  cout << ans - 1;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
constexpr int n = 1e5 + 5;
int N, M;
vector<vector<int>> edges(n);
vector<int> dp(n, 0);
vector<int> vis(n, 0);
int dfs(int x) {
  if (!x) {
    return 0;
  }
  int ans = 0;
  for (int &nb : edges[x]) {
    if (!vis[nb]) {
      ans = max(dfs(nb) + 1, ans);
    } else {
      ans = max(dp[nb] + 1, ans);
    }
  }
  vis[x] = 1;
  dp[x] = ans;
  return dp[x];
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int x, y;
  cin >> N >> M;
  for (int i = 1; i <= N; i++) {
    edges[i].push_back(0);
  }
  for (int i = 0; i < M; i++) {
    cin >> x >> y;
    edges[y].push_back(x);
  }
  int ans = 0;
  for (int i = 1; i <= N; i++) {
    if (!vis[i]) {
      ans = max(dfs(i), ans);
    } else {
      ans = max(dp[i], ans);
    }
  }
  cout << ans - 1;
  return 0;
}
Problem p03166 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
vector<int> g[100000];
long long mx[100000];
int n, m;
void dfs(int u) {
  if (mx[u] == 0)
    for (auto i = g[u].begin(); i != g[u].end(); ++i) {
      if (mx[*i] == 0)
        dfs(*i);
      mx[u] = max(mx[u], mx[*i] + 1);
    }
}
int main() {
  cin >> n >> m;
  int u, v;
  for (int i = 0; i < m; ++i) {
    cin >> u >> v;
    g[u].push_back(v);
  }
  long long w = 0;
  for (int i = 1; i <= n; ++i)
    dfs(i);
  for (int i = 1; i <= n; ++i)
    w = max(w, mx[i]);
  cout << w;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
vector<int> g[100001];
long long mx[100001];
int n, m;
void dfs(int u) {
  if (mx[u] == 0)
    for (auto i = g[u].begin(); i != g[u].end(); ++i) {
      if (mx[*i] == 0)
        dfs(*i);
      mx[u] = max(mx[u], mx[*i] + 1);
    }
}
int main() {
  cin >> n >> m;
  int u, v;
  for (int i = 0; i < m; ++i) {
    cin >> u >> v;
    g[u].push_back(v);
  }
  long long w = 0;
  for (int i = 1; i <= n; ++i)
    dfs(i);
  for (int i = 1; i <= n; ++i)
    w = max(w, mx[i]);
  cout << w;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
const int MAX = 1e5;
vector<bool> visited(MAX);
vector<int> dist(MAX);
vector<vector<int>> adj(MAX);
void dfs(int u) {
  dist[u] = 0;
  for (int it : adj[u]) {
    if (!visited[it]) {
      dfs(it);
    }
    dist[u] = max(dist[u], 1 + dist[it]);
  }
}
void solve() {
  int n, m;
  cin >> n >> m;
  visited.assign(n, false);
  while (m--) {
    int x, y;
    cin >> x >> y;
    x--, y--;
    adj[x].push_back(y);
  }
  int res = 0;
  for (int i = 0; i < n; i++) {
    if (!visited[i]) {
      dfs(i);
    }
    res = max(res, dist[i]);
  }
  cout << res << '\n';
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  solve();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
const int MAX = 1e5;
vector<bool> visited(MAX);
vector<int> dist(MAX);
vector<vector<int>> adj(MAX);
void dfs(int u) {
  visited[u] = true;
  dist[u] = 0;
  for (int it : adj[u]) {
    if (!visited[it]) {
      dfs(it);
    }
    dist[u] = max(dist[u], 1 + dist[it]);
  }
}
void solve() {
  int n, m;
  cin >> n >> m;
  visited.assign(n, false);
  while (m--) {
    int x, y;
    cin >> x >> y;
    x--, y--;
    adj[x].push_back(y);
  }
  int res = 0;
  for (int i = 0; i < n; i++) {
    if (!visited[i]) {
      dfs(i);
    }
    res = max(res, dist[i]);
  }
  cout << res << '\n';
}

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

Buggy submission (status: Runtime Error):
// BWOTSHEWCHB

#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define in insert
#define X first
#define Y second

const int MAXN = 1e4 + 69, MOD = 1e9 + 7;

int n, m, ans, mark[MAXN], dist[MAXN];
vector<int> top, g[MAXN], rev[MAXN];

void dfs(int v) {
  mark[v] = 1;
  for (auto u : g[v])
    if (!mark[u])
      dfs(u);
  top.pb(v);
}

int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  int v, u;
  for (int i = 1; i <= m; i++)
    cin >> v >> u, g[v].pb(u), rev[u].pb(v);
  for (int i = 1; i <= n; i++)
    if (!mark[i])
      dfs(i);
  while (top.size()) {
    int v = top.back();
    for (auto u : rev[v])
      dist[v] = max(dist[v], dist[u] + 1);
    top.pop_back();
    ans = max(ans, dist[v]);
  }
  cout << ans << "\n";
}


Fixed submission:
// BWOTSHEWCHB

#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define in insert
#define X first
#define Y second

const int MAXN = 1e6 + 69, MOD = 1e9 + 7;

int n, m, ans, mark[MAXN], dist[MAXN];
vector<int> top, g[MAXN], rev[MAXN];

void dfs(int v) {
  mark[v] = 1;
  for (auto u : g[v])
    if (!mark[u])
      dfs(u);
  top.pb(v);
}

int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  int v, u;
  for (int i = 1; i <= m; i++)
    cin >> v >> u, g[v].pb(u), rev[u].pb(v);
  for (int i = 1; i <= n; i++)
    if (!mark[i])
      dfs(i);
  while (top.size()) {
    int v = top.back();
    for (auto u : rev[v])
      dist[v] = max(dist[v], dist[u] + 1);
    top.pop_back();
    ans = max(ans, dist[v]);
  }
  cout << ans << "\n";
}
Problem p03166 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#define ll long long

const ll INF = 1LL << 60;

// 入力
ll N, M;
vector<vector<ll>> vec(10010);

// dp
ll dp[100010];

ll lg(ll v) {
  // 頂点vからの最も長い矢印の長さ
  if (dp[v] != -1) {
    return dp[v];
  } else {
    if (vec[v].size() == 0) {
      chmax(dp[v], 0LL);
    } else {
      for (ll i : vec[v]) {
        chmax(dp[v], lg(i) + 1);
      }
    }
    // cout<<v<<endl;
    return dp[v];
  }
}

int main() {
  cin >> N >> M;
  ll x, y;
  for (int i = 0; i < M; i++) {
    cin >> x >> y;
    vec[x - 1].push_back(y - 1);
  }
  for (int i = 0; i <= N; i++)
    dp[i] = -1;

  ll res = 0;
  for (ll i = 0; i < N; i++) {
    chmax(res, lg(i));
  }

  cout << res << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#define ll long long

const ll INF = 1LL << 60;

// 入力
ll N, M;
vector<vector<ll>> vec(100010);

// dp
ll dp[100010];

ll lg(ll v) {
  // 頂点vからの最も長い矢印の長さ
  if (dp[v] != -1) {
    return dp[v];
  } else {
    if (vec[v].size() == 0) {
      chmax(dp[v], 0LL);
    } else {
      for (ll i : vec[v]) {
        chmax(dp[v], lg(i) + 1);
      }
    }
    // cout<<v<<endl;
    return dp[v];
  }
}

int main() {
  cin >> N >> M;
  ll x, y;
  for (int i = 0; i < M; i++) {
    cin >> x >> y;
    vec[x - 1].push_back(y - 1);
  }
  for (int i = 0; i <= N; i++)
    dp[i] = -1;

  ll res = 0;
  for (ll i = 0; i < N; i++) {
    chmax(res, lg(i));
  }

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

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

#define MEM(a, b) memset(a, (b), sizeof(a))
#define PI 3.1415926535897932384626433832795
#define endl '\n'
#define exists(s, e) (s.find(e) != s.end())
#define WHILE(n)
#define pb push_back
#define f first
#define s second
#define UNI(vec) vec.erase(std::unique(vec.begin(), vec.end()), vec.end());
#define scan(x)                                                                \
  do {                                                                         \
    while ((x = getchar()) < '0')                                              \
      ;                                                                        \
    for (x -= '0'; '0' <= (_ = getchar()); x = (x << 3) + (x << 1) + _ - '0')  \
      ;                                                                        \
  } while (0)
char _;
typedef long long ll;
typedef long double ld;
typedef std::pair<int, int> pii;
typedef std::pair<int, pii> piii;

const int INF = 0x3f3f3f3f;
#define MAXN 10001
using namespace std;
int n, m, ans;
int dp[MAXN];
vector<int> a[MAXN];

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

int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (int i = 0, x, y; i < m; i++) {
    cin >> x >> y;
    a[x].pb(y);
  }
  for (int i = 1; i <= n; i++) {
    ans = max(ans, dfs(i));
  }
  cout << ans;
}


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

#define MEM(a, b) memset(a, (b), sizeof(a))
#define PI 3.1415926535897932384626433832795
#define endl '\n'
#define exists(s, e) (s.find(e) != s.end())
#define WHILE(n)
#define pb push_back
#define f first
#define s second
#define UNI(vec) vec.erase(std::unique(vec.begin(), vec.end()), vec.end());
#define scan(x)                                                                \
  do {                                                                         \
    while ((x = getchar()) < '0')                                              \
      ;                                                                        \
    for (x -= '0'; '0' <= (_ = getchar()); x = (x << 3) + (x << 1) + _ - '0')  \
      ;                                                                        \
  } while (0)
char _;
typedef long long ll;
typedef long double ld;
typedef std::pair<int, int> pii;
typedef std::pair<int, pii> piii;

const int INF = 0x3f3f3f3f;
#define MAXN 100001
using namespace std;
int n, m, ans;
int dp[MAXN];
vector<int> a[MAXN];

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

int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (int i = 0, x, y; i < m; i++) {
    cin >> x >> y;
    a[x].pb(y);
  }
  for (int i = 1; i <= n; i++) {
    ans = max(ans, dfs(i));
  }
  cout << ans;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ii pair<int, int>
#define fi first
#define sc second
using namespace std;

const int N = 1e5 + 5;

int d[N], v[N];
vector<int> adj[N];

int dfs(int i) {
  v[i] = 1;
  for (int j = 0; j < (int)adj[i].size(); j++) {
    d[i] = max(d[i], dfs(adj[i][j]));
  }
  return d[i] + 1;
}

int main() {
  int n, m;
  cin >> n >> m;
  ii s[N];
  for (int i = 1; i <= n; i++) {
    s[i].second = i;
  }
  for (int i = 1; i <= m; i++) {
    int u, v;
    cin >> u >> v;
    adj[v].push_back(u);
    s[u].first += 1;
  }
  sort(s + 1, s + n + 1);
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (!v[s[i].second]) {
      dfs(s[i].second);
      ans = max(ans, d[s[i].second]);
    }
  }
  cout << ans << '\n';
}

Fixed submission:
#include <bits/stdc++.h>
#define ii pair<int, int>
#define fi first
#define sc second
using namespace std;

const int N = 1e5 + 5;

int d[N], v[N];
vector<int> adj[N];

int dfs(int i) {
  v[i] = 1;
  for (int j = 0; j < (int)adj[i].size(); j++) {
    int t = adj[i][j];
    if (v[t])
      t = d[t] + 1;
    else
      t = dfs(t);
    d[i] = max(d[i], t);
  }
  return d[i] + 1;
}

int main() {
  int n, m;
  cin >> n >> m;
  ii s[N];
  for (int i = 1; i <= n; i++) {
    s[i].second = i;
  }
  for (int i = 1; i <= m; i++) {
    int u, v;
    cin >> u >> v;
    adj[v].push_back(u);
    s[u].first += 1;
  }
  sort(s + 1, s + n + 1);
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (!v[s[i].second]) {
      dfs(s[i].second);
      ans = max(ans, d[s[i].second]);
    }
  }
  cout << ans << '\n';
}
Problem p03166 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using lli = long long;
#define rep(i, n) for (int i = 0; i < n; i++)
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

lli n, m;
vector<vector<lli>> v(10010);
vector<lli> dp(10010);
vector<bool> seen(10010);

lli f(lli x) {
  if (seen[x])
    return dp[x];
  seen[x] = true;
  lli mx = 0;
  for (auto to : v[x]) {
    mx = max(mx, f(to) + 1);
  }
  return dp[x] = mx;
}

int main(void) {
  cin >> n >> m;
  rep(i, m) {
    lli a, b;
    cin >> a >> b;
    a--;
    b--;
    v[a].push_back(b);
  }
  lli ans = 0;
  rep(i, n) { ans = max(ans, f(i)); }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using lli = long long;
#define rep(i, n) for (int i = 0; i < n; i++)
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

lli n, m;
vector<vector<lli>> v(100100);
vector<lli> dp(100100);
vector<bool> seen(100100);

lli f(lli x) {
  if (seen[x])
    return dp[x];
  seen[x] = true;
  lli mx = 0;
  for (auto to : v[x]) {
    mx = max(mx, f(to) + 1);
  }
  return dp[x] = mx;
}

int main(void) {
  cin >> n >> m;
  rep(i, m) {
    lli a, b;
    cin >> a >> b;
    a--;
    b--;
    v[a].push_back(b);
  }
  lli ans = 0;
  rep(i, n) { ans = max(ans, f(i)); }
  cout << ans << endl;
  return 0;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
const int INF = 1e9;

vector<int> e[100000];
int num[100000];

void dfs(int v) {
  int ma = -1;
  for (int u : e[v]) {
    dfs(u);
    ma = max(ma, num[u]);
  }
  num[v] = ma + 1;
}

int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    --x, --y;
    e[x].push_back(y);
  }
  for (int i = 0; i < n; i++) {
    num[i] = -1;
  }
  for (int i = 0; i < n; i++) {
    if (num[i] == -1) {
      dfs(i);
    }
  }
  int ans = -1;
  for (int i = 0; i < n; i++) {
    ans = max(ans, num[i]);
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
const int INF = 1e9;

vector<int> e[100000];
int num[100000];

void dfs(int v) {
  int ma = -1;
  for (int u : e[v]) {
    if (num[u] == -1)
      dfs(u);
    ma = max(ma, num[u]);
  }
  num[v] = ma + 1;
}

int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    --x, --y;
    e[x].push_back(y);
  }
  for (int i = 0; i < n; i++) {
    num[i] = -1;
  }
  for (int i = 0; i < n; i++) {
    if (num[i] == -1) {
      dfs(i);
    }
  }
  int ans = -1;
  for (int i = 0; i < n; i++) {
    ans = max(ans, num[i]);
  }
  cout << ans << endl;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define INF 100000000
#define fi first
#define se second
const int LEN = 2e5 + 5;
const int LEN5 = 1e5 + 5;
const int MOD = 1e9 + 7;
typedef long long ll;
int n, m;
int dp[LEN5];
vector<int> adj[LEN5];
void sol(int x) {
  dp[x] = 0;
  for (auto v : adj[x]) {
    sol(v);
    dp[x] = max(dp[x], 1 + dp[v]);
  }
}
int main() {

  // freopen ("myfile.txt","r",stdin);
  // freopen ("myfile.txt","w",stdout);

  // ios_base::sync_with_stdio(0); cin.tie(NULL);
  cin >> n >> m;
  int x, y;
  for (int i = 0; i < m; i++) {
    scanf("%d%d", &x, &y);
    adj[x].push_back(y);
  }
  for (int i = 0; i < LEN5; i++)
    dp[i] = -1;
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (dp[i] == -1) {
      sol(i);
    }
    ans = max(ans, dp[i]);
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define INF 100000000
#define fi first
#define se second
const int LEN = 2e5 + 5;
const int LEN5 = 1e5 + 5;
const int MOD = 1e9 + 7;
typedef long long ll;
int n, m;
int dp[LEN5];
vector<int> adj[LEN5];
void sol(int x) {
  if (dp[x] != -1)
    return;
  dp[x] = 0;
  for (auto v : adj[x]) {
    sol(v);
    dp[x] = max(dp[x], 1 + dp[v]);
  }
}
int main() {

  // freopen ("myfile.txt","r",stdin);
  // freopen ("myfile.txt","w",stdout);

  // ios_base::sync_with_stdio(0); cin.tie(NULL);
  cin >> n >> m;
  int x, y;
  for (int i = 0; i < m; i++) {
    scanf("%d%d", &x, &y);
    adj[x].push_back(y);
  }
  for (int i = 0; i < LEN5; i++)
    dp[i] = -1;
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (dp[i] == -1) {
      sol(i);
    }
    ans = max(ans, dp[i]);
  }
  cout << ans << endl;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
typedef long long ll;
#define _GLIBCXX_DEBUG
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(v) v.begin(), v.end()
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
const int INF = 1 << 30;
const ll INFll = 1ll << 60;
template <typename T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b; // aをbで更新
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b; // aをbで更新
    return true;
  }
  return false;
}

vi dp(1000000, -1);
vvi G(1000000);

ll rec(int v) {
  if (dp[v] >= 0) {
    return dp[v];
  }
  if (G[v].size() == 0) {
    return 0;
  }

  ll ret = -1;
  for (auto nv : G[v]) {
    ret = max(ret, rec(nv) + 1);
  }
  return ret;
}

int main() {
  int N, M;
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    G[a - 1].push_back(b - 1);
  }
  ll ans = -1;
  rep(i, N) { ans = max(ans, rec(i)); }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
typedef long long ll;
#define _GLIBCXX_DEBUG
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(v) v.begin(), v.end()
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
const int INF = 1 << 30;
const ll INFll = 1ll << 60;
template <typename T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b; // aをbで更新
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b; // aをbで更新
    return true;
  }
  return false;
}

vi dp(1000000, -1);
vvi G(1000000);

ll rec(int v) {
  if (dp[v] >= 0) {
    return dp[v];
  }
  if (G[v].size() == 0) {
    return 0;
  }

  ll ret = -1;
  for (auto nv : G[v]) {
    ret = max(ret, rec(nv) + 1);
  }
  return dp[v] = ret;
}

int main() {
  int N, M;
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    G[a - 1].push_back(b - 1);
  }
  ll ans = -1;
  rep(i, N) { ans = max(ans, rec(i)); }
  cout << ans << endl;
  return 0;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <fstream>
#include <iostream>
#include <vector>
using namespace std;
vector<long long> adj[1000005];
long long F[1000005], a, b, n, m, dd[1000005], res;
long long dfs(long long u) {
  for (long long v : adj[u])
    F[u] = max(F[u], dfs(v) + 1);
  return F[u];
}

int main() {
  // freopen("LONGEST.INP", "r", stdin);
  cin >> n >> m;
  for (long long i = 1; i <= m; i++) {
    cin >> a >> b;
    adj[a].push_back(b);
  }

  for (long long i = 1; i <= n; i++)
    if (!dd[i]) {
      dd[i] = 1;
      dfs(i);
    }

  for (long long i = 1; i <= n; i++)
    res = max(res, F[i]);
  cout << res;
  return 0;
}


Fixed submission:
#include <fstream>
#include <iostream>
#include <vector>
using namespace std;
vector<long long> adj[1000005];
long long F[1000005], a, b, n, m, dd[1000005], res;
long long dfs(long long u) {
  for (long long v : adj[u])
    if (!dd[v]) {
      dd[v] = 1;
      F[u] = max(F[u], dfs(v) + 1);
    } else
      F[u] = max(F[u], F[v] + 1);
  return F[u];
}

int main() {
  // freopen("LONGEST.INP", "r", stdin);
  cin >> n >> m;
  for (long long i = 1; i <= m; i++) {
    cin >> a >> b;
    adj[a].push_back(b);
  }

  for (long long i = 1; i <= n; i++)
    if (!dd[i]) {
      dd[i] = 1;
      dfs(i);
    }

  for (long long i = 1; i <= n; i++)
    res = max(res, F[i]);
  cout << res;
  return 0;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int solve(vector<int> *edges, int n, int i, int *dp) {
  int leaf = 1;
  int ans = 0;
  for (auto x : edges[i]) {
    leaf = 0;
    int t = solve(edges, n, x, dp);
    ans = max(ans, t);
  }
  if (leaf == 1) {
    return dp[i] = 0;
  }
  return dp[i] = 1 + ans;
}
int main() {
  int n, m;
  cin >> n >> m;
  vector<int> edges[n + 1];
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    edges[u].push_back(v);
  }
  int ans = INT_MIN;
  int dp[n + 1];
  memset(dp, -1, sizeof dp);
  for (int i = 1; i <= n; i++) {
    if (dp[i] != -1) {
      ans = max(ans, dp[i]);
    } else {
      ans = max(ans, solve(edges, n, i, dp));
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int solve(vector<int> *edges, int n, int i, int *dp) {
  int leaf = 1;
  int ans = 0;
  for (auto x : edges[i]) {
    leaf = 0;
    int t;
    if (dp[x] == -1) {
      t = solve(edges, n, x, dp);
    } else {
      t = dp[x];
    }
    ans = max(ans, t);
  }
  if (leaf == 1) {
    return dp[i] = 0;
  }
  return dp[i] = 1 + ans;
}
int main() {
  int n, m;
  cin >> n >> m;
  vector<int> edges[n + 1];
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    edges[u].push_back(v);
  }
  int ans = INT_MIN;
  int dp[n + 1];
  memset(dp, -1, sizeof dp);
  for (int i = 1; i <= n; i++) {
    if (dp[i] != -1) {
      ans = max(ans, dp[i]);
    } else {
      ans = max(ans, solve(edges, n, i, dp));
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03166 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <cmath>
#include <iostream>
#include <stdio.h>
#include <vector>
#define rep(i, n) for (ll i = 0; i < n; i++)
typedef long long ll;
using namespace std;

const ll MAX = 1000000;
const ll MOD = 1000000007;

ll fac[MAX];

// テーブルを作る前処理
/*
void COMinit() {
    fac[0] = fac[1] = 1;
    for (int i = 2; i < MAX; i++){
        fac[i] = fac[i - 1] * i % MOD;
    }
}
*/

ll pow_kai(ll a, ll n) { // aのn乗を計算します。
  ll x = 1;
  while (n > 0) { // 全てのbitが捨てられるまで。
    if (n & 1) {  // 1番右のbitが1のとき。
      x = x * a % MOD;
    }
    a = a * a % MOD;
    n >>= 1; // bit全体を右に1つシフトして一番右を捨てる。
  }
  return x;
}

// 二項係数計算
ll COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  ll tmp = 1;
  for (ll i = n; i >= n - k + 1; i--)
    tmp = (tmp * i) % MOD;

  return (tmp * pow_kai(fac[k], MOD - 2)) % MOD;
}
ll N, M;
ll dp[100001];
vector<vector<ll>> G;

void chmax(ll &a, const ll &b) {
  if (a < b)
    a = b;
}
void chmin(ll &a, const ll &b) {
  if (a > b)
    a = b;
}

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;
  G.assign(N + 1, vector<ll>());

  ll a, b;

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

  rep(i, N + 1) dp[i] = -1;

  ll ans = 0;

  for (ll v = 1; v <= N; v++) {
    chmax(ans, rec(v));
  }

  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#include <cmath>
#include <iostream>
#include <stdio.h>
#include <vector>
#define rep(i, n) for (ll i = 0; i < n; i++)
typedef long long ll;
using namespace std;

const ll MAX = 1000000;
const ll MOD = 1000000007;

ll fac[MAX];

// テーブルを作る前処理
/*
void COMinit() {
    fac[0] = fac[1] = 1;
    for (int i = 2; i < MAX; i++){
        fac[i] = fac[i - 1] * i % MOD;
    }
}
*/

ll pow_kai(ll a, ll n) { // aのn乗を計算します。
  ll x = 1;
  while (n > 0) { // 全てのbitが捨てられるまで。
    if (n & 1) {  // 1番右のbitが1のとき。
      x = x * a % MOD;
    }
    a = a * a % MOD;
    n >>= 1; // bit全体を右に1つシフトして一番右を捨てる。
  }
  return x;
}

// 二項係数計算
ll COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  ll tmp = 1;
  for (ll i = n; i >= n - k + 1; i--)
    tmp = (tmp * i) % MOD;

  return (tmp * pow_kai(fac[k], MOD - 2)) % MOD;
}
ll N, M;
ll dp[100001];
vector<vector<ll>> G;

void chmax(ll &a, const ll &b) {
  if (a < b)
    a = b;
}
void chmin(ll &a, const ll &b) {
  if (a > b)
    a = b;
}

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;
  G.assign(N + 1, vector<ll>());

  ll a, b;

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

  rep(i, N + 1) dp[i] = -1;

  ll ans = 0;

  for (ll v = 1; v <= N; v++) {
    chmax(ans, rec(v));
  }

  cout << ans << endl;

  return 0;
}
Problem p03166 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
vector<int> g[100];
int dp[100];
int f(int src) {
  if (dp[src] != -1)
    return dp[src];
  int ans = 0;
  int flag = 0;
  for (auto i : g[src]) {
    flag = 1;
    ans = max(ans, f(i));
  }
  return dp[src] = flag == 1 ? ans + 1 : 0;
}
int main() {
  memset(dp, -1, sizeof(dp));
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    g[x].push_back(y);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++)
    ans = max(ans, f(i));
  cout << ans;
}


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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
vector<int> graph[10000];
int dp[1000];
int lenof(int src) {
  bool leaf = 1;
  if (dp[src] != -1) {
    return dp[src];
  }
  int bestchild = 0;
  for (auto child : graph[src]) {
    leaf = 0;
    bestchild = max(bestchild, lenof(child));
  }
  return dp[src] = leaf ? 0 : 1 + bestchild;
}
int main() {
  memset(dp, -1, sizeof(dp));
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    graph[x].push_back(y);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    ans = max(ans, lenof(i));
  }
  cout << ans;
}


Fixed submission:
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
vector<int> graph[100001];
int dp[100001];
int lenof(int src) {
  bool leaf = 1;
  if (dp[src] != -1) {
    return dp[src];
  }
  int bestchild = 0;
  for (auto child : graph[src]) {
    leaf = 0;
    bestchild = max(bestchild, lenof(child));
  }
  return dp[src] = leaf ? 0 : 1 + bestchild;
}
int main() {
  memset(dp, -1, sizeof(dp));
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    graph[x].push_back(y);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    ans = max(ans, lenof(i));
  }
  cout << ans;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define all(v) v.begin(), v.end()
#define ff(i, x, n, k) for (int i = x; i < n; i += k)
#define rf(i, x, n, k) for (int i = n - 1; i >= x; i += k)
#define ef(i, x, n, k) for (int i = x; i <= n; i += k)
#define pb push_back
#define eb emplace_back
#define pp pop_back
#define mp make_pair
#define fast_cin()                                                             \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL)

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int MOD = 1e9 + 7;
const ll INF = pow(10, 18) + 5;

vector<vector<int>> graph(210000);
int maxPath[210000] = {0};
bool visit[210000] = {false};

int dfs(int n) {
  for (auto u : graph[n])
    maxPath[n] = max(maxPath[n], dfs(u) + 1);

  visit[n] = true;
  if (visit[n] == true) {
    return maxPath[n];
  }
}

int main() {
  fast_cin();
  int n, m;
  cin >> n >> m;

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

  int store = 0;
  for (int i = 1; i <= n; ++i) {
    store = dfs(i);
  }

  int MAX = 0;

  for (int i = 1; i <= n; ++i) {
    MAX = max(MAX, maxPath[i]);
  }

  cout << MAX << '\n';
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define all(v) v.begin(), v.end()
#define ff(i, x, n, k) for (int i = x; i < n; i += k)
#define rf(i, x, n, k) for (int i = n - 1; i >= x; i += k)
#define ef(i, x, n, k) for (int i = x; i <= n; i += k)
#define pb push_back
#define eb emplace_back
#define pp pop_back
#define mp make_pair
#define fast_cin()                                                             \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL)

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int MOD = 1e9 + 7;
const ll INF = pow(10, 18) + 5;

vector<vector<int>> graph(210000);
int maxPath[210000] = {0};
bool visit[210000] = {false};

int dfs(int n) {
  if (maxPath[n] != 0)
    return maxPath[n];

  for (auto u : graph[n])
    maxPath[n] = max(maxPath[n], dfs(u) + 1);

  visit[n] = true;
  if (visit[n] == true) {
    return maxPath[n];
  }
}

int main() {
  fast_cin();
  int n, m;
  cin >> n >> m;

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

  int store = 0;
  for (int i = 1; i <= n; ++i) {
    store = dfs(i);
  }

  int MAX = 0;

  for (int i = 1; i <= n; ++i) {
    MAX = max(MAX, maxPath[i]);
  }

  cout << MAX << '\n';
  return 0;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
// hail to jwalaji
#include <bits/stdc++.h>
using namespace std;
typedef long long int lli;
vector<vector<lli>> adj(100005, vector<lli>());
vector<lli> store(100005, -1);
lli dfs(lli u, lli parent = -1) {
  lli maxa = 1;
  lli storemax = 0;
  for (auto &v : adj[u]) {
    if (v == parent)
      continue;
    else {
      storemax = max(storemax, dfs(v, u));
    }
  }
  store[u] = maxa + storemax;
  return maxa + storemax;
}
void solve(lli n) {
  lli finalmax = 0;
  for (lli i = 1; i <= n; i++) {
    if (store[i] != -1) {
      finalmax = max(finalmax, store[i]);
    } else
      ;
    finalmax = max(finalmax, dfs(i, -1));
    // cout<<finalmax-1<<" "<<i<<" hello "<<endl;
  }
  cout << finalmax - 1 << endl;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  lli n, m;
  cin >> n >> m;
  for (lli i = 0; i < m; i++) {
    lli x, y;
    cin >> x >> y;
    adj[x].push_back(y);
  }
  solve(n);
  return 0;
}


Fixed submission:
// hail to jwalaji
#include <bits/stdc++.h>
using namespace std;
typedef long long int lli;
vector<vector<lli>> adj(100005, vector<lli>());
vector<lli> store(100005, -1);
lli dfs(lli u, lli parent = -1) {
  lli maxa = 1;
  lli storemax = 0;
  for (auto &v : adj[u]) {
    if (v == parent)
      continue;
    else {
      if (store[v] != -1) {
        storemax = max(storemax, store[v]);
      } else
        storemax = max(storemax, dfs(v, u));
    }
  }
  store[u] = maxa + storemax;
  return maxa + storemax;
}
void solve(lli n) {
  lli finalmax = 0;
  for (lli i = 1; i <= n; i++) {
    if (store[i] != -1) {
      finalmax = max(finalmax, store[i]);
    } else
      ;
    finalmax = max(finalmax, dfs(i, -1));
    // cout<<finalmax-1<<" "<<i<<" hello "<<endl;
  }
  cout << finalmax - 1 << endl;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  lli n, m;
  cin >> n >> m;
  for (lli i = 0; i < m; i++) {
    lli x, y;
    cin >> x >> y;
    adj[x].push_back(y);
  }
  solve(n);
  return 0;
}
Problem p03166 (C++)

Buggy submission (status: Runtime Error):
#define _USE_MATH_DEFINES
#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>

using ll = long long;

using namespace std;

ll mod = 1000000007;

ll n_pow(ll a, ll n) {
  if (n == 0)
    return 1;
  ll t = n_pow(a, n / 2);
  t *= t;
  t %= mod;
  if (n % 2 == 1)
    t *= a;
  return t % mod;
}

const int INF = 1001001001;
// const ll INF = 1e18;
vector<vector<int>> path(10050);

int rec(vector<int> &dp, int v) {
  if (dp[v] != -1)
    return dp[v];
  int res = 0;
  for (int p : path[v]) {
    res = max(res, rec(dp, p) + 1);
  }
  return dp[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;
    path[x].push_back(y);
  }
  vector<int> dp(n, -1);
  int ans = 0;
  for (int i = 0; i < n; i++) {
    ans = max(ans, rec(dp, i));
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#define _USE_MATH_DEFINES
#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>

using ll = long long;

using namespace std;

ll mod = 1000000007;

ll n_pow(ll a, ll n) {
  if (n == 0)
    return 1;
  ll t = n_pow(a, n / 2);
  t *= t;
  t %= mod;
  if (n % 2 == 1)
    t *= a;
  return t % mod;
}

const int INF = 1001001001;
// const ll INF = 1e18;
vector<vector<int>> path(100050);

int rec(vector<int> &dp, int v) {
  if (dp[v] != -1)
    return dp[v];
  int res = 0;
  for (int p : path[v]) {
    res = max(res, rec(dp, p) + 1);
  }
  return dp[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;
    path[x].push_back(y);
  }
  vector<int> dp(n, -1);
  int ans = 0;
  for (int i = 0; i < n; i++) {
    ans = max(ans, rec(dp, i));
  }
  cout << ans << endl;
  return 0;
}