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

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>
#define MAX 100005
using namespace std;
vector<int> maps[MAX];
int memo[MAX];
int indegree[MAX] = {0};
// 从i节点到结束所能达到的最长路径
int dfs(int x) {
  if (memo[x] != -1)
    return memo[x];

  int l = maps[x].size();
  int res = 0;
  for (int i = 0; i < l; i++) {
    res = max(res, dfs(maps[x][i]) + 1);
  }
  return res;
}
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  fill(memo, memo + MAX, -1);
  for (int i = 0; i < m; i++) {
    int from, to;
    scanf("%d %d", &from, &to);
    maps[from].push_back(to);
    indegree[to] += 1;
  }

  int res = 0;
  for (int i = 1; i <= n; i++) {
    if (indegree[i] == 0) {
      res = max(res, dfs(i));
    }
  }
  printf("%d", res);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>
#define MAX 100005
using namespace std;
vector<int> maps[MAX];
int memo[MAX];
int indegree[MAX] = {0};
// 从i节点到结束所能达到的最长路径
int dfs(int x) {
  if (memo[x] != -1)
    return memo[x];

  int l = maps[x].size();
  int res = 0;
  for (int i = 0; i < l; i++) {
    res = max(res, dfs(maps[x][i]) + 1);
  }
  memo[x] = res;
  return memo[x];
}
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  fill(memo, memo + MAX, -1);
  for (int i = 0; i < m; i++) {
    int from, to;
    scanf("%d %d", &from, &to);
    maps[from].push_back(to);
    indegree[to] += 1;
  }

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; (i)--)
#define rrep1(i, n) for (int i = ((int)(n)); i > 0; (i)--)
#define sz(c) ((int)(c).size())
#define all(x) (x).begin(), (x).end()
#define mp make_pair
#define pb push_back

using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> P;
typedef pair<int, pair<int, int>> PP;
const int MAX = 3e3 + 10;
const int INF = 1e9 + 7;

int N, M;
vi G[MAX];
void input() {
  cin >> N >> M;
  rep(_, M) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].pb(b);
  }
}

int d[MAX];
int dfs(int v) {
  if (d[v] != -1)
    return d[v];
  int tmp = 0;
  rep(i, sz(G[v])) {
    int u = G[v][i];
    int p = dfs(u);
    tmp = max(tmp, p + 1);
  }
  d[v] = tmp;
  return d[v];
}

int ans;
void solve() {
  rep(i, N) d[i] = -1;
  rep(i, N) {
    int tmp = dfs(i);
    ans = max(ans, tmp);
  }
}

void output() { cout << ans << endl; }

int main() {
  input();
  solve();
  output();
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; (i)--)
#define rrep1(i, n) for (int i = ((int)(n)); i > 0; (i)--)
#define sz(c) ((int)(c).size())
#define all(x) (x).begin(), (x).end()
#define mp make_pair
#define pb push_back

using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> P;
typedef pair<int, pair<int, int>> PP;
const int MAX = 1e5 + 10;
const int INF = 1e9 + 7;

int N, M;
vi G[MAX];
void input() {
  cin >> N >> M;
  rep(_, M) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].pb(b);
  }
}

int d[MAX];
int dfs(int v) {
  if (d[v] != -1)
    return d[v];
  int tmp = 0;
  rep(i, sz(G[v])) {
    int u = G[v][i];
    int p = dfs(u);
    tmp = max(tmp, p + 1);
  }
  d[v] = tmp;
  return d[v];
}

int ans;
void solve() {
  rep(i, N) d[i] = -1;
  rep(i, N) {
    int tmp = dfs(i);
    ans = max(ans, tmp);
  }
}

void output() { cout << ans << endl; }

