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

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
typedef long long int ll;
typedef long double ld;
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
const ll MOD = 1000000007;
const ll MAX_N = 500010;
const ll INF = 9999999999;
ll dp[MAX_N];
vector<vector<int>> G(MAX_N);

int rec(int v) {
  if (dp[v] >= 0)
    return dp[v];
  int res = 0;
  for (int u : G[v]) {
    res = max(res, rec(u) + 1);
  }
  return res;
}

int main() {
  int n, m, ans = 0;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    G[x].push_back(y);
  }
  for (int i = 0; i < n; i++)
    dp[i] = -1;
  for (int i = 0; i < n; i++) {
    ans = max(ans, rec(i));
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
typedef long long int ll;
typedef long double ld;
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
const ll MOD = 1000000007;
const ll MAX_N = 500010;
const ll INF = 9999999999;
ll dp[MAX_N];
vector<vector<int>> G(MAX_N);

int rec(int v) {
  if (dp[v] >= 0)
    return dp[v];
  int res = 0;
  for (int u : G[v]) {
    res = max(res, rec(u) + 1);
  }
  dp[v] = res;
  return res;
}

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

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

using namespace std;

vector<int> adj[100010];
vector<int> ts; /// topological sort

bool vis[100010];
void dfs(int u) {
  vis[u] = true;

  for (auto v : adj[u]) {
    if (vis[v])
      continue;
    dfs(v);
  }

  ts.push_back(u);
}

int dp[10010]; /// dp table

int main() {
  ios_base::sync_with_stdio(false);

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

  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;

    u--, v--;

    adj[u].push_back(v);
  }

  for (int i = 0; i < n; i++)
    if (!vis[i])
      dfs(i);
  reverse(ts.begin(), ts.end());

  for (int i = 0; i < n; i++) {
    int u = ts[i];
    for (int v : adj[u]) {
      dp[v] = max(dp[v], dp[u] + 1);
    }
  }

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

  cout << ans << endl;
}

/*
 * G
 **/


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

using namespace std;

vector<int> adj[100010];
vector<int> ts; /// topological sort

bool vis[100010];
void dfs(int u) {
  vis[u] = true;

  for (auto v : adj[u]) {
    if (vis[v])
      continue;
    dfs(v);
  }

  ts.push_back(u);
}

int dp[100010]; /// dp table

int main() {
  ios_base::sync_with_stdio(false);

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

  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;

    u--, v--;

    adj[u].push_back(v);
  }

  for (int i = 0; i < n; i++)
    if (!vis[i])
      dfs(i);
  reverse(ts.begin(), ts.end());

  for (int i = 0; i < n; i++) {
    int u = ts[i];
    for (int v : adj[u]) {
      dp[v] = max(dp[v], dp[u] + 1);
    }
  }

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

  cout << ans << endl;
}

/*
 * G
 **/
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
/**
 *  Kaiwen Zhou,
 *  CUHK
 **/
#include <bits/stdc++.h>
using namespace std;
#define INS                                                                    \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0)
typedef long long LINT;
#define INF 1000000000000
vector<int> dp(100001, -1);
vector<vector<int>> prev_map(100001);
int N, M;

int msearch(int i) {
  if (dp[i] != -1)
    return dp[i];
  int maxi = 0;
  for (auto u : prev_map[i]) {
    maxi = max(maxi, msearch(u) + 1);
  }
  return maxi;
}

int main(void) {
  INS;
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    int x, y;
    cin >> x >> y;
    prev_map[y].push_back(x);
  }
  int ans = -1;
  for (int i = 1; i <= N; i++) {
    ans = max(ans, msearch(i));
  }
  cout << ans << '\n';
  return 0;
}


Fixed submission:
/**
 *  Kaiwen Zhou,
 *  CUHK
 **/
#include <bits/stdc++.h>
using namespace std;
#define INS                                                                    \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0)
typedef long long LINT;
#define INF 1000000000000
vector<int> dp(100001, -1);
vector<vector<int>> prev_map(100001);
int N, M;

int msearch(int i) {
  if (dp[i] != -1)
    return dp[i];
  int maxi = 0;
  for (auto u : prev_map[i]) {
    maxi = max(maxi, msearch(u) + 1);
  }
  dp[i] = maxi;
  return maxi;
}

int main(void) {
  INS;
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    int x, y;
    cin >> x >> y;
    prev_map[y].push_back(x);
  }
  int ans = -1;
  for (int i = 1; i <= N; i++) {
    ans = max(ans, msearch(i));
  }
  cout << ans << '\n';
  return 0;
}
Problem p03166 (C++)

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

using namespace std;

vector<int> adj[100001];
vector<int> val, vis;

int dfs(int u) {
  vis[u] = 1;
  int ans = 0;
  for (auto it : adj[u]) {
    if (vis[it]) {
      ans = max(ans, 1 + val[it]);
    }
    ans = max(1 + dfs(it), ans);
  }
  val[u] = ans;
  return val[u];
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  val.resize(n + 1);
  vis.resize(n + 1);
  for (int i = 1; i <= n; i++)
    vis[i] = val[i] = 0;
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    adj[u].push_back(v);
  }

  for (int i = 1; i <= n; i++) {
    if (!vis[i])
      dfs(i);
  }
  int mx = INT_MIN;
  for (int i = 1; i <= n; i++)
    mx = max(mx, val[i]);

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

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

using namespace std;

vector<int> adj[100001];
vector<int> val, vis;

