Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;

void imie() {
  cout << "hello"
       << "\n";
}

const ll INF = 1e18 + 5;

const int mXN = 100005;
int vis[mXN][2];

int dfs(int u, const vector<vector<int>> &dp) {
  if (vis[u][1] == 1)
    return vis[u][0];

  int ans = 0;
  for (int i = 0; i < dp[u].size(); i++) {
    ans = max(ans, dfs(dp[u][i] - 1, dp) + 1);
  }

  return ans;
}

void solve() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> dp(n, vector<int>());
  vector<pair<int, int>> p(n);
  for (int i = 0; i < m; i++) {
    int A, B;
    cin >> A >> B;
    dp[A - 1].push_back(B);
    p[A - 1].first++;
  }

  for (int i = 0; i < n; i++) {
    p[i].second = i;
  }

  sort(p.begin(), p.end());

  int ans1 = 0;
  // call dfs function
  for (int i = 0; i < n; i++) {
    if (p[i].first == 0) {
      vis[p[i].second][0] = 0;
      vis[p[i].second][1] = 1;
    } else {
      int tmp = dfs(p[i].second, dp);
      ans1 = max(tmp, ans1);
      vis[p[i].second][0] = tmp;
      vis[p[i].second][1] = 1;
    }
  }

  cout << ans1;
}

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

  solve();

  /*
  int t;
  cin >> t;
  while (t) {
          solve();
          cout << "\n";
          t--;
  }
  */
}


Fixed submission:
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;

void imie() {
  cout << "hello"
       << "\n";
}

const ll INF = 1e18 + 5;

const int mXN = 100005;
int vis[mXN][2];

int dfs(int u, const vector<vector<int>> &dp) {
  if (vis[u][1] == 1)
    return vis[u][0];

  int ans = 0;
  for (int i = 0; i < dp[u].size(); i++) {
    ans = max(ans, dfs(dp[u][i] - 1, dp) + 1);
    vis[u][0] = ans;
    vis[u][1] = 1;
  }

  return ans;
}

void solve() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> dp(n, vector<int>());
  vector<pair<int, int>> p(n);
  for (int i = 0; i < m; i++) {
    int A, B;
    cin >> A >> B;
    dp[A - 1].push_back(B);
    p[A - 1].first++;
  }

  for (int i = 0; i < n; i++) {
    p[i].second = i;
  }

  sort(p.begin(), p.end());

  int ans1 = 0;
  // call dfs function
  for (int i = 0; i < n; i++) {
    if (p[i].first == 0) {
      vis[p[i].second][0] = 0;
      vis[p[i].second][1] = 1;
    } else {
      int tmp = dfs(p[i].second, dp);
      ans1 = max(tmp, ans1);
      vis[p[i].second][0] = tmp;
      vis[p[i].second][1] = 1;
    }
  }

  cout << ans1;
}

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

  solve();

  /*
  int t;
  cin >> t;
  while (t) {
          solve();
          cout << "\n";
          t--;
  }
  */
}