int main() {
  input();
  solve();
  output();
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0, i##_len = (n); i < i##_len; i++)
#define reps(i, s, n) for (ll i = (s), i##_len = (n); i < i##_len; i++)
#define rrep(i, n) for (ll i = (n)-1; i >= 0; i--)
#define rreps(i, e, n) for (ll i = (n)-1; i >= (e); i--)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((ll)(x).size())
#define len(x) ((ll)(x).length())

ll n, m;
vector<vector<ll>> g;
vector<ll> dp;

ll dfs(ll v, ll p = -1) {
  if (dp[v] != -1)
    return dp[v];
  ll res = 0;
  for (auto u : g[v]) {
    if (u == p)
      continue;
    res = max(res, dfs(u, v) + 1);
  }
  return res;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  // ifstream in("input.txt");
  // cin.rdbuf(in.rdbuf());
  cin >> n >> m;
  g.resize(n);
  rep(i, m) {
    ll x, y;
    cin >> x >> y;
    x--;
    y--;
    g[x].push_back(y);
  }
  dp.resize(n, -1);
  ll ans = 0;
  rep(i, n) ans = max(ans, dfs(i));
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0, i##_len = (n); i < i##_len; i++)
#define reps(i, s, n) for (ll i = (s), i##_len = (n); i < i##_len; i++)
#define rrep(i, n) for (ll i = (n)-1; i >= 0; i--)
#define rreps(i, e, n) for (ll i = (n)-1; i >= (e); i--)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((ll)(x).size())
#define len(x) ((ll)(x).length())

ll n, m;
vector<vector<ll>> g;
vector<ll> dp;

ll dfs(ll v, ll p = -1) {
  if (dp[v] != -1)
    return dp[v];
  ll res = 0;
  for (auto u : g[v]) {
    if (u == p)
      continue;
    res = max(res, dfs(u, v) + 1);
  }
  dp[v] = res;
  return res;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  // ifstream in("input.txt");
  // cin.rdbuf(in.rdbuf());
  cin >> n >> m;
  g.resize(n);
  rep(i, m) {
    ll x, y;
    cin >> x >> y;
    x--;
    y--;
    g[x].push_back(y);
  }
  dp.resize(n, -1);
  ll ans = 0;
  rep(i, n) ans = max(ans, dfs(i));
  cout << ans << endl;
  return 0;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
// #include <cmath>
// #include <cstdio>
// #include <vector>
// #include <map>
// #include <iostream>
// #include <algorithm>
// #include <set>
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using vi = vector<int>;
using vb = vector<bool>;
using pli = pair<ll, int>;
#define fi first
#define v vector
#define se second
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
int INF = 1e9 + 7;
ll inf = 1e18 + 7;

const int N = 100010;

void add_self(int &a, int b) { a += b; }

void max_self(int &a, int b) {
  if (b > a)
    a = b;
}

void topsort(int i, v<vi> &graph, vb &visit, stack<int> &res) {
  visit[i] = true;

  for (int x : graph[i]) {
    if (!visit[x])
      topsort(x, graph, visit, res);
  }
  res.push(i);
}

int dp[N];

int find(int i, v<vi> &graph) {
  if (dp[i] != -1)
    return dp[i];
  int res = 0;
  for (int v : graph[i]) {
    max_self(res, 1 + find(v, graph));
  }
  return res;
}

void solve() {
  for (int i = 0; i < N; ++i) {
    dp[i] = -1;
  }
  int n, m;
  scanf("%d%d", &n, &m);
  v<vi> graph(n);
  for (int i = 0; i < m; ++i) {
    int a, b;
    scanf("%d%d", &a, &b);
    graph[a - 1].pb(b - 1);
  }
  int ans = 0;

  for (int i = 0; i < n; ++i) {
    max_self(ans, find(i, graph));
  }
  printf("%d\n", ans);
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t = 1;
  // cin >> t;
  // scanf("%d",&t);
  while (t--) {
    solve();
  }
}


Fixed submission:
// #include <cmath>
// #include <cstdio>
// #include <vector>
// #include <map>
// #include <iostream>
// #include <algorithm>
// #include <set>
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using vi = vector<int>;
using vb = vector<bool>;
using pli = pair<ll, int>;
#define fi first
#define v vector
#define se second
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
int INF = 1e9 + 7;
ll inf = 1e18 + 7;

const int N = 100010;

void add_self(int &a, int b) { a += b; }

void max_self(int &a, int b) {
  if (b > a)
    a = b;
}

void topsort(int i, v<vi> &graph, vb &visit, stack<int> &res) {
  visit[i] = true;

  for (int x : graph[i]) {
    if (!visit[x])
      topsort(x, graph, visit, res);
  }
  res.push(i);
}

int dp[N];

int find(int i, v<vi> &graph) {
  if (dp[i] != -1)
    return dp[i];
  int res = 0;
  for (int v : graph[i]) {
    max_self(res, 1 + find(v, graph));
  }
  dp[i] = res;
  return res;
}

void solve() {
  for (int i = 0; i < N; ++i) {
    dp[i] = -1;
  }
  int n, m;
  scanf("%d%d", &n, &m);
  v<vi> graph(n);
  for (int i = 0; i < m; ++i) {
    int a, b;
    scanf("%d%d", &a, &b);
    graph[a - 1].pb(b - 1);
  }
  int ans = 0;

  for (int i = 0; i < n; ++i) {
    max_self(ans, find(i, graph));
  }
  printf("%d\n", ans);
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t = 1;
  // cin >> t;
  // scanf("%d",&t);
  while (t--) {
    solve();
  }
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ll long long int
#define Fast                                                                   \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);
using namespace std;

int dp[1000005];

int dfs(vector<int> adj[], bool vis[], int i) {
  if (vis[i])
    return 0;
  if (dp[i] != -1)
    return dp[i];

  vis[i] = true;
  int ans = 0;
  for (int j : adj[i])
    ans = max(ans, 1 + dfs(adj, vis, j));
  vis[i] = false;

  return ans;
}

int main() {
  Fast

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

  memset(dp, -1, sizeof(dp));
  for (int i = 0; i <= v; i++) {
    bool vis[v];
    memset(vis, false, sizeof(vis));
    dp[i] = max(dp[i], dfs(adj, vis, i));
    // cout<<"ans: "<<ans<<endl;
  }

  int ans = 0;
  for (int i = 0; i <= v; i++)
    ans = max(ans, dp[i]);
  cout << ans;
  // 5 5 1 2 1 3 2 3 3 2 2 4   ans 3
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long int
#define Fast                                                                   \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);
using namespace std;

int dp[1000005];

int dfs(vector<int> adj[], bool vis[], int i) {
  if (vis[i])
    return 0;
  if (dp[i] != -1)
    return dp[i];

  vis[i] = true;
  int ans = 0;
  for (int j : adj[i])
    ans = max(ans, 1 + dfs(adj, vis, j));
  vis[i] = false;

  dp[i] = ans;
  return ans;
}

int main() {
  Fast

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

  memset(dp, -1, sizeof(dp));
  for (int i = 0; i <= v; i++) {
    bool vis[v];
    memset(vis, false, sizeof(vis));
    dp[i] = max(dp[i], dfs(adj, vis, i));
    // cout<<"ans: "<<ans<<endl;
  }

  int ans = 0;
  for (int i = 0; i <= v; i++)
    ans = max(ans, dp[i]);
  cout << ans;
  // 5 5 1 2 1 3 2 3 3 2 2 4   ans 3
}
Problem p03166 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
const long long INF = 1LL << 60;

long long dp[1000000];
vector<vector<int>> G;

int rec(int v) {
  if (dp[v] != -1)
    return dp[v];
  int res = 0;

  for (auto nv : G[v]) {
    chmax(res, rec(nv) + 1);
  }

  return dp[v] = res;
}

int main() {
  int n, m;
  cin >> n >> m;
  G.assign(n, vector<int>());
  for (int i = 0; i < n; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    G[x].push_back(y);
  }
  int res = 0;
  for (int i = 0; i < n; i++)
    dp[i] = -1;
  for (int v = 0; v < n; v++)
    chmax(res, rec(v));
  cout << res << endl;
}

Fixed submission:
#include <iostream>
#include <vector>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
const long long INF = 1LL << 60;

long long dp[1000000];
vector<vector<int>> G;

int rec(int v) {
  if (dp[v] != -1)
    return dp[v];
  int res = 0;

  for (auto nv : G[v]) {
    chmax(res, rec(nv) + 1);
  }

  return dp[v] = res;
}

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

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

using namespace std;

const int N = 3000 + 77;
int n, m, d[N], A;
vector<int> a[N];
bool M[N];
void dfs(int v) {
  M[v] = 1;
  d[v] = 1;
  for (int u : a[v]) {
    if (!M[u])
      dfs(u);
    d[v] = max(d[v], d[u] + 1);
  }
  A = max(A, d[v]);
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m;
  for (int u, v, i = 1; i <= m; ++i) {
    cin >> v >> u;
    a[v].push_back(u);
  }
  for (int i = 1; i <= n; ++i)
    if (!M[i])
      dfs(i);
  cout << A - 1;
  return 0;
}


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

using namespace std;

const int N = 100000 + 77;
int n, m, d[N], A;
vector<int> a[N];
bool M[N];
void dfs(int v) {
  M[v] = 1;
  d[v] = 1;
  for (int u : a[v]) {
    if (!M[u])
      dfs(u);
    d[v] = max(d[v], d[u] + 1);
  }
  A = max(A, d[v]);
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m;
  for (int u, v, i = 1; i <= m; ++i) {
    cin >> v >> u;
    a[v].push_back(u);
  }
  for (int i = 1; i <= n; ++i)
    if (!M[i])
      dfs(i);
  cout << A - 1;
  return 0;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
// If there is no outward edge then ,length of longest path=0
#include <bits/stdc++.h>
#define ll long long
using namespace std;
map<ll, list<ll>> graph;
ll dp[100003];
ll solve(ll y) {
  if (dp[y] != -1)
    return dp[y];
  if (graph[y].size() == 0) {
    return 0;
  }
  ll ans = 0;
  for (auto it : graph[y]) {
    ans = max(ans, solve(it));
  }
  return 1 + ans;
}
int main() {
  ll n, m;
  cin >> n >> m;
  for (ll p = 0; p < m; p++) {
    ll x, y;
    cin >> x >> y;
    graph[x].push_back(y);
  }
  ll ans = 0;
  for (ll p = 1; p <= n; p++)
    dp[p] = -1;
  for (ll r = 1; r <= n; r++) {
    ans = max(ans, solve(r));
  }
  cout << ans << endl;
}

Fixed submission:
// If there is no outward edge then ,length of longest path=0
#include <bits/stdc++.h>
#define ll long long
using namespace std;
map<ll, list<ll>> graph;
ll dp[100003];
ll solve(ll y) {
  if (dp[y] != -1)
    return dp[y];
  if (graph[y].size() == 0) {
    return 0;
  }
  ll ans = 0;
  for (auto it : graph[y]) {
    ans = max(ans, solve(it));
  }
  return dp[y] = 1 + ans;
}
int main() {
  ll n, m;
  cin >> n >> m;
  for (ll p = 0; p < m; p++) {
    ll x, y;
    cin >> x >> y;
    graph[x].push_back(y);
  }
  ll ans = 0;
  for (ll p = 1; p <= n; p++)
    dp[p] = -1;
  for (ll r = 1; r <= n; r++) {
    ans = max(ans, solve(r));
  }
  cout << ans << endl;
}
Problem p03166 (C++)

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

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> child;
  vector<int> parent;
  rep(i, n) parent[i] = 0;
  int x, y;
  rep(i, m) {
    cin >> x >> y;
    x--;
    y--;
    parent[y]++;
    child[x].push_back(y);
  }

  vector<int> dp(n);
  queue<int> nodes;
  rep(i, n) {
    if (parent[i] == 0)
      nodes.push(i);
    dp[i] = 0;
  }

  while (nodes.size()) {
    int j = nodes.front();
    nodes.pop();

    if (child[j].size() > 0)
      rep(k, child[j].size()) {
        int c = child[j][k];
        dp[c] = max(dp[j] + 1, dp[c]);
        parent[c]--;
        if (parent[c] == 0)
          nodes.push(c);
      }
  }

  int max = 0;
  rep(i, n) if (max < dp[i]) max = dp[i];
  cout << max << endl;
}


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

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> child(n);
  vector<int> parent(n);
  rep(i, n) parent[i] = 0;
  int x, y;
  rep(i, m) {
    cin >> x >> y;
    x--;
    y--;
    parent[y]++;
    child[x].push_back(y);
  }

  vector<int> dp(n);
  queue<int> nodes;
  rep(i, n) {
    if (parent[i] == 0)
      nodes.push(i);
    dp[i] = 0;
  }

  while (nodes.size()) {
    int j = nodes.front();
    nodes.pop();

    if (child[j].size() > 0)
      rep(k, child[j].size()) {
        int c = child[j][k];
        dp[c] = max(dp[j] + 1, dp[c]);
        parent[c]--;
        if (parent[c] == 0)
          nodes.push(c);
      }
  }

  int max = 0;
  rep(i, n) if (max < dp[i]) max = dp[i];
  cout << max << endl;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define pb push_back
#define ll long long
#define ff first
#define ss second
using namespace std;

int n, m;
vector<int> v[100001];
vector<int> sink;
vector<int> src[100001];
int dp[100001];

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

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> m;
  int x, y;
  for (int i = 0; i < m; i++) {
    cin >> x >> y;
    v[x].pb(y);
    src[y].pb(x);
  }
  for (int i = 1; i <= n; i++) {
    if (v[i].size() == 0) {
      sink.pb(i);
    }
  }
  memset(dp, -1, sizeof(dp));
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (src[i].size() == 0 && v[i].size() != 0) {
      ans = max(ans, fun(i));
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define pb push_back
#define ll long long
#define ff first
#define ss second
using namespace std;

int n, m;
vector<int> v[100001];
vector<int> sink;
vector<int> src[100001];
int dp[100001];

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

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> m;
  int x, y;
  for (int i = 0; i < m; i++) {
    cin >> x >> y;
    v[x].pb(y);
    src[y].pb(x);
  }
  for (int i = 1; i <= n; i++) {
    if (v[i].size() == 0 && src[i].size() != 0) {
      sink.pb(i);
    }
  }
  memset(dp, -1, sizeof(dp));
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (src[i].size() == 0 && v[i].size() != 0) {
      ans = max(ans, fun(i));
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define ll long long
#define lld long double
#define ALL(x) x.begin(), x.end()
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
using namespace std;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;
using PII = pair<int, int>;

const int IINF = 1 << 30;
const ll INF = 1ll << 60;
const ll MOD = 1e9 + 7;
VI dp(101010, -1);
map<int, VI> g;

int dfs(int cur) {
  int ret = 0;
  for (auto x : g[cur]) {
    chmax(ret, 1 + dfs(x));
  }
  dp[cur] = ret;
  return ret;
}

int main(int argc, char const *argv[]) {
  int n, m;
  cin >> n >> m;
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].push_back(b);
  }
  rep(i, n) {
    if (dp[i] == -1) {
      dfs(i);
    }
  }
  int ans = -1;
  rep(i, n) ans = chmax(ans, dp[i]);
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define ll long long
#define lld long double
#define ALL(x) x.begin(), x.end()
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
using namespace std;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;
using PII = pair<int, int>;

const int IINF = 1 << 30;
const ll INF = 1ll << 60;
const ll MOD = 1e9 + 7;
VI dp(101010, -1);
map<int, VI> g;

int dfs(int cur) {
  if (dp[cur] != -1)
    return dp[cur];
  int ret = 0;
  for (auto x : g[cur]) {
    chmax(ret, 1 + dfs(x));
  }
  dp[cur] = ret;
  return ret;
}

int main(int argc, char const *argv[]) {
  int n, m;
  cin >> n >> m;
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].push_back(b);
  }
  rep(i, n) {
    if (dp[i] == -1) {
      dfs(i);
    }
  }
  int ans = -1;
  rep(i, n) ans = chmax(ans, dp[i]);
  cout << ans << endl;
  return 0;
}
Problem p03166 (C++)

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

vector<int> dp;
vector<vector<int>> e;

int f(int x) {
  int r = 0;
  if (e[x].size())
    for (auto p : e[x])
      r = max(r, f(p) + 1);
  else
    r = 0;
  dp[x] = r;
  return r;
}

int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  dp = vector<int>(n, -1);
  e = vector<vector<int>>(n);
  for (int i = 0; i < m; i++) {
    int x, y;
    scanf("%d %d", &x, &y);
    e[--x].push_back(--y);
  }
  int o = 0;
  for (int i = 0; i < n; i++)
    if (dp[i] == -1)
      o = max(o, f(i));
  printf("%d\n", o);
}

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

vector<int> dp;
vector<vector<int>> e;

int f(int x) {
  if (dp[x] != -1)
    return dp[x];
  int r = 0;
  if (e[x].size())
    for (auto p : e[x])
      r = max(r, f(p) + 1);
  else
    r = 0;
  dp[x] = r;
  return r;
}

int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  dp = vector<int>(n, -1);
  e = vector<vector<int>>(n);
  for (int i = 0; i < m; i++) {
    int x, y;
    scanf("%d %d", &x, &y);
    e[--x].push_back(--y);
  }
  int o = 0;
  for (int i = 0; i < n; i++)
    if (dp[i] == -1)
      o = max(o, f(i));
  printf("%d\n", o);
}
Problem p03166 (C++)

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

#define ll long long
#define pb push_back
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define mi map<int, int>
#define mii map<pii, int>
#define all(a) (a).begin(), (a).end()
#define x first
#define y second
#define sz(x) (int)x.size()
#define endl '\n'
#define hell 1000000007
#define rep(i, a, b) for (int i = a; i < b; i++)
using namespace std;
int n, m, vis[100005], dp[100005];
vi v, a[100005];
void dfs(int node) {
  for (auto i : a[node]) {
    if (!vis[i])
      dfs(i);
  }
  v.pb(node);
}
void solve() {
  cin >> n >> m;
  rep(i, 1, m + 1) {
    int x, y;
    cin >> x >> y;
    a[x].pb(y);
  }
  rep(i, 1, n + 1) {
    if (vis[i] == 0) {
      dfs(i);
    }
  }
  int ans = 0;
  for (auto i : v) {
    for (auto j : a[i]) {
      dp[i] = max(dp[i], dp[j] + 1);
    }
    ans = max(ans, dp[i]);
  }
  cout << ans << endl;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}

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

#define ll long long
#define pb push_back
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define mi map<int, int>
#define mii map<pii, int>
#define all(a) (a).begin(), (a).end()
#define x first
#define y second
#define sz(x) (int)x.size()
#define endl '\n'
#define hell 1000000007
#define rep(i, a, b) for (int i = a; i < b; i++)
using namespace std;
int n, m, vis[100005], dp[100005];
vi v, a[100005];
void dfs(int node) {
  vis[node] = 1;
  for (auto i : a[node]) {
    if (!vis[i])
      dfs(i);
  }
  v.pb(node);
}
void solve() {
  cin >> n >> m;
  rep(i, 1, m + 1) {
    int x, y;
    cin >> x >> y;
    a[x].pb(y);
  }
  rep(i, 1, n + 1) {
    if (vis[i] == 0) {
      dfs(i);
    }
  }
  int ans = 0;
  for (auto i : v) {
    for (auto j : a[i]) {
      dp[i] = max(dp[i], dp[j] + 1);
    }
    ans = max(ans, dp[i]);
  }
  cout << ans << endl;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}
Problem p03166 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
#define pb push_back
// #all(x) (x).begin(),(x).end()

// int const N = 1;
int const MOD = 1e9 + 7;

ll bigmod(ll a, ll b) {
  if (b == 0)
    return 1 % MOD;
  ll x = bigmod(a, b / 2);
  x = (x * x) % MOD;
  if (b % 2)
    x = (x * a) % MOD;
  return x;
}

int const N = 3e3 + 10;

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

int dfs(int node) {
  int &r = dp[node];
  if (r != -1)
    return r;

  r = 1;
  int mx = 0;
  for (auto v : g[node]) {
    mx = max(mx, dfs(v));
  }
  r += mx;
  return r;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  memset(dp, -1, sizeof dp);
  cin >> n >> m;

  for (int i = 1; i <= m; i++) {
    int a, b;
    cin >> a >> b;
    g[a].pb(b);
  }
  memset(dp, -1, sizeof dp);

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


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
#define pb push_back
// #all(x) (x).begin(),(x).end()

// int const N = 1;
int const MOD = 1e9 + 7;

ll bigmod(ll a, ll b) {
  if (b == 0)
    return 1 % MOD;
  ll x = bigmod(a, b / 2);
  x = (x * x) % MOD;
  if (b % 2)
    x = (x * a) % MOD;
  return x;
}

int const N = 3e5 + 10;

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

int dfs(int node) {
  int &r = dp[node];
  if (r != -1)
    return r;

  r = 1;
  int mx = 0;
  for (auto v : g[node]) {
    mx = max(mx, dfs(v));
  }
  r += mx;
  return r;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  memset(dp, -1, sizeof dp);
  cin >> n >> m;

  for (int i = 1; i <= m; i++) {
    int a, b;
    cin >> a >> b;
    g[a].pb(b);
  }
  memset(dp, -1, sizeof dp);

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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
// #define int long long
#define deb(x) cout << #x << " = " << x << "\n";
#define maxn 100005
using namespace std;
int dp[maxn];
vector<int> g[maxn];
bitset<maxn> vis;
int dfs(int s) {
  vis[s] = 1;
  for (int ch : g[s]) {
    dfs(ch);
    dp[s] = max(dp[s], 1 + dp[ch]);
  }
  return dp[s];
}

signed main() {

  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    g[x].push_back(y);
  }
  memset(dp, 0, sizeof(dp));
  vis.reset();
  for (int i = 1; i <= n; i++) {
    if (!vis[i])
      int x = dfs(i);
  }
  int ans = *max_element(dp + 1, dp + 1 + n);
  cout << ans << "\n";
}


Fixed submission:
#include <bits/stdc++.h>
// #define int long long
#define deb(x) cout << #x << " = " << x << "\n";
#define maxn 100005
using namespace std;
int dp[maxn];
vector<int> g[maxn];
bitset<maxn> vis;
int dfs(int s) {
  vis[s] = 1;
  for (int ch : g[s]) {
    if (!vis[ch])
      dfs(ch);
    dp[s] = max(dp[s], 1 + dp[ch]);
  }
  return dp[s];
}

signed main() {

  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    g[x].push_back(y);
  }
  memset(dp, 0, sizeof(dp));
  vis.reset();
  for (int i = 1; i <= n; i++) {
    if (!vis[i])
      int x = dfs(i);
  }
  int ans = *max_element(dp + 1, dp + 1 + n);
  cout << ans << "\n";
}
Problem p03166 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
#define all(x) (x).begin(), (x).end()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
// ll gcd(ll a, ll b){return b?gcd(b,a%b):a;}
// ll lcm(ll x, ll y) {return x / gcd(x, y) * y;}
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const ll mod = 1e9 + 7;
const ll inf = 1LL << 60;
const long double pi = 3.14159265358979323846;

// ****************************************CODE***************************************//

ll n, m;
ll dp[100100];
vector<vector<ll>> g;

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

  ll res = 0;
  for (auto j : g[i]) {
    res = max(res, rec(j) + 1);
  }

  return dp[i] = res;
}

int main() {
  cin >> n >> m;
  g.assign(n, vector<ll>());
  for (int i = 0; i < n; i++) {
    ll x, y;
    cin >> x >> y;
    x--;
    y--;
    g[x].push_back(y);
  }
  for (int i = 0; i < 100100; i++)
    dp[i] = -1;

  ll res = 0;
  for (int i = 0; i < n; i++) {
    res = max(res, rec(i));
  }

  cout << res << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
#define all(x) (x).begin(), (x).end()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
// ll gcd(ll a, ll b){return b?gcd(b,a%b):a;}
// ll lcm(ll x, ll y) {return x / gcd(x, y) * y;}
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const ll mod = 1e9 + 7;
const ll inf = 1LL << 60;
const long double pi = 3.14159265358979323846;

// ****************************************CODE***************************************//

ll n, m;
ll dp[100100];
vector<vector<ll>> g;

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

  ll res = 0;
  for (auto j : g[i]) {
    res = max(res, rec(j) + 1);
  }

  return dp[i] = res;
}

int main() {
  cin >> n >> m;
  g.assign(n, vector<ll>());
  for (int i = 0; i < m; i++) {
    ll x, y;
    cin >> x >> y;
    x--;
    y--;
    g[x].push_back(y);
  }
  for (int i = 0; i < 100100; i++)
    dp[i] = -1;

  ll res = 0;
  for (int i = 0; i < n; i++) {
    res = max(res, rec(i));
  }

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

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
#define all(x) (x).begin(), (x).end()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
// ll gcd(ll a, ll b){return b?gcd(b,a%b):a;}
// ll lcm(ll x, ll y) {return x / gcd(x, y) * y;}
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const ll mod = 1e9 + 7;
const ll inf = 1LL << 60;
const long double pi = 3.14159265358979323846;

// ****************************************CODE***************************************//

ll n, m;
vector<vector<ll>> g;
ll dp[100100];

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

  ll res = 0;
  for (auto j : g[i]) {
    res = max(res, rec(j) + 1);
  }

  return res;
}

int main() {
  cin >> n >> m;
  g.assign(n, vector<ll>());
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].push_back(b);
  }

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

  ll res = 0;
  for (int i = 0; i < n; i++) {
    res = max(res, rec(i));
  }

  cout << res << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