int dfs(int u) {
  vis[u] = 1;
  int ans = 0;
  for (auto it : adj[u]) {
    if (vis[it]) {
      ans = max(ans, 1 + val[it]);
      continue;
    }
    ans = max(1 + dfs(it), ans);
  }
  val[u] = ans;
  return val[u];
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  val.resize(n + 1);
  vis.resize(n + 1);
  for (int i = 1; i <= n; i++)
    vis[i] = val[i] = 0;
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    adj[u].push_back(v);
  }

  for (int i = 1; i <= n; i++) {
    if (!vis[i])
      dfs(i);
  }
  int mx = INT_MIN;
  for (int i = 1; i <= n; i++)
    mx = max(mx, val[i]);

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

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>     // minmax, sort, swap
#include <climits>       // INT_MIN, LLONG_MIN
#include <cmath>         // long, trig, pow
#include <cstdio>        // printf, scanf
#include <iomanip>       // cout<<setprecision(n)
#include <iostream>      // cin, cout, cerr, clog
#include <map>           // key-value pairs sorted by keys
#include <numeric>       // iota, accumulate, inner_product
#include <queue>         // queue, priority_queue
#include <set>           // set
#include <stack>         // stack
#include <string>        // string, stoi, to_string
#include <unordered_map> // hashed by keys
#include <unordered_set> // hashed by keys
#include <vector>        // vector

#define rep(i, n) for (int i = 0; i < n; i++)
#define ENDL "\n"
#define print(i) std::cout << (i) << "\n"

#define int long long // at least int64 > 9*10^18
#define all(v) v.begin(), v.end()

void dfs(int i, std::vector<std::vector<int>> &to, std::vector<int> &dp) {
  for (auto v : to[i]) {
    dp[v] = std::max(dp[v], dp[i] + 1);
    dfs(v, to, dp);
  }
  return;
}

signed main() {
  int n, m;
  std::cin >> n >> m;
  std::vector<std::vector<int>> to(n, std::vector<int>());
  std::vector<int> dp(n, 0);
  rep(i, m) {
    int a, b;
    std::cin >> a >> b;
    a--;
    b--;
    to[a].emplace_back(b);
  }
  rep(i, n) if (dp[i] == 0) dfs(i, to, dp);
  int max = 0;
  rep(i, n) max = std::max(dp[i], max);
  print(max);
  return 0;
}


Fixed submission:
#include <algorithm>     // minmax, sort, swap
#include <climits>       // INT_MIN, LLONG_MIN
#include <cmath>         // long, trig, pow
#include <cstdio>        // printf, scanf
#include <iomanip>       // cout<<setprecision(n)
#include <iostream>      // cin, cout, cerr, clog
#include <map>           // key-value pairs sorted by keys
#include <numeric>       // iota, accumulate, inner_product
#include <queue>         // queue, priority_queue
#include <set>           // set
#include <stack>         // stack
#include <string>        // string, stoi, to_string
#include <unordered_map> // hashed by keys
#include <unordered_set> // hashed by keys
#include <vector>        // vector

#define rep(i, n) for (int i = 0; i < n; i++)
#define ENDL "\n"
#define print(i) std::cout << (i) << "\n"

#define int long long // at least int64 > 9*10^18
#define all(v) v.begin(), v.end()

void dfs(int i, std::vector<std::vector<int>> &to, std::vector<int> &dp) {
  for (auto v : to[i]) {
    if (dp[v] == 0)
      dfs(v, to, dp);
    dp[i] = std::max(dp[i], dp[v] + 1);
  }
  return;
}

signed main() {
  int n, m;
  std::cin >> n >> m;
  std::vector<std::vector<int>> to(n, std::vector<int>());
  std::vector<int> dp(n, 0);
  rep(i, m) {
    int a, b;
    std::cin >> a >> b;
    a--;
    b--;
    to[a].emplace_back(b);
  }
  rep(i, n) if (dp[i] == 0) dfs(i, to, dp);
  int max = 0;
  rep(i, n) max = std::max(dp[i], max);
  print(max);
  return 0;
}
Problem p03166 (C++)

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

const int mxn = 1e5;
int n, m;
vector<int> adj[mxn];
ll d[mxn], indegree[mxn];
bool vis[mxn];

void bfs(int node) {
  queue<int> q;
  q.push(node);
  vis[node] = true;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (auto i : adj[u]) {
      // if(!vis[i])
      {
        d[i] = max(d[i], d[u] + 1);
        indegree[i]--;
        vis[i] = true;
        if (indegree[i] <= 0)
          q.push(i);
      }
    }
  }
}
int main() {
  // #ifndef ONLINE_JUDGE
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  // #endif

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

    for (int i = 0; i < n; i++) {
      if (!vis[i]) {
        bfs(i);
      }
    }
    // for(int i=0;i<n;i++)
    //{
    // cout<<indegree[i]<<' ';
    //}
    // cout<<'\n';
    ll ans = 0;
    for (int i = 0; i < mxn; i++) {
      ans = max(ans, d[i]);
    }
    cout << ans << '\n';
  }
}


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

const int mxn = 1e5;
int n, m;
vector<int> adj[mxn];
ll d[mxn], indegree[mxn];
bool vis[mxn];

void bfs(int node) {
  queue<int> q;
  q.push(node);
  vis[node] = true;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (auto i : adj[u]) {
      // if(!vis[i])
      {
        d[i] = max(d[i], d[u] + 1);
        indegree[i]--;
        vis[i] = true;
        if (indegree[i] <= 0)
          q.push(i);
      }
    }
  }
}
int main() {
  // #ifndef ONLINE_JUDGE
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  // #endif

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

    for (int i = 0; i < n; i++) {
      if (!vis[i] && indegree[i] == 0) {
        bfs(i);
      }
    }
    // for(int i=0;i<n;i++)
    //{
    // cout<<indegree[i]<<' ';
    //}
    // cout<<'\n';
    ll ans = 0;
    for (int i = 0; i < mxn; i++) {
      ans = max(ans, d[i]);
    }
    cout << ans << '\n';
  }
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <chrono>
#include <cstring>
#include <iostream>
#include <random>

