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

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

#define int long long
#define ll long long
#define pb push_back
#define mp make_pair
#define f(i, a, n) for (int i = a; i < (int)n; i++)
#define rf(i, n, a) for (int i = n; i >= a; i--)
#define F first
#define S second
#define all(c) (c).begin(), (c).end()
#define sz(v) (int)(v).size()
#define d1(a) cout << a << "\n"
#define d2(a, b) cout << a << " " << b << "\n"
#define d3(a, b, c) cout << a << " " << b << " " << c << "\n"
#define d4(a, b, c, d) cout << a << " " << b << " " << c << " " << d << "\n"
#define fast ios::sync_with_stdio(false), cin.tie(nullptr);

typedef long double ld;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef pair<pii, int> ppi;
typedef vector<pii> vpi;

const int inf = 1e9;
const int inf64 = 1e18;
const int MOD = inf + 7;
const int N = 1e5 + 5;
vi g[N];
int dp[N];
bool vis[N];
int ans = 0;
void dfs(int node) {
  for (auto v : g[node]) {
    if (!vis[v]) {
      dfs(v);
    }
    dp[node] = max(dp[node], dp[v] + 1);
  }
}
int32_t main() {
  fast;
  int n, m;
  cin >> n >> m;
  f(i, 0, m) {
    int u, v;
    cin >> u >> v;
    g[u].pb(v);
  }
  f(i, 1, n + 1) if (!vis[i]) { dfs(i); }
  int ans = 0;
  f(i, 1, n + 1) ans = max(ans, dp[i]);
  cout << ans << "\n";
}

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

#define int long long
#define ll long long
#define pb push_back
#define mp make_pair
#define f(i, a, n) for (int i = a; i < (int)n; i++)
#define rf(i, n, a) for (int i = n; i >= a; i--)
#define F first
#define S second
#define all(c) (c).begin(), (c).end()
#define sz(v) (int)(v).size()
#define d1(a) cout << a << "\n"
#define d2(a, b) cout << a << " " << b << "\n"
#define d3(a, b, c) cout << a << " " << b << " " << c << "\n"
#define d4(a, b, c, d) cout << a << " " << b << " " << c << " " << d << "\n"
#define fast ios::sync_with_stdio(false), cin.tie(nullptr);

typedef long double ld;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef pair<pii, int> ppi;
typedef vector<pii> vpi;

const int inf = 1e9;
const int inf64 = 1e18;
const int MOD = inf + 7;
const int N = 1e5 + 5;
vi g[N];
int dp[N];
bool vis[N];
int ans = 0;
void dfs(int node) {
  vis[node] = true;
  for (auto v : g[node]) {
    if (!vis[v]) {
      dfs(v);
    }
    dp[node] = max(dp[node], dp[v] + 1);
  }
}
int32_t main() {
  fast;
  int n, m;
  cin >> n >> m;
  f(i, 0, m) {
    int u, v;
    cin >> u >> v;
    g[u].pb(v);
  }
  f(i, 1, n + 1) if (!vis[i]) { dfs(i); }
  int ans = 0;
  f(i, 1, n + 1) ans = max(ans, dp[i]);
  cout << ans << "\n";
}
Problem p03166 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int N = 1e4 + 10;
int memo[N];
bool vis[N];
vector<int> g[N];

int dp(int u) {
  if (vis[u])
    return memo[u];
  int ans = 0;
  for (auto v : g[u]) {
    ans = max(ans, dp(v) + 1);
  }
  vis[u] = true;
  return memo[u] = ans;
}

int main() {
  int n, m;
  cin >> n >> m;
  int a, b;
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    g[a].push_back(b);
  }
  int res = 0;
  for (int i = 1; i <= n; i++) {
    res = max(res, dp(i));
  }
  cout << res << "\n";
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int memo[N];
bool vis[N];
vector<int> g[N];

int dp(int u) {
  if (vis[u])
    return memo[u];
  int ans = 0;
  for (auto v : g[u]) {
    ans = max(ans, dp(v) + 1);
  }
  vis[u] = true;
  return memo[u] = ans;
}

int main() {
  int n, m;
  cin >> n >> m;
  int a, b;
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    g[a].push_back(b);
  }
  int res = 0;
  for (int i = 1; i <= n; i++) {
    res = max(res, dp(i));
  }
  cout << res << "\n";
  return 0;
}
Problem p03166 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
#define ll long long

using namespace std;

vector<int> dp(10010, 0);
vector<vector<int>> g(10010, vector<int>());
int N, M;

int dfs(int u) {
  if (dp[u])
    return dp[u];
  int cu = 0;
  for (int i = 0; i < g[u].size(); i++) {
    int to = g[u][i];
    int nxt = dfs(to);
    dp[u] = max(dp[u], dp[to] + 1);
  }
  return dp[u];
}
int main() {
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    int x, y;
    cin >> x >> y;
    g[x].push_back(y);
  }
  //  for( int i = 1 ; i <= N ; i++ ){
  //    cout << i << ":";
  //    for( int j = 0 ; j < g[i].size() ; j++ ){
  //      cout << g[i][j] << " " ;
  //    }
  //    cout << endl;
  //  }
  int ret = 0;
  for (int i = 1; i <= N; i++) {
    ret = max(ret, dfs(i));
  }
  cout << ret << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
#define ll long long

using namespace std;

vector<int> dp(100010, 0);
vector<vector<int>> g(100010, vector<int>());
int N, M;

int dfs(int u) {
  if (dp[u])
    return dp[u];
  int cu = 0;
  for (int i = 0; i < g[u].size(); i++) {
    int to = g[u][i];
    int nxt = dfs(to);
    dp[u] = max(dp[u], dp[to] + 1);
  }
  return dp[u];
}
int main() {
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    int x, y;
    cin >> x >> y;
    g[x].push_back(y);
  }
  //  for( int i = 1 ; i <= N ; i++ ){
  //    cout << i << ":";
  //    for( int j = 0 ; j < g[i].size() ; j++ ){
  //      cout << g[i][j] << " " ;
  //    }
  //    cout << endl;
  //  }
  int ret = 0;
  for (int i = 1; i <= N; i++) {
    ret = max(ret, dfs(i));
  }
  cout << ret << endl;
  return 0;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<vii> viii;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define repu(i, a, b) for (int i = a; i <= b; i++)