#define all(x) (x).begin(), (x).end()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
// ll gcd(ll a, ll b){return b?gcd(b,a%b):a;}
// ll lcm(ll x, ll y) {return x / gcd(x, y) * y;}
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const ll mod = 1e9 + 7;
const ll inf = 1LL << 60;
const long double pi = 3.14159265358979323846;

// ****************************************CODE***************************************//

ll n, m;
vector<vector<ll>> g;
ll dp[100100];

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

  ll res = 0;
  for (auto j : g[i]) {
    res = max(res, rec(j) + 1);
  }

  return dp[i] = res;
}

int main() {
  cin >> n >> m;
  g.assign(n, vector<ll>());
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].push_back(b);
  }

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

  ll res = 0;
  for (int i = 0; i < n; i++) {
    res = max(res, rec(i));
  }

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

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

class Edge {
public:
  long long cost;
  int from, to;
  Edge(long long c, int f, int t) {
    cost = c;
    from = f;
    to = t;
  }
};
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
void readGraph(Graph &g, int E, bool D,
               bool C) { // E:NumofEdges, D:Directed, C:Costed
  for (int i = 0; i < E; ++i) {
    Edge e(0, 0, 0);
    if (!C)
      cin >> e.from >> e.to;
    else
      cin >> e.from >> e.to >> e.cost;
    --e.from;
    --e.to;
    g[e.from].push_back(e);
    if (!D) {
      swap(e.from, e.to);
      g[e.from].push_back(e);
    }
  }
}
int a[100010], ans;