#define rep(i, begin, end)                                                     \
  for (__typeof(end) i = (begin) - ((begin) > (end));                          \
       i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
using namespace std;
typedef long long ll;
const ll MOD = (ll)1e9 + 7;
const int N = 1e5 + 5, inf = 1e9 + 5;
ll add(ll x, ll y) {
  x += y;
  if (x >= MOD)
    return x - MOD;
  return x;
}
ll sub(ll x, ll y) {
  x -= y;
  if (x < 0)
    return x + MOD;
  return x;
}
ll mult(ll x, ll y) { return (x * y) % MOD; }
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

vector<int> adj[N];
int dp[N];

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

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  memset(dp, -1, sizeof(dp));
  rep(i, 0, m) {
    int u, v;
    cin >> u >> v;
    adj[u].push_back(v);
  }
  int ans = 0;
  for (int i = 1; i <= n; ++i)
    f(i);
  for (int i = 1; i <= n; ++i)
    ans = max(ans, f(i));
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <chrono>
#include <cstring>
#include <iostream>
#include <random>

#define rep(i, begin, end)                                                     \
  for (__typeof(end) i = (begin) - ((begin) > (end));                          \
       i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
using namespace std;
typedef long long ll;
const ll MOD = (ll)1e9 + 7;
const int N = 1e5 + 5, inf = 1e9 + 5;
ll add(ll x, ll y) {
  x += y;
  if (x >= MOD)
    return x - MOD;
  return x;
}
ll sub(ll x, ll y) {
  x -= y;
  if (x < 0)
    return x + MOD;
  return x;
}
ll mult(ll x, ll y) { return (x * y) % MOD; }
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

vector<int> adj[N];
int dp[N];

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

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  memset(dp, -1, sizeof(dp));
  rep(i, 0, m) {
    int u, v;
    cin >> u >> v;
    adj[u].push_back(v);
  }
  int ans = 0;
  for (int i = 1; i <= n; ++i)
    f(i);
  for (int i = 1; i <= n; ++i)
    ans = max(ans, f(i));
  cout << ans << endl;

  return 0;
}
Problem p03166 (C++)

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

int solve(int cur, vector<int> *gr, vector<int> &dp) {
  bool isLeaf = true;
  int best = 0;
  for (auto nbr : gr[cur]) {
    isLeaf = false;
    best = max(best, solve(nbr, gr, dp));
  }
  dp[cur] = (isLeaf) ? 0 : 1 + best;
  return dp[cur];
}

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> gr[n + 1];
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    gr[x].push_back(y);
  }
  vector<int> dp(n + 1, -1);
  for (int i = 1; i <= n; i++) {
    if (dp[i] == -1) {
      dp[i] = solve(i, gr, dp);
    }
  }
  cout << *max_element(dp.begin(), dp.end());
  return 0;
}


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

int solve(int cur, vector<int> *gr, vector<int> &dp) {
  if (dp[cur] != -1) {
    return dp[cur];
  }
  bool isLeaf = true;
  int best = 0;
  for (auto nbr : gr[cur]) {
    isLeaf = false;
    best = max(best, solve(nbr, gr, dp));
  }
  dp[cur] = (isLeaf) ? 0 : 1 + best;
  return dp[cur];
}

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> gr[n + 1];
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    gr[x].push_back(y);
  }
  vector<int> dp(n + 1, -1);
  for (int i = 1; i <= n; i++) {
    if (dp[i] == -1) {
      dp[i] = solve(i, gr, dp);
    }
  }
  cout << *max_element(dp.begin(), dp.end());
  return 0;
}
Problem p03166 (C++)

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

#include <iostream>
// #include <algorithm>
//  #include <iomanip>
#define ll long long

using namespace std;

const ll MOD = 1000000007;
const ll INF = (1LL << 62);

ll dp[100005];

map<ll, vector<ll>> m;

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

  auto ite = m[v].begin();

  ll r = 0;

  for (; ite != end(m[v]); ite++) {
    // cout << (*ite) << endl;
    r = max(r, rec(*ite) + 1);
  }

  return r;
}

int main() {
  ll N, M;
  scanf("%lld %lld", &N, &M);

  for (ll i = 0; i < M; i++) {
    ll x, y;
    scanf("%lld %lld", &x, &y);
    x--;
    y--;

    m[x].emplace_back(y);
  }

  fill_n(dp, 100005, -1);

  ll ans = 0;

  for (ll i = 0; i < N; i++) {
    ans = max(ans, rec(i));
  }

  cout << ans << endl;
}


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

#include <iostream>
// #include <algorithm>
//  #include <iomanip>
#define ll long long

using namespace std;

const ll MOD = 1000000007;
const ll INF = (1LL << 62);

ll dp[100005];

map<ll, vector<ll>> m;

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

  auto ite = m[v].begin();

  ll r = 0;

  for (; ite != end(m[v]); ite++) {
    // cout << (*ite) << endl;
    r = max(r, rec(*ite) + 1);
  }

  dp[v] = r;

  return r;
}

