Problem p03166 (C++)

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

int dfs(int currentnode, vector<vector<int>> &g, unordered_set<int> visited,
        unordered_set<int> &currentvisit, int dp[], int &ans) {

  if (visited.find(currentnode) ==
      visited.end()) // already visited this,dp array has the answer
  {
    return dp[currentnode];
  }

  visited.erase(currentnode);      // visited
  currentvisit.erase(currentnode); // visited in local visited

  int currentlongest = 0;

  for (int i = 0; i < g[currentnode].size(); i++) {
    int neighbour = g[currentnode][i];

    if (currentvisit.find(neighbour) !=
        currentvisit.end()) // neighbour is not visited

    {
      currentlongest = max(currentlongest, 1 + dfs(neighbour, g, visited,
                                                   currentvisit, dp, ans));
    }
  }
  currentvisit.insert(currentnode);
  dp[currentnode] = currentlongest;
  ans = max(ans, currentlongest);
  return currentlongest;
}

void findit(vector<vector<int>> &g, int n, int e) {
  unordered_set<int> visited;
  unordered_set<int> currentvisit;
  int dp[n + 1];

  for (int i = 1; i <= n; i++) {
    visited.insert(i);
    currentvisit.insert(i);
    dp[i] = 0; // not solved yet
  }

  int ans = 0;

  for (int i = 1; i <= n; i++) {
    if (visited.find(i) != visited.end()) // this node is never visited
    {
      dfs(i, g, visited, currentvisit, dp, ans);
    }
  }

  cout << ans;
}

int main() {

  int n;
  cin >> n;
  int e;
  cin >> e;

  vector<vector<int>> g(n + 1);

  for (int i = 0; i < e; i++) {
    int e1;
    int e2;
    cin >> e1;
    cin >> e2;
    g[e1].push_back(e2);
  }

  findit(g, n, e);

  return 0;
}


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

int dfs(int currentnode, vector<vector<int>> &g, unordered_set<int> &visited,
        unordered_set<int> &currentvisit, int dp[], int &ans) {

  if (visited.find(currentnode) ==
      visited.end()) // already visited this,dp array has the answer
  {
    return dp[currentnode];
  }

  visited.erase(currentnode);      // visited
  currentvisit.erase(currentnode); // visited in local visited

  int currentlongest = 0;

  for (int i = 0; i < g[currentnode].size(); i++) {
    int neighbour = g[currentnode][i];

    if (currentvisit.find(neighbour) !=
        currentvisit.end()) // neighbour is not visited

    {
      currentlongest = max(currentlongest, 1 + dfs(neighbour, g, visited,
                                                   currentvisit, dp, ans));
    }
  }
  currentvisit.insert(currentnode);
  dp[currentnode] = currentlongest;
  ans = max(ans, currentlongest);
  return currentlongest;
}

void findit(vector<vector<int>> &g, int n, int e) {
  unordered_set<int> visited;
  unordered_set<int> currentvisit;
  int dp[n + 1];

  for (int i = 1; i <= n; i++) {
    visited.insert(i);
    currentvisit.insert(i);
    dp[i] = 0; // not solved yet
  }

  int ans = 0;

  for (int i = 1; i <= n; i++) {
    if (visited.find(i) != visited.end()) // this node is never visited
    {
      dfs(i, g, visited, currentvisit, dp, ans);
    }
  }

  cout << ans;
}

int main() {

  int n;
  cin >> n;
  int e;
  cin >> e;

  vector<vector<int>> g(n + 1);

  for (int i = 0; i < e; i++) {
    int e1;
    int e2;
    cin >> e1;
    cin >> e2;
    g[e1].push_back(e2);
  }

  findit(g, n, e);

  return 0;
}