int func(Graph &g, int p) {
  if (a[p] != -1)
    return a[p];
  int res = 0;
  for (int i = 0; i < g[p].size(); ++i)
    res = max(res, 1 + func(g, g[p][i].to));
  return res;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  Graph g(n);
  readGraph(g, m, true, false);
  memset(a, -1, sizeof(a));
  for (int i = 0; i < n; ++i)
    ans = max(ans, func(g, i));
  cout << ans << "\n";
}

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

class Edge {
public:
  long long cost;
  int from, to;
  Edge(long long c, int f, int t) {
    cost = c;
    from = f;
    to = t;
  }
};
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
void readGraph(Graph &g, int E, bool D,
               bool C) { // E:NumofEdges, D:Directed, C:Costed
  for (int i = 0; i < E; ++i) {
    Edge e(0, 0, 0);
    if (!C)
      cin >> e.from >> e.to;
    else
      cin >> e.from >> e.to >> e.cost;
    --e.from;
    --e.to;
    g[e.from].push_back(e);
    if (!D) {
      swap(e.from, e.to);
      g[e.from].push_back(e);
    }
  }
}
int a[100010], ans;

int func(Graph &g, int p) {
  if (a[p] != -1)
    return a[p];
  int res = 0;
  for (int i = 0; i < g[p].size(); ++i)
    res = max(res, 1 + func(g, g[p][i].to));
  return a[p] = res;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  Graph g(n);
  readGraph(g, m, true, false);
  memset(a, -1, sizeof(a));
  for (int i = 0; i < n; ++i)
    ans = max(ans, func(g, i));
  cout << ans << "\n";
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(var, begin, end) for (int var = (begin); var < (end); var++)
#define RFOR(var, begin, end) for (int var = (begin)-1; var >= (end); var--)
#define REP(var, length) FOR(var, 0, length)
#define RREP(var, length) RFOR(var, length, 0)
#define SORT(vector) sort(vector.begin(), vector.end())

const ll MOD = 1e9 + 7;

vector<vector<int>> e;
vector<int> dp;

int DFS(int n) {
  if (dp[n] != -1) {
    return dp[n];
  }

  int ret = -1;
  for (auto to : e[n]) {
    ret = max(ret, DFS(to) + 1);
  }
  return ret;
}

void solve(istream &cin, ostream &cout) {
  int n, m;
  cin >> n >> m;

  e = vector<vector<int>>(n, vector<int>(0));
  dp = vector<int>(n, -1);

  REP(i, m) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    e[x].push_back(y);
  }

  int ans = 0;
  REP(i, n) { ans = max(ans, DFS(i) + 1); }
  cout << ans << endl;
}