#define repd(i, b, a) for (int i = b; i >= a; i--)
#define pb push_back
#define db double
#define fi first
#define se second

void dfs(int u, vector<int> adj[], int dist[], bool vis[]) {
  vis[u] = true;
  rep(i, adj[u].size()) {
    if (!vis[adj[u][i]])
      dfs(adj[u][i], adj, dist, vis);
    dist[u] = max(dist[u], 1 + dist[adj[u][i]]);
  }
}

int longestPath(vector<int> adj[], int s, int n) {
  bool vis[n];
  int dist[n];
  memset(vis, false, sizeof vis);
  memset(dist, 0, sizeof dist);
  dist[s] = 0;
  stack<int> stk;
  stk.push(s);
  rep(i, n) if (!vis[i]) dfs(i, adj, dist, vis);
  int ma = 0;
  rep(i, n) ma = max(ma, dist[i]);
  return ma;
}

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> adj[n];
  rep(i, m) {
    int x, y;
    cin >> x >> y;
    x--, y--;
    adj[x].pb(y);
  }
  int ma = 0;
  rep(i, n) ma = max(ma, longestPath(adj, i, n));
  cout << ma;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<vii> viii;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define repu(i, a, b) for (int i = a; i <= b; i++)
#define repd(i, b, a) for (int i = b; i >= a; i--)
#define pb push_back
#define db double
#define fi first
#define se second

void dfs(int u, vector<int> adj[], int dist[], bool vis[]) {
  vis[u] = true;
  rep(i, adj[u].size()) {
    if (!vis[adj[u][i]])
      dfs(adj[u][i], adj, dist, vis);
    dist[u] = max(dist[u], 1 + dist[adj[u][i]]);
  }
}

int longestPath(vector<int> adj[], int s, int n) {
  bool vis[n];
  int dist[n];
  memset(vis, false, sizeof vis);
  memset(dist, 0, sizeof dist);
  dist[s] = 0;
  stack<int> stk;
  stk.push(s);
  rep(i, n) if (!vis[i]) dfs(i, adj, dist, vis);
  int ma = 0;
  rep(i, n) ma = max(ma, dist[i]);
  return ma;
}

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> adj[n];
  rep(i, m) {
    int x, y;
    cin >> x >> y;
    x--, y--;
    adj[x].pb(y);
  }
  cout << longestPath(adj, 0, n);
}
Problem p03166 (C++)

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

using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

#define FOR(k, m, n) for (ll(k) = (m); (k) < (n); (k)++)
#define REP(i, n) FOR((i), 0, (n))
#define WAITING(str)                                                           \
  int str;                                                                     \
  std::cin >> str;
#define DEBUGING(str) cout << #str << " " << str << endl

constexpr int INF = (1 << 30);
constexpr ll INFL = (1ll << 60);
constexpr ll MOD = 1000000007; // 10^9+7

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

int dfs(int now) {
  int res = 0;
  for (auto next : edges[now])
    res = max(res, dfs(next) + 1);
  dp[now] = res;
  return res;
}

// メイン関数
int main() {
  cin >> N >> M;
  edges.resize(N);
  dp.resize(N, -1);
  REP(i, M) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    edges[a].push_back(b);
  }

  REP(i, N) if (dp[i] == -1) dfs(i);

  cout << *max_element(dp.begin(), dp.end()) << endl;
  cin >> N;

  return 0;
}


Fixed submission:
#include "bits/stdc++.h"

using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

#define FOR(k, m, n) for (ll(k) = (m); (k) < (n); (k)++)
#define REP(i, n) FOR((i), 0, (n))
#define WAITING(str)                                                           \
  int str;                                                                     \
  std::cin >> str;
#define DEBUGING(str) cout << #str << " " << str << endl

constexpr int INF = (1 << 30);
constexpr ll INFL = (1ll << 60);
constexpr ll MOD = 1000000007; // 10^9+7

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

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

// メイン関数
int main() {
  cin >> N >> M;
  edges.resize(N);
  dp.resize(N, -1);
  REP(i, M) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    edges[a].push_back(b);
  }

  REP(i, N) if (dp[i] == -1) dfs(i);

  cout << *max_element(dp.begin(), dp.end()) << endl;
  cin >> N;

  return 0;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (long long int i = 0; i < n; i++)
#define foi(i, a, n) for (long long int i = a; i < n; i++)
#define el "\n"

const int INF = 2e9 + 5;
const int MAX = 1e5 + 5;
using ll = long long int;
vector<int> vis(MAX, 0), dp(MAX, 0), deg(MAX, 0);
void dfs(vector<vector<int>> &gr, int node, int par) {
  vis[node] = 1;
  for (auto i : gr[node]) {
    if (i != par) {
      dp[i] = max(dp[i], dp[node] + 1);
      deg[i]--;
      if (deg[i] <= 0)
        dfs(gr, i, node);
    }
  }
}
void solve() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> gr(n + 1);
  rep(i, m) {
    int x, y;
    cin >> x >> y;
    x, y;
    gr[x].push_back(y);
    deg[y]++;
  }
  foi(i, 1, n + 1) {
    if (!vis[i])
      dfs(gr, i, 0);
  }
  cout << *max_element(dp.begin(), dp.end());
}

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

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (long long int i = 0; i < n; i++)
#define foi(i, a, n) for (long long int i = a; i < n; i++)
#define el "\n"