int main() {
  ll N, M;
  scanf("%lld %lld", &N, &M);

  for (ll i = 0; i < M; i++) {
    ll x, y;
    scanf("%lld %lld", &x, &y);
    x--;
    y--;

    m[x].emplace_back(y);
  }

  fill_n(dp, 100005, -1);

  ll ans = 0;

  for (ll i = 0; i < N; i++) {
    ans = max(ans, rec(i));
  }

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

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <deque>
#include <iostream>
#include <vector>
using namespace std;
using uint = unsigned int;
using ull = unsigned long long;

uint lps(const vector<vector<uint>> &E, vector<int> &LP, const uint v) {
  if (LP[v] >= 0)
    return LP[v];

  uint d = 0;
  for (auto nv : E[v]) {
    d = max(d, lps(E, LP, nv) + 1);
  }

  return d;
}

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

  vector<vector<uint>> E(N, vector<uint>());
  for (uint i = 0; i < M; i++) {
    uint x, y;
    cin >> x >> y;
    E[x - 1].push_back(y - 1);
  }

  vector<int> V(N, -1);

  uint ans = 0;
  for (uint i = 0; i < N; i++) { // start node
    ans = max(ans, lps(E, V, i));
  }

  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <deque>
#include <iostream>
#include <vector>
using namespace std;
using uint = unsigned int;
using ull = unsigned long long;

uint lps(const vector<vector<uint>> &E, vector<int> &LP, const uint v) {
  if (LP[v] >= 0)
    return LP[v];

  uint d = 0;
  for (auto nv : E[v]) {
    d = max(d, lps(E, LP, nv) + 1);
  }

  LP[v] = d;
  return d;
}

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

  vector<vector<uint>> E(N, vector<uint>());
  for (uint i = 0; i < M; i++) {
    uint x, y;
    cin >> x >> y;
    E[x - 1].push_back(y - 1);
  }

  vector<int> V(N, -1);

  uint ans = 0;
  for (uint i = 0; i < N; i++) { // start node
    ans = max(ans, lps(E, V, i));
  }

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

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

using namespace std;

#define MAX(X, Y) ((X) > (Y) ? (X) : (Y))

vector<vector<int>> graph;
vector<int> dp;

int dfs(int fr) {
  int mx = 0;
  if (dp[fr] != -1)
    return dp[fr];
  for (auto itr = graph[fr].begin(); itr != graph[fr].end(); itr++) {
    int t = dfs(*itr) + 1;
    mx = MAX(mx, t);
  }
  return mx;
}

int main() {
  int n, m;
  int ans;

  cin >> n >> m;
  graph = vector<vector<int>>(n);
  dp = vector<int>(n);
  fill(dp.begin(), dp.end(), -1);
  for (int i = 0; i < m; i++) {
    int fr, to;
    cin >> fr >> to;
    graph[fr - 1].push_back(to - 1);
  }

  ans = 0;
  for (int i = 0; i < n; i++) {
    ans = MAX(ans, dfs(i));
    //  cout << i << ": " << dfs(i) << endl;
  }
  cout << ans << endl;
  return 0;
}

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

using namespace std;

#define MAX(X, Y) ((X) > (Y) ? (X) : (Y))

vector<vector<int>> graph;
vector<int> dp;

int dfs(int fr) {
  int mx = 0;
  if (dp[fr] != -1)
    return dp[fr];
  for (auto itr = graph[fr].begin(); itr != graph[fr].end(); itr++) {
    int t = dfs(*itr) + 1;
    mx = MAX(mx, t);
  }
  return dp[fr] = mx;
}

int main() {
  int n, m;
  int ans;

  cin >> n >> m;
  graph = vector<vector<int>>(n);
  dp = vector<int>(n);
  fill(dp.begin(), dp.end(), -1);
  for (int i = 0; i < m; i++) {
    int fr, to;
    cin >> fr >> to;
    graph[fr - 1].push_back(to - 1);
  }

  ans = 0;
  for (int i = 0; i < n; i++) {
    ans = MAX(ans, dfs(i));
    //  cout << i << ": " << dfs(i) << endl;
  }
  cout << ans << endl;
  return 0;
}
Problem p03166 (C++)

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

using namespace std;

#define MAX_N 10005

int n, m, dp[MAX_N];

vector<int> adh[MAX_N];

int f_imp(int);

int f(int p) {
  if (dp[p] == 0)
    dp[p] = f_imp(p) + 1;
  return dp[p] - 1;
}

int f_imp(int p) {
  int rs = 0;
  for (auto &i : adh[p])
    rs = max(rs, f(i) + 1);
  return rs;
}

int main() {
  scanf("%d %d", &n, &m);
  for (int i = 0; i < m; i++) {
    int t1, t2;
    scanf("%d %d", &t1, &t2);
    t1--;
    t2--;
    adh[t1].push_back(t2);
  }
  int rs = 0;
  for (int i = 0; i < n; i++) {
    rs = max(rs, f(i));
  }
  printf("%d\n", rs);
}


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

using namespace std;

#define MAX_N 100005

int n, m, dp[MAX_N];

vector<int> adh[MAX_N];

int f_imp(int);

int f(int p) {
  if (dp[p] == 0)
    dp[p] = f_imp(p) + 1;
  return dp[p] - 1;
}

int f_imp(int p) {
  int rs = 0;
  for (auto &i : adh[p])
    rs = max(rs, f(i) + 1);
  return rs;
}

int main() {
  scanf("%d %d", &n, &m);
  for (int i = 0; i < m; i++) {
    int t1, t2;
    scanf("%d %d", &t1, &t2);
    t1--;
    t2--;
    adh[t1].push_back(t2);
  }
  int rs = 0;
  for (int i = 0; i < n; i++) {
    rs = max(rs, f(i));
  }
  printf("%d\n", rs);
}
Problem p03166 (C++)

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

int d[100000];
int f(int x, vector<vector<int>> &v) {
  if (d[x] != 0)
    return d[x];
  if (v[x].size() == 0)
    return 0;
  int ans = 0;
  for (int a : v[x]) {
    ans = max(ans, f(a, v) + 1);
  }
  return ans;
}

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

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

int d[100000];
int f(int x, vector<vector<int>> &v) {
  if (d[x] != 0)
    return d[x];
  if (v[x].size() == 0)
    return 0;
  int ans = 0;
  for (int a : v[x]) {
    ans = max(ans, f(a, v) + 1);
  }
  return d[x] = ans;
}

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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define ld double
#define ll long long
#define pb emplace_back
#define mk make_pair
#define mod 1000000007
#define ff first
#define ss second
#define sz(x) (int)x.size()
#define FIO                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define all(x) x.begin(), x.end()
ll power(ll a, ll b) {
  ll res = 1;
  a = a % mod;
  while (b) {
    if (b & 1)
      res = (res * a) % mod;
    a = (a * a) % mod;
    b /= 2;
  }
  return res;
}
ll invmod(ll a) { return power(a, mod - 2); }
vector<vector<int>> adj;
vector<int> dp;
int dfs(int v) {
  int mx = -1;
  for (auto u : adj[v]) {
    mx = max(mx, dfs(u));
  }
  return dp[v] = mx + 1;
}
int main(void) {
  FIO int t = 1;
  // cin>>t;
  while (t--) {
    int n, m, u, v;
    cin >> n >> m;
    adj.assign(n + 1, vector<int>());
    dp.assign(n + 1, -1);
    for (int i = 0; i < m; i++) {
      cin >> u >> v;
      adj[u].pb(v);
    }
    for (int i = 1; i <= n; i++) {
      if (dp[i] == -1)
        dfs(i);
    }
    cout << *max_element(all(dp)) << "\n";
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ld double
#define ll long long
#define pb emplace_back
#define mk make_pair
#define mod 1000000007
#define ff first
#define ss second
#define sz(x) (int)x.size()
#define FIO                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define all(x) x.begin(), x.end()
ll power(ll a, ll b) {
  ll res = 1;
  a = a % mod;
  while (b) {
    if (b & 1)
      res = (res * a) % mod;
    a = (a * a) % mod;
    b /= 2;
  }
  return res;
}
ll invmod(ll a) { return power(a, mod - 2); }
vector<vector<int>> adj;
vector<int> dp;
int dfs(int v) {
  int mx = -1;
  for (auto u : adj[v]) {
    if (dp[u] == -1)
      dfs(u);
    mx = max(mx, dp[u]);
  }
  return dp[v] = mx + 1;
}
int main(void) {
  FIO int t = 1;
  // cin>>t;
  while (t--) {
    int n, m, u, v;
    cin >> n >> m;
    adj.assign(n + 1, vector<int>());
    dp.assign(n + 1, -1);
    for (int i = 0; i < m; i++) {
      cin >> u >> v;
      adj[u].pb(v);
    }
    for (int i = 1; i <= n; i++) {
      if (dp[i] == -1)
        dfs(i);
    }
    cout << *max_element(all(dp)) << "\n";
  }
  return 0;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
vector<int> vis, dp;
void initialise(int n) {
  vis.assign(n, 0);
  dp.assign(n, 0);
}
int dfs(int i, vector<int> v[]) {
  int max = 0;
  vis[i] = 1;
  for (int j = 0; j < (int)v[i].size(); j++) {
    if (max < dfs(v[i][j], v)) {
      max = dfs(v[i][j], v);
    }
  }
  dp[i] = max + 1;
  return dp[i];
}
int main() {
  int n, m;
  cin >> n >> m;
  int x, y;
  vector<int> v[n];
  initialise(n);
  for (int i = 0; i < m; i++) {
    cin >> x >> y;
    x--, y--;
    v[x].push_back(y);
  }
  for (int i = 0; i < n; i++) {
    if (!vis[i]) {
      dfs(i, v);
    }
  }
  int i = *max_element(dp.begin(), dp.end());
  cout << i - 1;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
vector<int> vis, dp;
void initialise(int n) {
  vis.assign(n, 0);
  dp.assign(n, 0);
}
int dfs(int i, vector<int> v[]) {
  int max = 0;
  vis[i] = 1;
  for (int j = 0; j < (int)v[i].size(); j++) {
    if (!vis[v[i][j]]) {
      if (max < dfs(v[i][j], v)) {
        max = dp[v[i][j]];
      }
    } else if (max < dp[v[i][j]]) {
      max = dp[v[i][j]];
    }
  }
  dp[i] = max + 1;
  return dp[i];
}
int main() {
  int n, m;
  cin >> n >> m;
  int x, y;
  vector<int> v[n];
  initialise(n);
  for (int i = 0; i < m; i++) {
    cin >> x >> y;
    x--, y--;
    v[x].push_back(y);
  }
  for (int i = 0; i < n; i++) {
    if (!vis[i]) {
      dfs(i, v);
    }
  }
  int i = *max_element(dp.begin(), dp.end());
  cout << i - 1;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef vector<vector<int>> Graph;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

Graph G;
int dp[100010];

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

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

  rep(i, n) { dp[i] = -1; }
  int ans = 0;
  rep(i, n) { chmax(ans, rec(i)); }
  cout << ans << endl;
  return 0;
}

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

Graph G;
int dp[100010];

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

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

  rep(i, n) { dp[i] = -1; }
  int ans = 0;
  rep(i, n) { chmax(ans, rec(i)); }
  cout << ans << endl;
  return 0;
}
Problem p03166 (C++)

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

using namespace std;

int n, m;
vector<int> tree[100001];
vector<int> emp[100001];
int dp[100001];
int gmx = 0;

int dfs(int ind) {
  int mx = 0;
  for (auto e : tree[ind]) {
    mx = max(mx, dfs(e));
  }
  dp[ind] = mx + 1;
  return mx + 1;
}

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

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

  while (!q.empty()) {
    gmx = max(gmx, dfs(q.front()));
    q.pop();
  }

  cout << gmx - 1 << endl;
}

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

using namespace std;

int n, m;
vector<int> tree[100001];
vector<int> emp[100001];
int dp[100001];
int gmx = 0;

int dfs(int ind) {
  int mx = 0;
  if (dp[ind] != 0)
    return dp[ind];

  for (auto e : tree[ind]) {
    mx = max(mx, dfs(e));
  }
  dp[ind] = mx + 1;
  return mx + 1;
}

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

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

  while (!q.empty()) {
    gmx = max(gmx, dfs(q.front()));
    q.pop();
  }

  cout << gmx - 1 << endl;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int n, m;
vector<int> adj[100100];
vector<int> inc[100100];
bool vis[100100];
vector<int> ord;
void topo(int x) {
  vis[x] = true;
  for (auto i : adj[x]) {
    topo(i);
  }
  ord.push_back(x);
}
int dp[100100];
int o = 0;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m;
  int ai, bi;
  for (int i = 0; i < m; i++) {
    cin >> ai >> bi;
    ai--;
    bi--;
    adj[ai].push_back(bi);
    inc[bi].push_back(ai);
  }
  for (int i = 0; i < n; i++) {
    if (!vis[i])
      topo(i);
  }
  reverse(ord.begin(), ord.end());
  for (auto &i : ord) {
    for (auto &j : inc[i]) {
      dp[i] = max(dp[i], dp[j] + 1);
    }
    o = max(o, dp[i]);
  }
  cout << o << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int n, m;
vector<int> adj[100100];
vector<int> inc[100100];
bool vis[100100];
vector<int> ord;
void topo(int x) {
  vis[x] = true;
  for (auto i : adj[x]) {
    if (vis[i])
      continue;
    topo(i);
  }
  ord.push_back(x);
}
int dp[100100];
int o = 0;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m;
  int ai, bi;
  for (int i = 0; i < m; i++) {
    cin >> ai >> bi;
    ai--;
    bi--;
    adj[ai].push_back(bi);
    inc[bi].push_back(ai);
  }
  for (int i = 0; i < n; i++) {
    if (!vis[i])
      topo(i);
  }
  reverse(ord.begin(), ord.end());
  for (auto &i : ord) {
    for (auto &j : inc[i]) {
      dp[i] = max(dp[i], dp[j] + 1);
    }
    o = max(o, dp[i]);
  }
  cout << o << endl;
}
Problem p03166 (C++)

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

using namespace std;
void dfs(int i, vector<int> adj[], vector<bool> vis, vector<int> &dp) {

  vis[i] = 1;
  for (int j = 0; j < adj[i].size(); j++) {
    if (!vis[adj[i][j]])
      dfs(adj[i][j], adj, vis, dp);
    dp[i] = max(dp[i], 1 + dp[adj[i][j]]);
  }
}
int main() {
  int n, m, x, y;
  cin >> n >> m;
  vector<int> dp(n + 1, 0);
  vector<int> adj[n + 1];
  for (int i = 0; i < m; i++) {
    cin >> x >> y;
    adj[x].push_back(y);
  }

  vector<bool> vis(n + 1, 0);
  for (int i = 1; i <= n; i++) {
    if (!vis[i])
      dfs(i, adj, vis, dp);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    ans = max(ans, dp[i]);
  }
  cout << ans << endl;
}


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

using namespace std;
void dfs(int i, vector<int> adj[], vector<bool> &vis, vector<int> &dp) {

  vis[i] = 1;
  for (int j = 0; j < adj[i].size(); j++) {
    if (!vis[adj[i][j]])
      dfs(adj[i][j], adj, vis, dp);
    dp[i] = max(dp[i], 1 + dp[adj[i][j]]);
  }
}
int main() {
  int n, m, x, y;
  cin >> n >> m;
  vector<int> dp(n + 1, 0);
  vector<int> adj[n + 1];
  for (int i = 0; i < m; i++) {
    cin >> x >> y;
    adj[x].push_back(y);
  }

  vector<bool> vis(n + 1, 0);
  for (int i = 1; i <= n; i++) {
    if (!vis[i])
      dfs(i, adj, vis, dp);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    ans = max(ans, dp[i]);
  }
  cout << ans << endl;
}
Problem p03166 (C++)

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

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

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

  bool leaf = true;
  int best = 0;
  for (int child : graph[src]) {
    leaf = false;
    best = max(best, solve(child));
  }

  return leaf ? 0 : 1 + best;
}

int main() {
  memset(dp, -1, sizeof dp);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    graph[a].push_back(b);
  }

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

  cout << ans << endl;
}


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

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

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

  bool leaf = true;
  int best = 0;
  for (int child : graph[src]) {
    leaf = false;
    best = max(best, solve(child));
  }

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