#ifndef TEST
int main() { solve(cin, cout); }
#endif


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(var, begin, end) for (int var = (begin); var < (end); var++)
#define RFOR(var, begin, end) for (int var = (begin)-1; var >= (end); var--)
#define REP(var, length) FOR(var, 0, length)
#define RREP(var, length) RFOR(var, length, 0)
#define SORT(vector) sort(vector.begin(), vector.end())

const ll MOD = 1e9 + 7;

vector<vector<int>> e;
vector<int> dp;

int DFS(int n) {
  if (dp[n] != -1) {
    return dp[n];
  }

  int ret = -1;
  for (auto to : e[n]) {
    ret = max(ret, DFS(to) + 1);
  }
  dp[n] = ret;
  return ret;
}

void solve(istream &cin, ostream &cout) {
  int n, m;
  cin >> n >> m;

  e = vector<vector<int>>(n, vector<int>(0));
  dp = vector<int>(n, -1);

  REP(i, m) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    e[x].push_back(y);
  }

  int ans = 0;
  REP(i, n) { ans = max(ans, DFS(i) + 1); }
  cout << ans << endl;
}

#ifndef TEST
int main() { solve(cin, cout); }
#endif
Problem p03166 (C++)

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

using namespace std;

const long long INF = 1LL << 60;