const int INF = 2e9 + 5;
const int MAX = 1e5 + 5;
using ll = long long int;
vector<int> vis(MAX, 0), dp(MAX, 0), deg(MAX, 0);
void dfs(vector<vector<int>> &gr, int node, int par) {
  vis[node] = 1;
  for (auto i : gr[node]) {
    if (i != par) {
      dp[i] = max(dp[i], dp[node] + 1);
      deg[i]--;
      if (deg[i] <= 0)
        dfs(gr, i, node);
    }
  }
}
void solve() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> gr(n + 1);
  rep(i, m) {
    int x, y;
    cin >> x >> y;
    x, y;
    gr[x].push_back(y);
    deg[y]++;
  }
  foi(i, 1, n + 1) {
    if (!vis[i] && deg[i] == 0)
      dfs(gr, i, 0);
  }
  cout << *max_element(dp.begin(), dp.end());
}

int main() {
  solve();
  return 0;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
// mangesh2102000

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

#define IOS                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
#define int long long int
#define ld long double
#define pb push_back
#define tr(c, it) for (auto it = (c).begin(); it != (c).end(); ++it)
#define F first
#define S second
#define mod 1000000007

int n, m;
vector<int> g[100005];
int dp[100005];
bool vis[100005];

void dfs(int node) {
  for (int i = 0; i < g[node].size(); i++) {
    if (!vis[g[node][i]])
      dfs(g[node][i]);

    dp[node] = max(dp[node], 1 + dp[g[node][i]]);
  }
}

void solve(int t) {
  cin >> n >> m;
  int x, y;
  for (int i = 0; i < m; i++) {
    cin >> x >> y;
    g[x].pb(y);
  }

  memset(dp, 0, sizeof(dp));
  memset(vis, false, sizeof(vis));

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

  int ans = -1;
  for (int i = 1; i <= n; i++) {
    // cout << i << "-" << dp[i] << "\n";
    ans = max(ans, dp[i]);
  }

  cout << ans;

  // cout << "Case #" << t << ": ";
}

int32_t main() {
  IOS;
  int t;
  t = 1;
  for (int i = 1; i <= t; i++) {
    solve(i);
  }

  return 0;
}

Fixed submission:
// mangesh2102000

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

#define IOS                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
#define int long long int
#define ld long double
#define pb push_back
#define tr(c, it) for (auto it = (c).begin(); it != (c).end(); ++it)
#define F first
#define S second
#define mod 1000000007

int n, m;
vector<int> g[100005];
int dp[100005];
bool vis[100005];

void dfs(int node) {
  vis[node] = true;
  for (int i = 0; i < g[node].size(); i++) {
    if (!vis[g[node][i]])
      dfs(g[node][i]);

    dp[node] = max(dp[node], 1 + dp[g[node][i]]);
  }
}

void solve(int t) {
  cin >> n >> m;
  int x, y;
  for (int i = 0; i < m; i++) {
    cin >> x >> y;
    g[x].pb(y);
  }

  memset(dp, 0, sizeof(dp));
  memset(vis, false, sizeof(vis));

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

  int ans = -1;
  for (int i = 1; i <= n; i++) {
    // cout << i << "-" << dp[i] << "\n";
    ans = max(ans, dp[i]);
  }

  cout << ans;

  // cout << "Case #" << t << ": ";
}

int32_t main() {
  IOS;
  int t;
  t = 1;
  for (int i = 1; i <= t; i++) {
    solve(i);
  }

  return 0;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
using namespace std;
using ll = int64_t;
using vi = vector<int>;
using vvi = vector<vi>;

int n, m;
vvi G;
vi check;

int dfs(int i) {
  if (check[i] != -1) {
    return check[i];
  }
  if (!G[i].size()) {
    check[i] = 0;
    return 0;
  }
  int ma = 0;
  for (int nexti : G[i]) {
    ma = max(ma, dfs(nexti));
  }
  ma++;
  return ma;
}

int main() {

  cin >> n >> m;
  vi G_(n);
  G.resize(n);
  rep(i, m) {
    int x, y;
    cin >> x >> y;
    G[x - 1].push_back(y - 1);
    G_[y - 1]++;
  }

  check.assign(n, -1);

  int ma = 0;
  rep(i, n) {
    if (G_[i])
      continue;
    ma = max(ma, dfs(i));
  }

  cout << ma << '\n';
}

Fixed submission:
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
using namespace std;
using ll = int64_t;
using vi = vector<int>;
using vvi = vector<vi>;

int n, m;
vvi G;
vi check;

int dfs(int i) {
  if (check[i] != -1) {
    return check[i];
  }
  if (!G[i].size()) {
    check[i] = 0;
    return 0;
  }
  int ma = 0;
  for (int nexti : G[i]) {
    ma = max(ma, dfs(nexti));
  }
  ma++;
  check[i] = ma;
  return ma;
}

int main() {

  cin >> n >> m;
  vi G_(n);
  G.resize(n);
  rep(i, m) {
    int x, y;
    cin >> x >> y;
    G[x - 1].push_back(y - 1);
    G_[y - 1]++;
  }

  check.assign(n, -1);

  int ma = 0;
  rep(i, n) {
    if (G_[i])
      continue;
    ma = max(ma, dfs(i));
  }

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

Buggy submission (status: Runtime Error):
// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// #pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#define ll long long int
#define ld long double
#define mp make_pair
#define pi pair<int, int>
#define pl pair<long long int, long long int>
#define pb push_back
#define fi first
#define se second
#define len(a) (int)(a).length()
#define sz(a) (int)(a).size()

using namespace std;

const int N = 1e5;
int n, m, x, y, dp[N] = {0}, ans = -1;
vector<int> adj[N];
bool vis[N];

void dfs(int x) {
  vis[x] = true;
  for (int i = 0; i < sz(adj[x]); ++i) {
    if (!vis[adj[x][i]])
      dfs(adj[x][i]);
    dp[x] = max(dp[x], 1 + dp[adj[x][i]]);
    ans = max(ans, dp[x]);
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    cin >> x >> y;
    adj[x].pb(y);
  }
  for (int i = 1; i <= n; ++i)
    vis[i] = false;
  for (int i = 1; i <= n; ++i)
    if (!vis[i])
      dfs(i);
  cout << ans << endl;
  return 0;
}


Fixed submission:
// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// #pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#define ll long long int
#define ld long double
#define mp make_pair
#define pi pair<int, int>
#define pl pair<long long int, long long int>
#define pb push_back
#define fi first
#define se second
#define len(a) (int)(a).length()
#define sz(a) (int)(a).size()

using namespace std;

const int N = 1e5 + 5;
int n, m, x, y, dp[N] = {0}, ans = -1;
vector<int> adj[N];
bool vis[N];

void dfs(int x) {
  vis[x] = true;
  for (int i = 0; i < sz(adj[x]); ++i) {
    if (!vis[adj[x][i]])
      dfs(adj[x][i]);
    dp[x] = max(dp[x], 1 + dp[adj[x][i]]);
    ans = max(ans, dp[x]);
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    cin >> x >> y;
    adj[x].pb(y);
  }
  for (int i = 1; i <= n; ++i)
    vis[i] = false;
  for (int i = 1; i <= n; ++i)
    if (!vis[i])
      dfs(i);
  cout << ans << endl;
  return 0;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
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;
}
const long long INF = 1LL << 60;

int dp[100010];
Graph G;

int rec(int v) {
  // 到達済みならdp[v]を返す
  if (dp[v] != -1)
    return dp[v];

  // 0で初期化(vから出る辺が無ければ0となる)
  int res = 0;
  // 範囲for文
  for (auto nv : G[v]) {
    chmax(res, rec(nv) + 1);
  }
  return res;
}

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

  // N×0の配列で初期化(重要)
  G.assign(N, vector<int>());
  int x, y;
  rep(i, M) {
    cin >> x >> y;
    x--;
    y--; // 0から始まる添字にする
    G[x].push_back(y);
  }

  // 未到達(-1)で初期化
  rep(i, N) dp[i] = -1;

  // すべての頂点iについて,iから始まる最長の部分列を調べてその長さを更新
  int ans = 0;
  rep(i, N) { chmax(ans, rec(i)); }
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
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;
}
const long long INF = 1LL << 60;

int dp[100010];
Graph G;

int rec(int v) {
  // 到達済みならdp[v]を返す
  if (dp[v] != -1)
    return dp[v];

  // 0で初期化(vから出る辺が無ければ0となる)
  int res = 0;
  // 範囲for文
  for (auto nv : G[v]) {
    chmax(res, rec(nv) + 1);
  }
  // dp[v]を更新してそれを返す
  dp[v] = res;
  return dp[v];
}

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

  // N×0の配列で初期化(重要)
  G.assign(N, vector<int>());
  int x, y;
  rep(i, M) {
    cin >> x >> y;
    x--;
    y--; // 0から始まる添字にする
    G[x].push_back(y);
  }

  // 未到達(-1)で初期化
  rep(i, N) dp[i] = -1;

  // すべての頂点iについて,iから始まる最長の部分列を調べてその長さを更新
  int ans = 0;
  rep(i, N) { chmax(ans, rec(i)); }
  cout << ans << endl;
}
Problem p03166 (C++)

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

using namespace std;

template <typename T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <typename T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N, M;
  cin >> N >> M;
  vector<int> deg(N, 0);
  vector<vector<int>> G(N);
  for (int i = 0; i < M; i++) {
    int x, y;
    cin >> x >> y;
    x--, y--;
    G[x].emplace_back(y);
    deg[y]++;
  }

  queue<int> que;
  for (int i = 0; i < N; i++) {
    if (deg[i] == 0)
      que.push(i);
  }

  vector<int> dp(N, 0);
  while (!que.empty()) {
    int u = que.front();
    que.pop();

    for (int v : G[u]) {
      dp[v] = dp[u] + 1;
      que.push(v);
    }
  }

  cout << *max_element(dp.begin(), dp.end()) << "\n";
  return 0;
}


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