int main() {
  memset(dp, -1, sizeof dp);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    graph[a].push_back(b);
  }

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

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

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

using namespace std;

typedef long int li;
typedef long double ld;
typedef long long ll;
typedef pair<int, int> pt;

#define fi first
#define se second
#define dump(x) cout << #x << " = " << x << endl
#define fastIO                                                                 \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0);

li n, m, x, y, cl[100007], dp[100007], ans;
vector<int> g[100007];

li dfs(int v) {
  cl[v] = 1;
  dp[v] = 0;
  for (int i : g[v]) {
    dp[v] = max(dp[v], dfs(i) + 1);
  }
  return (dp[v]);
}

int main() {
  fastIO;
  cin >> n >> m;
  for (li i = 0; i < m; i++) {
    cin >> x >> y;
    g[x].push_back(y);
  }
  ans = 0;
  for (li i = 1; i <= n; i++) {
    if (!cl[i])
      ans = max(ans, dfs(i));
  }
  cout << ans;
}


Fixed submission:
#pragma GCC optimize("Ofast")
#include "bits/stdc++.h"

using namespace std;

typedef long int li;
typedef long double ld;
typedef long long ll;
typedef pair<int, int> pt;

#define fi first
#define se second
#define dump(x) cout << #x << " = " << x << endl
#define fastIO                                                                 \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0);