int N, M;              // input
vector<vector<int>> G; // graph

// メモ化再帰
// int dp[100100];
int dp[30];

int rec(int v) {
  if (dp[v] != -1)
    return dp[v]; // if it's updated, use the value

  int res = 0;
  for (auto nv : G[v]) {
    res = max(res, rec(nv) + 1);
  }

  return dp[v] = res; // memo and return
}

int main() {
  cin >> N >> M;
  G.assign(N, vector<int>());

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

  // init(not visted)
  for (int v = 0; v < N; ++v)
    dp[v] = -1;

  // memosaiki
  int res = 0;
  for (int v = 0; v < N; ++v) {
    res = max(res, rec(v));
  }

  cout << res << endl;
}

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

using namespace std;

const long long INF = 1LL << 60;

int N, M;              // input
vector<vector<int>> G; // graph

// メモ化再帰
int dp[100100];

int rec(int v) {
  if (dp[v] != -1)
    return dp[v]; // if it's updated, use the value

  int res = 0;
  for (auto nv : G[v]) {
    res = max(res, rec(nv) + 1);
  }

  return dp[v] = res; // memo and return
}

int main() {
  cin >> N >> M;
  G.assign(N, vector<int>());

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

  // init(not visted)
  for (int v = 0; v < N; ++v)
    dp[v] = -1;

  // memosaiki
  int res = 0;
  for (int v = 0; v < N; ++v) {
    res = max(res, rec(v));
  }

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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
vector<int> dp(100001, -2);
int path(vector<vector<int>> v, int n) {
  if (n == 0) {
    return -1;
  }
  if (dp[n] != -2)
    return dp[n];
  vector<int> t;
  for (int i = 0; i < v[n].size(); i++) {
    if (dp[v[n][i]] == -2)
      dp[v[n][i]] = path(v, v[n][i]);
    t.push_back(dp[v[n][i]]);
  }
  int max = t[0];
  for (int i = 1; i < t.size(); i++)
    if (max < t[i])
      max = t[i];
  dp[n] = 1 + max;
  return dp[n];
}
int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> v(n + 1);
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    vector<int> t = v[x];
    t.push_back(y);
    v[x] = t;
  }
  vector<int> t;
  t.push_back(0);
  v[0] = t;
  for (int i = 1; i <= n; i++)
    (v[i]).push_back(0);
  for (int i = 1; i <= n; i++)
    path(v, i);
  int max = dp[1];
  for (int i = 2; i <= n; i++)
    if (dp[i] > max)
      max = dp[i];
  cout << max << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