using namespace std;

template <typename T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <typename T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N, M;
  cin >> N >> M;
  vector<int> deg(N, 0);
  vector<vector<int>> G(N);
  for (int i = 0; i < M; i++) {
    int x, y;
    cin >> x >> y;
    x--, y--;
    G[x].emplace_back(y);
    deg[y]++;
  }

  queue<int> que;
  for (int i = 0; i < N; i++) {
    if (deg[i] == 0)
      que.push(i);
  }

  vector<int> dp(N, 0);
  while (!que.empty()) {
    int u = que.front();
    que.pop();

    for (int v : G[u]) {
      if (--deg[v] != 0)
        continue;
      dp[v] = dp[u] + 1;
      que.push(v);
    }
  }

  cout << *max_element(dp.begin(), dp.end()) << "\n";
  return 0;
}
Problem p03166 (C++)

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

using namespace std;

vector<vector<int>> G;
vector<int> memo;

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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N, M;
  cin >> N >> M;
  G.resize(N), memo.resize(N, -1);
  for (int i = 0; i < M; i++) {
    int x, y;
    cin >> x >> y;
    x--, y--;
    G[x].emplace_back(y);
  }
  int res = 0;
  for (int i = 0; i < N; i++) {
    res = max(res, dp(i));
  }
  cout << res << '\n';
  return 0;
}


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

using namespace std;

vector<vector<int>> G;
vector<int> memo;

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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N, M;
  cin >> N >> M;
  G.resize(N), memo.resize(N, -1);
  for (int i = 0; i < M; i++) {
    int x, y;
    cin >> x >> y;
    x--, y--;
    G[x].emplace_back(y);
  }
  int res = 0;
  for (int i = 0; i < N; i++) {
    res = max(res, dp(i));
  }
  cout << res << '\n';
  return 0;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):

// Problem : G - Longest Path
// Contest : Educational DP Contest
// URL : https://atcoder.jp/contests/dp/tasks/dp_g
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/coder3101/cp-editor)

