Problem p03166 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long int lint;
typedef long long int llint;

#define INF 1073741824
#define LINF 4611686018000000000

// Graph

int dp[110000];
int visited[110000];

class Graph {
protected:
  vector<vector<int>> G;
  void dfs(int from, int visited[], void (*func)(int));

public:
  int count;
  Graph(int);
  void add(int, int);
  void dfs(void (*func)(int));
  void print();
  int ans(int);
};

Graph::Graph(int n) {
  count = n;

  vector<int> g;
  for (int i = 0; i < n; i++) {
    G.push_back(g);
  }
}

void Graph::add(int from, int to) {
  G[from].push_back(to);
  G[to].push_back(from);
}

int Graph::ans(int v) {
  visited[v] = 1;
  if (G[v].empty()) {
    dp[v] = 0;
    return dp[v];
  } else if (dp[v] >= 0) {
    return dp[v];
  }
  int m = 0;
  for (int i = 0; i < G[v].size(); i++) {
    m = max(m, ans(G[v][i]) + 1);
  }
  dp[v] = m;
  return m;
}

void Graph::dfs(int from, int visited[], void (*func)(int)) {
  if (visited[from]) {
    return;
  }
  stack<int> s;
  visited[from] = 1;
  s.push(from);
  while (!s.empty()) {
    int v = s.top();
    s.pop();
    (*func)(v);
    for (int i = 0; i < G[v].size(); i++) {
      if (!visited[G[v][i]]) {
        visited[G[v][i]] = 1;
        s.push(G[v][i]);
      }
    }
  }
}

void Graph::dfs(void (*func)(int)) {
  int visited[count];
  for (int i = 0; i < count; i++) {
    visited[i] = 0;
  }
  for (int i = 0; i < count; i++) {
    dfs(i, visited, func);
  }
}

void Graph::print() {
  for (int i = 0; i < G.size(); i++) {
    cout << i << ": ";
    for (int j = 0; j < G[i].size(); j++) {
      cout << G[i][j] << " ";
    }
    cout << endl;
  }
}

class DGraph : public Graph {
public:
  DGraph(int n) : Graph(n){};
  void add(int, int);
};

void DGraph::add(int from, int to) { G[from].push_back(to); }

int n, m;
int x, y;

int main() {
  cin >> n >> m;
  for (int i = 0; i < 110000; i++) {
    dp[i] = -1;
  }
  DGraph g(n);
  for (int i = 0; i < m; i++) {
    cin >> x >> y;
    g.add(x, y);
  }

  for (int i = 1; i <= n; i++) {
    g.ans(i);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    ans = max(ans, dp[i]);
  }
  cout << ans << endl;
  return 0;
}


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

using namespace std;

typedef long int lint;
typedef long long int llint;

#define INF 1073741824
#define LINF 4611686018000000000

// Graph

int dp[110000];
int visited[110000];

class Graph {
protected:
  vector<vector<int>> G;
  void dfs(int from, int visited[], void (*func)(int));

public:
  int count;
  Graph(int);
  void add(int, int);
  void dfs(void (*func)(int));
  void print();
  int ans(int);
};

Graph::Graph(int n) {
  count = n;

  vector<int> g;
  for (int i = 0; i < n; i++) {
    G.push_back(g);
  }
}

void Graph::add(int from, int to) {
  G[from].push_back(to);
  G[to].push_back(from);
}

int Graph::ans(int v) {
  visited[v] = 1;
  if (G[v].empty()) {
    dp[v] = 0;
    return dp[v];
  } else if (dp[v] >= 0) {
    return dp[v];
  }
  int m = 0;
  for (int i = 0; i < G[v].size(); i++) {
    m = max(m, ans(G[v][i]) + 1);
  }
  dp[v] = m;
  return m;
}

void Graph::dfs(int from, int visited[], void (*func)(int)) {
  if (visited[from]) {
    return;
  }
  stack<int> s;
  visited[from] = 1;
  s.push(from);
  while (!s.empty()) {
    int v = s.top();
    s.pop();
    (*func)(v);
    for (int i = 0; i < G[v].size(); i++) {
      if (!visited[G[v][i]]) {
        visited[G[v][i]] = 1;
        s.push(G[v][i]);
      }
    }
  }
}

void Graph::dfs(void (*func)(int)) {
  int visited[count];
  for (int i = 0; i < count; i++) {
    visited[i] = 0;
  }
  for (int i = 0; i < count; i++) {
    dfs(i, visited, func);
  }
}

void Graph::print() {
  for (int i = 0; i < G.size(); i++) {
    cout << i << ": ";
    for (int j = 0; j < G[i].size(); j++) {
      cout << G[i][j] << " ";
    }
    cout << endl;
  }
}

class DGraph : public Graph {
public:
  DGraph(int n) : Graph(n){};
  void add(int, int);
};

void DGraph::add(int from, int to) { G[from].push_back(to); }

int n, m;
int x, y;

int main() {
  cin >> n >> m;
  for (int i = 0; i < 110000; i++) {
    dp[i] = -1;
  }
  DGraph g(n + 1);
  for (int i = 0; i < m; i++) {
    cin >> x >> y;
    g.add(x, y);
  }

  for (int i = 1; i <= n; i++) {
    g.ans(i);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    ans = max(ans, dp[i]);
  }
  cout << ans << endl;
  return 0;
}