vector<int> dp(100001, -2);
int path(vector<vector<int>> &v, int n) {
  if (n == 0) {
    return -1;
  }
  if (dp[n] != -2)
    return dp[n];
  vector<int> t;
  for (int i = 0; i < v[n].size(); i++) {
    if (dp[v[n][i]] == -2)
      dp[v[n][i]] = path(v, v[n][i]);
    t.push_back(dp[v[n][i]]);
  }
  int max = t[0];
  for (int i = 1; i < t.size(); i++)
    if (max < t[i])
      max = t[i];
  dp[n] = 1 + max;
  return dp[n];
}
int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> v(n + 1);
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    vector<int> t = v[x];
    t.push_back(y);
    v[x] = t;
  }
  vector<int> t;
  t.push_back(0);
  v[0] = t;
  for (int i = 1; i <= n; i++)
    (v[i]).push_back(0);
  for (int i = 1; i <= n; i++)
    path(v, i);
  int max = dp[1];
  for (int i = 2; i <= n; i++)
    if (dp[i] > max)
      max = dp[i];
  cout << max << endl;
  return 0;
}
Problem p03166 (C++)

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

#define watch(x) cout << (#x) << " is " << (x) << endl
#define fr(i, n) for (int i = 0; i < n; i++)
#define rep(i, st, en) for (int i = st; i <= en; i++)
#define repn(i, st, en) for (int i = st; i >= en; i--)
#define sq(a) (a * a)
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
ll mod = 1e9 + 7;