#include <bits/stdc++.h>
using namespace std;
vector<int> adj[(int)1e5 + 5], rev[(int)1e5 + 5], indegree((int)1e5 + 5);
vector<int> dp((int)1e5 + 5), v((int)1e5 + 5);
void dfs(int i) {
  v[i] = 1;
  for (auto u : rev[i]) {
    dfs(u);
    dp[i] = max(dp[i], dp[u] + 1);
  }
}
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    adj[x - 1].push_back(y - 1);
    rev[y - 1].push_back(x - 1);
    ++indegree[y - 1];
  }
  for (int i = 0; i < n; i++) {
    if (!v[i]) {
      dfs(i);
    }
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    ans = max(ans, dp[i]);
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:

// Problem : G - Longest Path
// Contest : Educational DP Contest
// URL : https://atcoder.jp/contests/dp/tasks/dp_g
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/coder3101/cp-editor)

#include <bits/stdc++.h>
using namespace std;
vector<int> adj[(int)1e5 + 5], rev[(int)1e5 + 5], indegree((int)1e5 + 5);
vector<int> dp((int)1e5 + 5), v((int)1e5 + 5);
void dfs(int i) {
  v[i] = 1;
  for (auto u : rev[i]) {
    if (!v[u])
      dfs(u);
    dp[i] = max(dp[i], dp[u] + 1);
  }
}
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    adj[x - 1].push_back(y - 1);
    rev[y - 1].push_back(x - 1);
    ++indegree[y - 1];
  }
  for (int i = 0; i < n; i++) {
    if (!v[i]) {
      dfs(i);
    }
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    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;

typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;

const int MAXN = 10101;
vector<int> AdList[MAXN];
int n, m, dp[MAXN];

int maxdfs(int i) {
  if (dp[i] != -1)
    return dp[i];
  int mpath = 0;
  for (auto u : AdList[i]) {
    mpath = max(mpath, 1 + maxdfs(u));
  }
  dp[i] = mpath;
  return dp[i];
}
int main() {
  //    #ifdef Javier
  //        freopen(".in", "r", stdin);
  //    #endif // Javier

  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

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

  int sol = INT_MIN;
  fill(dp, dp + n + 1, -1);
  for (int i = 1; i <= n; i++) {
    if (dp[i] != -1) {
      continue;
    } else {
      dp[i] = maxdfs(i);
      sol = max(dp[i], sol);
    }
  }

  cout << sol;

  return 0;
}


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

typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;

const int MAXN = 101010;
vector<int> AdList[MAXN];
int n, m, dp[MAXN];

int maxdfs(int i) {
  if (dp[i] != -1)
    return dp[i];
  int mpath = 0;
  for (auto u : AdList[i]) {
    mpath = max(mpath, 1 + maxdfs(u));
  }
  dp[i] = mpath;
  return dp[i];
}
int main() {
  //    #ifdef Javier
  //        freopen(".in", "r", stdin);
  //    #endif // Javier

  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

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

  int sol = INT_MIN;
  fill(dp, dp + n + 1, -1);
  for (int i = 1; i <= n; i++) {
    if (dp[i] != -1) {
      continue;
    } else {
      dp[i] = maxdfs(i);
      sol = max(dp[i], sol);
    }
  }

  cout << sol;

  return 0;
}
Problem p03166 (C++)

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

const int mx = 1e5 + 5;

struct Edge {
  int to;
  int nxt;
} edges[mx];

int head[mx], depth[mx], tot;

void AddEdge(int u, int v) {
  edges[tot].to = v;
  edges[tot].nxt = head[u];
  head[u] = tot++;
}

int DFS(int cur) {
  if (depth[cur])
    return depth[cur];

  //    printf("[DFS] cur : %d\n", cur);
  int ret = 0;
  for (int i = head[cur]; i != -1; i = edges[i].nxt) {
    ret = max(ret, 1 + DFS(edges[i].to));
  }
  depth[cur] = ret;
  return ret;
}

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

  int N, M;
  scanf("%d%d", &N, &M);
  for (int i = 0; i < M; ++i) {
    int xi, yi;
    scanf("%d%d", &xi, &yi);
    AddEdge(xi, yi);
  }
  for (int i = 1; i <= N; ++i)
    AddEdge(0, i);

  DFS(0);
  printf("%d\n", depth[0] - 1);
  return 0;
}

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

const int mx = 1e5 + 5;

struct Edge {
  int to;
  int nxt;
} edges[2 * mx]; // virtual vertex

int head[mx], depth[mx], tot;

void AddEdge(int u, int v) {
  edges[tot].to = v;
  edges[tot].nxt = head[u];
  head[u] = tot++;
}

int DFS(int cur) {
  if (depth[cur])
    return depth[cur];

  //    printf("[DFS] cur : %d\n", cur);
  int ret = 0;
  for (int i = head[cur]; i != -1; i = edges[i].nxt) {
    ret = max(ret, 1 + DFS(edges[i].to));
  }
  depth[cur] = ret;
  return ret;
}

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

  int N, M;
  scanf("%d%d", &N, &M);
  for (int i = 0; i < M; ++i) {
    int xi, yi;
    scanf("%d%d", &xi, &yi);
    AddEdge(xi, yi);
  }
  for (int i = 1; i <= N; ++i)
    AddEdge(0, i);

  DFS(0);
  printf("%d\n", depth[0] - 1);
  return 0;
}
Problem p03166 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <memory>
#include <stack>
#include <string>
#include <vector>

using namespace std;

int dfs(int n, vector<vector<int>> &adjl, vector<int> &dp) {
  if (dp[n] != -1)
    return dp[n];
  int &ret = dp[n];
  ret = 0;
  for (int i = 0; i < adjl[n].size(); i++)
    ret = max(1 + dfs(adjl[n][i], adjl, dp), ret);
  return ret;
}