li n, m, x, y, cl[100007], dp[100007], ans;
vector<int> g[100007];

li dfs(int v) {
  if (dp[v])
    return (dp[v]);
  cl[v] = 1;
  dp[v] = 0;
  for (int i : g[v]) {
    dp[v] = max(dp[v], dfs(i) + 1);
  }
  return (dp[v]);
}

int main() {
  fastIO;
  cin >> n >> m;
  for (li i = 0; i < m; i++) {
    cin >> x >> y;
    g[x].push_back(y);
  }
  ans = 0;
  for (li i = 1; i <= n; i++) {
    if (!cl[i])
      ans = max(ans, dfs(i));
  }
  cout << ans;
}
Problem p03166 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> x;
int n, m;
bool visited[100009];
int dp[100009];
int solve(int curr) {
  if (dp[curr] != -1)
    return dp[curr];
  int temp = -1;
  for (auto i : x[curr]) {
    if (dp[i] != -1) {
      temp = 1 + dp[i];
      dp[curr] = max(dp[curr], temp);
    } else
      dp[curr] = max(dp[curr], 1 + solve(i));
  }
  if (dp[curr] == -1) {
    return dp[curr] = 0;
  }
  return dp[curr];
}
int main() {
  scanf("%d%d", &n, &m);
  x.resize(m + 5);
  int from, to;
  for (int i = 0; i < m; ++i) {
    scanf("%d%d", &from, &to);
    x[from].push_back(to);
  }
  memset(visited, false, sizeof visited);
  memset(dp, -1, sizeof dp);
  int ans = -1;
  for (int i = 1; i <= n; ++i)
    ans = max(ans, solve(i));
  printf("%d", ans);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> x;
int n, m;
bool visited[100009];
int dp[100009];
int solve(int curr) {
  if (dp[curr] != -1)
    return dp[curr];
  int temp = -1;
  for (auto i : x[curr]) {
    if (dp[i] != -1) {
      temp = 1 + dp[i];
      dp[curr] = max(dp[curr], temp);
    } else
      dp[curr] = max(dp[curr], 1 + solve(i));
  }
  if (dp[curr] == -1) {
    return dp[curr] = 0;
  }
  return dp[curr];
}
int main() {
  scanf("%d%d", &n, &m);
  x.resize(n + 5);
  int from, to;
  for (int i = 0; i < m; ++i) {
    scanf("%d%d", &from, &to);
    x[from].push_back(to);
  }
  memset(visited, false, sizeof visited);
  memset(dp, -1, sizeof dp);
  int ans = -1;
  for (int i = 1; i <= n; ++i)
    ans = max(ans, solve(i));
  printf("%d", ans);
}
Problem p03166 (C++)

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

#define ll long long int

int dp[10010];
int longest(vector<int> v[], int i) {
  if (v[i].size() == 0)
    return 0;
  if (dp[i] >= 0)
    return dp[i];
  else {
    for (auto e : v[i]) {
      dp[i] = max(dp[i], 1 + longest(v, e));
    }
    return dp[i];
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m, x, y;
  cin >> n >> m;
  vector<int> v[n + 1];
  for (int i = 0; i <= n; ++i) {
    dp[i] = -1;
  }
  for (int i = 1; i <= m; ++i) {
    cin >> x >> y;
    v[x].push_back(y);
  }
  int k = 0;
  for (int i = 1; i <= n; ++i) {
    k = max(k, longest(v, i));
  }
  cout << k << "\n";
  return 0;
}

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

#define ll long long int

int dp[100100];
int longest(vector<int> v[], int i) {
  if (v[i].size() == 0)
    return 0;
  if (dp[i] >= 0)
    return dp[i];
  else {
    for (auto e : v[i]) {
      dp[i] = max(dp[i], 1 + longest(v, e));
    }
    return dp[i];
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m, x, y;
  cin >> n >> m;
  vector<int> v[n + 1];
  for (int i = 0; i <= n; ++i) {
    dp[i] = -1;
  }
  for (int i = 1; i <= m; ++i) {
    cin >> x >> y;
    v[x].push_back(y);
  }
  int k = 0;
  for (int i = 1; i <= n; ++i) {
    k = max(k, longest(v, i));
  }
  cout << k << "\n";
  return 0;
}
Problem p03166 (C++)

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

using namespace std;

int dp[100001], n, m, a, b;
int solve(vector<vector<int>> &arr, int i) {
  if (dp[i] != -1)
    return dp[i];

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

  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  memset(dp, -1, sizeof(dp));
  cin >> n >> m;
  vector<vector<int>> arr(n + 1);
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    arr[a].push_back(b);
  }
  for (int i = 1; i <= n; i++) {
    if (arr[i].size() == 0) {
      dp[i] = 0;
    }
  }
  int maxi = INT_MIN;
  for (int i = 1; i <= n; i++) {
    memset(dp, -1, sizeof(dp));
    for (int j = 1; j <= n; j++) {
      if (arr[j].size() == 0) {
        dp[j] = 0;
      }
    }
    maxi = max(maxi, solve(arr, i));
  }
  cout << maxi;
}

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

using namespace std;

int dp[100001], n, m, a, b;
int solve(vector<vector<int>> &arr, int i) {
  if (dp[i] != -1)
    return dp[i];

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

  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  memset(dp, -1, sizeof(dp));
  cin >> n >> m;
  vector<vector<int>> arr(n + 1);
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    arr[a].push_back(b);
  }
  for (int i = 1; i <= n; i++) {
    if (arr[i].size() == 0) {
      dp[i] = 0;
    }
  }
  int maxi = INT_MIN;
  for (int i = 1; i <= n; i++) {
    maxi = max(maxi, solve(arr, i));
  }
  cout << maxi;
}
Problem p03166 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define BIGINF (int)4e18 + 7
#define MOD7 1000000007
#define MOD9 1e9 + 9
#define INF (int)2e9 + 7
#define SCD(t) scanf("%d", &t)
#define SCLD(t) scanf("%ld", &t)
#define SCLLD(t) scanf("%lld", &t)
#define SCC(t) scanf("%c", &t)
#define SCS(t) scanf("%s", t)
#define SCF(t) scanf("%f", &t)
#define SCLF(t) scanf("%lf", &t)
#define MEM(a, b) memset(a, (b), sizeof(a))
typedef long long ll;
typedef long double ld;
typedef string str;
typedef double d;
typedef unsigned u;
typedef unsigned long long ull;

// int N = 10005;
bool visited[100005];
int dp[100005];
int inDegree[100005];
vector<int> graph[100005];

void dfs(int vertex) {
  visited[vertex] = true;
  int size = graph[vertex].size();
  for (int i = 0; i < size; i++) {
    int a = graph[vertex][i];
    dp[a] = max(dp[a], dp[vertex] + 1);
    inDegree[a]--;
    if (inDegree[a] == 0)
      dfs(a);
  }
}

int main() {
  // freopen("inputf.txt", "r", stdin);
  // freopen("outputf.txt", "w", stdout);
  int n, m;
  SCD(n);
  SCD(m);
  int x, y;
  memset(dp, 0, sizeof(dp));
  memset(visited, false, sizeof(dp));
  for (int i = 0; i < m; i++) {
    SCD(x);
    SCD(y);
    graph[x].push_back(y);
    inDegree[y]++;
  }

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

  int ans = 0;
  for (int i = 1; i <= n; i++) {
    ans = max(ans, dp[i]);
  }
  printf("%d", ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define BIGINF (int)4e18 + 7
#define MOD7 1000000007
#define MOD9 1e9 + 9
#define INF (int)2e9 + 7
#define SCD(t) scanf("%d", &t)
#define SCLD(t) scanf("%ld", &t)
#define SCLLD(t) scanf("%lld", &t)
#define SCC(t) scanf("%c", &t)
#define SCS(t) scanf("%s", t)
#define SCF(t) scanf("%f", &t)
#define SCLF(t) scanf("%lf", &t)
#define MEM(a, b) memset(a, (b), sizeof(a))
typedef long long ll;
typedef long double ld;
typedef string str;
typedef double d;
typedef unsigned u;
typedef unsigned long long ull;

// int N = 10005;
bool visited[100005];
int dp[100005];
int inDegree[100005];
vector<int> graph[100005];

void dfs(int vertex) {
  visited[vertex] = true;
  int size = graph[vertex].size();
  for (int i = 0; i < size; i++) {
    int a = graph[vertex][i];
    dp[a] = max(dp[a], dp[vertex] + 1);
    inDegree[a]--;
    if (inDegree[a] == 0)
      dfs(a);
  }
}

int main() {
  // freopen("inputf.txt", "r", stdin);
  // freopen("outputf.txt", "w", stdout);
  int n, m;
  SCD(n);
  SCD(m);
  int x, y;
  memset(dp, 0, sizeof(dp));
  memset(visited, false, sizeof(visited));
  for (int i = 0; i < m; i++) {
    SCD(x);
    SCD(y);
    graph[x].push_back(y);
    inDegree[y]++;
  }

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

  int ans = 0;
  for (int i = 1; i <= n; i++) {
    ans = max(ans, dp[i]);
  }
  printf("%d", ans);
  return 0;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;

int dfs(const vector<vector<int>> &p, vector<int> &r, int n) {
  if (r[n] != -1)
    return r[n];
  int a = -1;
  for (auto m : p[n]) {
    a = max(a, dfs(p, r, m));
  }
  return a + 1;
}

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

  int res = -1;
  vector<int> r(N, -1);
  for (int i = 0; i < N; i++) {
    res = max(res, dfs(p, r, i));
  }

  cout << res << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;

int dfs(const vector<vector<int>> &p, vector<int> &r, int n) {
  if (r[n] != -1)
    return r[n];
  int a = -1;
  for (auto m : p[n]) {
    a = max(a, dfs(p, r, m));
  }
  return r[n] = a + 1;
}

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

  int res = -1;
  vector<int> r(N, -1);
  for (int i = 0; i < N; i++) {
    res = max(res, dfs(p, r, i));
  }

  cout << res << endl;

  return 0;
}