vector<vector<int>> g;
vector<int> lp; /// longest Directed path starting at i
vector<bool> vis;

void dfs(int v) {
  int &mx = lp[v];
  vis[v] = 1;
  for (auto u : g[v]) {
    dfs(u);
    mx = max(lp[u] + 1, mx);
  }
}

void solve() {
  int n, m, u, v;
  cin >> n >> m;
  g.resize(n + 1);
  lp.resize(n + 1, 1);
  vis.resize(n + 1, false);

  fr(i, m) {
    cin >> u >> v;
    g[u].push_back(v);
  }

  rep(i, 1, n) {
    if (!vis[i]) {
      dfs(i);
    }
  }

  cout << *max_element(lp.begin() + 1, lp.end()) - 1;
}

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

  ll t = 1;
  //  cin>>t;
  while (t--) {
    solve();
  }

  return 0;
}


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

#define watch(x) cout << (#x) << " is " << (x) << endl
#define fr(i, n) for (int i = 0; i < n; i++)
#define rep(i, st, en) for (int i = st; i <= en; i++)
#define repn(i, st, en) for (int i = st; i >= en; i--)
#define sq(a) (a * a)
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
ll mod = 1e9 + 7;

vector<vector<int>> g;
vector<int> lp; /// longest Directed path starting at i
vector<bool> vis;

void dfs(int v) {
  int &mx = lp[v];
  vis[v] = 1;
  for (auto u : g[v]) {
    if (!vis[u])
      dfs(u);
    mx = max(lp[u] + 1, mx);
  }
}

void solve() {
  int n, m, u, v;
  cin >> n >> m;
  g.resize(n + 1);
  lp.resize(n + 1, 1);
  vis.resize(n + 1, false);

  fr(i, m) {
    cin >> u >> v;
    g[u].push_back(v);
  }

  rep(i, 1, n) {
    if (!vis[i]) {
      dfs(i);
    }
  }

  cout << *max_element(lp.begin() + 1, lp.end()) - 1;
}

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

  ll t = 1;
  //  cin>>t;
  while (t--) {
    solve();
  }

  return 0;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
bool vist[1000001];
int dp[1000001];

void dfs(int s, vector<vector<int>> g) {
  vist[s] = true;
  for (auto v : g[s]) {
    if (!vist[v])
      dfs(v, g);
    dp[s] = max(dp[s], 1 + dp[v]);
  }
  return;
}
int main() {
  // your code goes here
  int n, m, x, y;
  cin >> n >> m;
  vector<vector<int>> g(n + 1);
  for (int i = 1; i <= m; ++i) {
    cin >> x >> y;
    g[x].push_back(y);
  }
  memset(vist, false, sizeof(vist));
  for (int i = 1; i <= n; ++i) {
    if (!vist[i])
      dfs(i, g);
  }
  int ans = dp[1];
  for (int i = 2; i <= n; ++i)
    ans = max(ans, dp[i]);
  cout << ans;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
bool vist[1000001];
int dp[1000001];

void dfs(int s, vector<vector<int>> &g) {
  vist[s] = true;
  for (auto v : g[s]) {
    if (!vist[v])
      dfs(v, g);
    dp[s] = max(dp[s], 1 + dp[v]);
  }
  return;
}
int main() {
  // your code goes here
  int n, m, x, y;
  cin >> n >> m;
  vector<vector<int>> g(n + 1);
  for (int i = 1; i <= m; ++i) {
    cin >> x >> y;
    g[x].push_back(y);
  }
  memset(vist, false, sizeof(vist));
  for (int i = 1; i <= n; ++i) {
    if (!vist[i])
      dfs(i, g);
  }
  int ans = dp[1];
  for (int i = 2; i <= n; ++i)
    ans = max(ans, dp[i]);
  cout << ans;
  return 0;
}
Problem p03166 (C++)

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

#define maxn 5005
#define N 100000
#define inf (int)2e9
#define ll long long
#define ii pair<int, int>
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define REP(i, a, b) for (int i = a; i >= b; i--)

int n, m, run = 1;
vector<int> g[maxn];
int cl[maxn], f1[maxn];

void dfs(int u) {
  cl[u] = run;
  for (auto &v : g[u]) {
    if (cl[v] != run)
      dfs(v);
    f1[u] = max(f1[u], f1[v] + 1);
  }
}

int main() {
  // freopen("main.inp", "r", stdin);
  cin >> n >> m;
  FOR(i, 1, m) {
    int u, v;
    cin >> u >> v;
    g[u].push_back(v);
  }
  FOR(i, 1, n)
  if (cl[i] != run)
    dfs(i);
  int ans = 0;
  FOR(i, 1, n) ans = max(ans, f1[i]);
  cout << ans;
}


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

#define maxn 100005
#define N 100000
#define inf (int)2e9
#define ll long long
#define ii pair<int, int>
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define REP(i, a, b) for (int i = a; i >= b; i--)

int n, m, run = 1;
vector<int> g[maxn];
int cl[maxn], f1[maxn];

void dfs(int u) {
  cl[u] = run;
  for (auto &v : g[u]) {
    if (cl[v] != run)
      dfs(v);
    f1[u] = max(f1[u], f1[v] + 1);
  }
}

int main() {
  // freopen("main.inp", "r", stdin);
  cin >> n >> m;
  FOR(i, 1, m) {
    int u, v;
    cin >> u >> v;
    g[u].push_back(v);
  }
  FOR(i, 1, n)
  if (cl[i] != run)
    dfs(i);
  int ans = 0;
  FOR(i, 1, n) ans = max(ans, f1[i]);
  cout << ans;
}