void LongPath() {
  int n, e;
  cin >> n >> e;
  vector<vector<bool>> adj(n + 1, vector<bool>(n + 1));
  vector<vector<int>> adjl(n + 1);
  vector<int> dp(n + 1, -1);
  int _max = 0;
  for (int i = 0; i < e; i++) {
    int a, b;
    cin >> a >> b;
    adjl[a].push_back(b);
  }
  for (int i = 1; i <= n; i++) {
    _max = max(_max, dfs(i, adjl, dp));
  }
  cout << _max;
}

int main() {
  LongPath();
  return 0;
}


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

using namespace std;

int dfs(int n, vector<vector<int>> &adjl, vector<int> &dp) {
  if (dp[n] != -1)
    return dp[n];
  int &ret = dp[n];
  ret = 0;
  for (int i = 0; i < adjl[n].size(); i++)
    ret = max(1 + dfs(adjl[n][i], adjl, dp), ret);
  return ret;
}

void LongPath() {
  int n, e;
  cin >> n >> e;
  vector<vector<int>> adjl(n + 1);
  vector<int> dp(n + 1, -1);
  int _max = 0;
  for (int i = 0; i < e; i++) {
    int a, b;
    cin >> a >> b;
    adjl[a].push_back(b);
  }
  for (int i = 1; i <= n; i++) {
    _max = max(_max, dfs(i, adjl, dp));
  }
  cout << _max;
}

int main() {
  LongPath();
  return 0;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
// Bismillahirrahmanirrahim
// █▀█─█──█──█▀█─█─█
// █▄█─█──█──█▄█─█▄█
// █─█─█▄─█▄─█─█─█─█

#pragma GCC optimize("O3")
#pragma GCC target("sse4")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx2")

#include <bits/stdc++.h>

using namespace std;

typedef long long lo;
typedef pair<lo, lo> PII;

#define fi first
#define se second
#define mp make_pair
#define endl "\n"
#define pb push_back
#define fio()                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
#define FOR for (int i = 1; i <= n; i++)
#define mid ((start + end) / 2)
#define ort ((bas + son) / 2)

const lo inf = 1000000000000000000;
const lo KOK = 100000;
const lo LOG = 30;
const lo li = 500005;
const lo mod = 1000000007;

int n, m, b[li], a[li], k, flag, t, dp[li];
int cev;
string s;
vector<int> v[li];

inline int dfs(int node) {
  int cevv = 0;
  if (v[node].size() == 0)
    return 0;
  if (~dp[node])
    return dp[node];
  for (int i = 0; i < (int)v[node].size(); i++) {
    cevv = max(cevv, dfs(v[node][i]) + 1);
  }
  return cevv;
}

int main(void) {
  memset(dp, -1, sizeof(dp));
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= m; i++) {
    int x, y;
    scanf("%d %d", &x, &y);
    v[x].pb(y);
  }
  FOR { cev = max(cev, dfs(i)); }
  printf("%d\n", cev);
  return 0;
}


Fixed submission:
// Bismillahirrahmanirrahim
// █▀█─█──█──█▀█─█─█
// █▄█─█──█──█▄█─█▄█
// █─█─█▄─█▄─█─█─█─█

#pragma GCC optimize("O3")
#pragma GCC target("sse4")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx2")

#include <bits/stdc++.h>

using namespace std;

typedef long long lo;
typedef pair<lo, lo> PII;

#define fi first
#define se second
#define mp make_pair
#define endl "\n"
#define pb push_back
#define fio()                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
#define FOR for (int i = 1; i <= n; i++)
#define mid ((start + end) / 2)
#define ort ((bas + son) / 2)

const lo inf = 1000000000000000000;
const lo KOK = 100000;
const lo LOG = 30;
const lo li = 500005;
const lo mod = 1000000007;

int n, m, b[li], a[li], k, flag, t, dp[li];
int cev;
string s;
vector<int> v[li];

inline int dfs(int node) {
  int cevv = 0;
  if (v[node].size() == 0)
    return 0;
  if (~dp[node])
    return dp[node];
  for (int i = 0; i < (int)v[node].size(); i++) {
    cevv = max(cevv, dfs(v[node][i]) + 1);
  }
  return dp[node] = cevv;
}

int main(void) {
  memset(dp, -1, sizeof(dp));
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= m; i++) {
    int x, y;
    scanf("%d %d", &x, &y);
    v[x].pb(y);
  }
  FOR { cev = max(cev, dfs(i)); }
  printf("%d\n", cev);
  return 0;
}
Problem p03166 (C++)

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

  for (int i = 0; i < m; i++) {
    cin >> u >> v;
    u--;
    v--;
    g[u].push_back(v);
  }
  for (int i = 0; i < n; i++) {
    dp[i] = -1;
  }
  int ret = 0;
  for (int i = 0; i < n; i++) {
    ret = max(ret, solve(i));
  }
  cout << ret << "\n";
}

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

  for (int i = 0; i < m; i++) {
    cin >> u >> v;
    u--;
    v--;
    g[u].push_back(v);
  }
  for (int i = 0; i < n; i++) {
    dp[i] = -1;
  }
  int ret = 0;
  for (int i = 0; i < n; i++) {
    ret = max(ret, solve(i));
  }
  cout << ret << "\n";
}
Problem p03166 (C++)

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

using namespace std;

typedef long long ll;
typedef long double ld;

const int N = (int)1e5 + 5;

int n, m;
vector<int> g[N];
int dp[N];

void calc(int nod) {
  dp[nod] = 1;
  for (auto &nou : g[nod]) {
    calc(nou);
    dp[nod] = max(dp[nod], 1 + dp[nou]);
  }
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int a, b;
    cin >> a >> b;
    g[a].push_back(b);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (dp[i] == 0) {
      calc(i);
    }
    ans = max(ans, dp[i]);
    ///   cout<<i<<" : "<<dp[i]<<"\n";
  }
  ans--;
  cout << ans << "\n";
  return 0;
}


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

using namespace std;

typedef long long ll;
typedef long double ld;

const int N = (int)1e5 + 5;

int n, m;
vector<int> g[N];
int dp[N];

void calc(int nod) {
  dp[nod] = 1;
  for (auto &nou : g[nod]) {
    if (dp[nou] == 0)
      calc(nou);
    dp[nod] = max(dp[nod], 1 + dp[nou]);
  }
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int a, b;
    cin >> a >> b;
    g[a].push_back(b);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (dp[i] == 0) {
      calc(i);
    }
    ans = max(ans, dp[i]);
    ///   cout<<i<<" : "<<dp[i]<<"\n";
  }
  ans--;
  cout << ans << "\n";
  return 0;
}
Problem p03166 (C++)

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

using namespace std;

#define ll long long
ll input() {
  ll x = 0, f = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9')
    f |= ch == '-', ch = getchar();
  while (ch >= '0' && ch <= '9')
    x = x * 10 + ch - '0', ch = getchar();
  return f ? -x : x;
}

#define ll long long
const int N = 1e5 + 7;

vector<int> G[N];
int n, m;
stack<int> s;
int d[N], dis[N];
int Ans = 0;

void bfs() {
  queue<int> q;
  for (int i = 1; i <= n; i++) {
    if (!d[i])
      q.push(i);
  }
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (auto v : G[u]) {
      if (dis[v] < dis[u] + 1) {
        q.push(v);
        dis[v] = dis[u] + 1;
      }
    }
  }
}

int main() {
  n = input(), m = input();
  for (int i = 1; i <= m; i++) {
    int u = input(), v = input();
    G[u].push_back(v);
    d[v]++;
  }
  bfs();
  for (int i = 1; i <= n; i++) {
    Ans = max(Ans, dis[i]);
  }
  printf("%d\n", Ans);
}

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

using namespace std;

#define ll long long
ll input() {
  ll x = 0, f = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9')
    f |= ch == '-', ch = getchar();
  while (ch >= '0' && ch <= '9')
    x = x * 10 + ch - '0', ch = getchar();
  return f ? -x : x;
}

#define ll long long
const int N = 1e5 + 7;

vector<int> G[N];
int n, m;
stack<int> s;
int d[N], dis[N];
int Ans = 0;

void bfs() {
  queue<int> q;
  for (int i = 1; i <= n; i++) {
    if (!d[i])
      q.push(i);
  }
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (auto v : G[u]) {
      d[v]--;
      if (d[v] == 0)
        dis[v] = dis[u] + 1, q.push(v);
    }
  }
}

int main() {
  n = input(), m = input();
  for (int i = 1; i <= m; i++) {
    int u = input(), v = input();
    G[u].push_back(v);
    d[v]++;
  }
  bfs();
  for (int i = 1; i <= n; i++) {
    Ans = max(Ans, dis[i]);
  }
  printf("%d\n", Ans);
}
Problem p03166 (C++)

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

const int maxn = 1e5 + 10;
int N, M;
vector<int> g[maxn];
int DP[maxn];
bool vis[maxn];

void DFS(int v, int p) {
  DP[v] = 1;
  for (int u : g[v]) {
    if (u != p) {
      DFS(u, v);
      DP[v] = max(DP[v], DP[u] + 1);
    }
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> N >> M;
  for (int i = 1; i <= M; ++i) {
    int x, y;
    cin >> x >> y;
    g[x].push_back(y);
  }
  for (int i = 1; i <= N; ++i) {
    if (!DP[i])
      DFS(i, -1);
  }
  int ret = 0;
  for (int i = 1; i <= N; ++i) {
    ret = max(ret, DP[i]);
  }
  cout << ret - 1 << endl;
}


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

const int maxn = 1e5 + 10;
int N, M;
vector<int> g[maxn];
int DP[maxn];
bool vis[maxn];

void DFS(int v, int p) {
  DP[v] = 1;
  for (int u : g[v]) {
    if (u != p) {
      if (!DP[u])
        DFS(u, v);
      DP[v] = max(DP[v], DP[u] + 1);
    }
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> N >> M;
  for (int i = 1; i <= M; ++i) {
    int x, y;
    cin >> x >> y;
    g[x].push_back(y);
  }
  for (int i = 1; i <= N; ++i) {
    if (!DP[i])
      DFS(i, -1);
  }
  int ret = 0;
  for (int i = 1; i <= N; ++i) {
    ret = max(ret, DP[i]);
  }
  cout << ret - 1 << endl;
}
Problem p03166 (C++)

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

using namespace std;

void traverse(vector<int> &distances, vector<vector<int>> &adjList,
              vector<bool> visited, int node) {
  visited[node] = true;
  for (int i = 0; i < adjList[node].size(); i++) {
    if (!visited[adjList[node][i]])
      traverse(distances, adjList, visited, adjList[node][i]);
    distances[node] = max(distances[node], 1 + distances[adjList[node][i]]);
  }
}

int main() {
  int N, M;
  int maxP = INT_MIN;
  cin >> N >> M;
  vector<vector<int>> adjList(N);
  vector<int> distances(N, 0);
  vector<bool> visited(N, false);
  for (int i = 0; i < M; i++) {
    pair<int, int> e;
    cin >> e.first >> e.second;
    adjList[e.first - 1].push_back(e.second - 1);
  }
  // for(int i=0;i<N-1;i++){
  //   for(int j=0;j<M;j++){
  //     pair<int,int> e = edges[j];
  //     int src = e.first;
  //     int dist = e.second;
  //     distances[dist-1] = max(distances[dist-1],1+distances[src-1]);
  //   }
  // }
  for (int i = 0; i < N; i++) {
    if (!visited[i])
      traverse(distances, adjList, visited, i);
  }
  for (int i = 0; i < N; i++) {
    //
    maxP = max(maxP, distances[i]);
  }

  cout << maxP;

  return 0;
}


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

using namespace std;

void traverse(vector<int> &distances, vector<vector<int>> &adjList,
              vector<bool> &visited, int node) {
  visited[node] = true;
  for (int i = 0; i < adjList[node].size(); i++) {
    if (!visited[adjList[node][i]])
      traverse(distances, adjList, visited, adjList[node][i]);
    distances[node] = max(distances[node], 1 + distances[adjList[node][i]]);
  }
}

int main() {
  int N, M;
  int maxP = INT_MIN;
  cin >> N >> M;
  vector<vector<int>> adjList(N);
  vector<int> distances(N, 0);
  vector<bool> visited(N, false);
  for (int i = 0; i < M; i++) {
    pair<int, int> e;
    cin >> e.first >> e.second;
    adjList[e.first - 1].push_back(e.second - 1);
  }
  // for(int i=0;i<N-1;i++){
  //   for(int j=0;j<M;j++){
  //     pair<int,int> e = edges[j];
  //     int src = e.first;
  //     int dist = e.second;
  //     distances[dist-1] = max(distances[dist-1],1+distances[src-1]);
  //   }
  // }
  for (int i = 0; i < N; i++) {
    if (!visited[i])
      traverse(distances, adjList, visited, i);
  }
  for (int i = 0; i < N; i++) {
    //
    maxP = max(maxP, distances[i]);
  }

  cout << maxP;

  return 0;
}
Problem p03166 (C++)

Buggy submission (status: Runtime Error):
/*Template by hemnath_d(Enlightened by chamow)*/
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define vec vector<ll>
#define pll pair<ll, ll>
#define fastread                                                               \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define pb push_back
#define mp make_pair
#define fx first
#define sx second
#define inf 100000000000000
#define minf -100000000000000
#define maxi 105
vec adj[maxi];
/*Function to add an edge*/
// Undirected graph
void add_edge(ll a, ll b) { adj[a].pb(b); }
ll dp[maxi];
ll dfs(ll x) {
  if (dp[x] != -1)
    return dp[x];
  ll ans = 0;
  for (ll i = 0; i < adj[x].size(); i++)
    ans = max(ans, dfs(adj[x][i]) + 1);
  dp[x] = ans;
  return ans;
}
int main() {
  fastread;
  ll n, m;
  cin >> n >> m;
  for (ll i = 0; i <= n; i++)
    dp[i] = -1;
  for (ll i = 0; i < m; i++) {
    ll x, y;
    cin >> x >> y;
    add_edge(x, y);
  }
  ll ans = 0;
  for (ll i = 1; i <= n; i++)
    ans = max(ans, dfs(i));
  cout << ans << endl;
  return 0;
}

Fixed submission:
/*Template by hemnath_d(Enlightened by chamow)*/
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define vec vector<ll>
#define pll pair<ll, ll>
#define fastread                                                               \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define pb push_back
#define mp make_pair
#define fx first
#define sx second
#define inf 100000000000000
#define minf -100000000000000
#define maxi 100005
vec adj[maxi];
/*Function to add an edge*/
// Undirected graph
void add_edge(ll a, ll b) { adj[a].pb(b); }
ll dp[maxi];
ll dfs(ll x) {
  if (dp[x] != -1)
    return dp[x];
  ll ans = 0;
  for (ll i = 0; i < adj[x].size(); i++)
    ans = max(ans, dfs(adj[x][i]) + 1);
  dp[x] = ans;
  return ans;
}
int main() {
  fastread;
  ll n, m;
  cin >> n >> m;
  for (ll i = 0; i <= n; i++)
    dp[i] = -1;
  for (ll i = 0; i < m; i++) {
    ll x, y;
    cin >> x >> y;
    add_edge(x, y);
  }
  ll ans = 0;
  for (ll i = 1; i <= n; i++)
    ans = max(ans, dfs(i));
  cout << ans << endl;
  return 0;
}
Problem p03166 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;

typedef long long ll;
typedef long double ld;
typedef array<ll, 2> pll;
const ll NMAX = 10005;
ll dp[NMAX], deg[NMAX];
vector<ll> edg[NMAX];

int main() {
  ll n, m, ans = 0;
  cin >> n >> m;
  for (ll i = 0; i < m; i++) {
    ll a, b;
    cin >> a >> b;
    deg[b]++;
    edg[a].push_back(b);
  }
  queue<ll> q;
  for (ll i = 1; i <= n; i++) {
    if (!deg[i]) {
      q.push(i);
    }
  }
  while (!q.empty()) {
    for (auto it : edg[q.front()]) {
      dp[it] = max(dp[it], dp[q.front()] + 1);
      deg[it]--;
      if (!deg[it])
        q.push(it);
    }
    ans = max(ans, dp[q.front()]);
    q.pop();
  }
  cout << ans;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;

typedef long long ll;
typedef long double ld;
typedef array<ll, 2> pll;
const ll NMAX = 100005;
ll dp[NMAX], deg[NMAX];
vector<ll> edg[NMAX];

int main() {
  ll n, m, ans = 0;
  cin >> n >> m;
  for (ll i = 0; i < m; i++) {
    ll a, b;
    cin >> a >> b;
    deg[b]++;
    edg[a].push_back(b);
  }
  queue<ll> q;
  for (ll i = 1; i <= n; i++) {
    if (!deg[i]) {
      q.push(i);
    }
  }
  while (!q.empty()) {
    for (auto it : edg[q.front()]) {
      dp[it] = max(dp[it], dp[q.front()] + 1);
      deg[it]--;
      if (!deg[it])
        q.push(it);
    }
    ans = max(ans, dp[q.front()]);
    q.pop();
  }
  cout << ans;
  return 0;
}
Problem p03166 (C++)

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

using namespace std;

const int N = 1e5 + 5;
vector<int> adj[N];
bool vis[N];
int dp[N], ans;

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

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

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

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

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

  cout << ans << "\n";
}

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

using namespace std;

const int N = 1e5 + 5;
vector<int> adj[N];
bool vis[N];
int dp[N], ans;

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

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

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

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

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

  cout << ans << "\n";
}