Problem p03166 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>
using namespace std;

#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()

const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;
/*----------------------------------------*/
template <typename T> struct TopologicalSort {
  struct Edge {
    int src, dst;
    T cost;
    Edge(int dst_, T cost_ = 0) : src(-1), dst(dst_), cost(cost_) {}
    Edge(int src_, int dst_, T cost_) : src(src_), dst(dst_), cost(cost_) {}
    inline bool operator<(const Edge &rhs) { return cost < rhs.cost; }
    inline bool operator<=(const Edge &rhs) { return cost <= rhs.cost; }
    inline bool operator>(const Edge &rhs) { return cost > rhs.cost; }
    inline bool operator>=(const Edge &rhs) { return cost >= rhs.cost; }
  };

  vector<vector<Edge>> graph;
  vector<int> order;

  TopologicalSort(int sz_) : sz(sz_), graph(sz) {}

  void add(int src, int dst, T cost = 0) {
    graph[src].emplace_back(Edge(dst, cost));
  }

  bool build() {
    order.clear();
    vector<int> state(sz, 0);
    REP(i, sz) if (state[i] == 0 && !dfs(i, state)) return false;
    reverse(ALL(order));
    return true;
  }

private:
  int sz;

  bool dfs(int ver, vector<int> &state) {
    state[ver] = 1;
    for (Edge e : graph[ver]) {
      if (state[e.dst] == 1)
        return false;
      if (state[e.dst] == 0 && !dfs(e.dst, state))
        return false;
    }
    order.emplace_back(ver);
    state[ver] = -1;
    return true;
  }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  // freopen("input.txt", "r", stdin);

  int n, m;
  cin >> n >> m;
  TopologicalSort<int> ts(n);
  REP(i, m) {
    int x, y;
    cin >> x >> y;
    --x;
    --y;
    ts.add(x, y);
  }
  ts.build();
  vector<int> toposo = ts.order;
  reverse(ALL(toposo));
  vector<int> dp(n, 0);
  for (int i : toposo) {
    for (auto e : ts.graph[i])
      dp[i] = max(dp[i], dp[e.dst] + 1);
  }
  int ans = 0;
  REP(i, n) ans = max(ans, dp[i]);
  cout << ans << '\n';
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>
using namespace std;

#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()

const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;
/*----------------------------------------*/
template <typename T> struct TopologicalSort {
  struct Edge {
    int src, dst;
    T cost;
    Edge(int dst_, T cost_ = 0) : src(-1), dst(dst_), cost(cost_) {}
    Edge(int src_, int dst_, T cost_) : src(src_), dst(dst_), cost(cost_) {}
    inline bool operator<(const Edge &rhs) { return cost < rhs.cost; }
    inline bool operator<=(const Edge &rhs) { return cost <= rhs.cost; }
    inline bool operator>(const Edge &rhs) { return cost > rhs.cost; }
    inline bool operator>=(const Edge &rhs) { return cost >= rhs.cost; }
  };

  vector<vector<Edge>> graph;
  vector<int> order;

  TopologicalSort(int sz_) : sz(sz_), graph(sz_) {}

  void add(int src, int dst, T cost = 0) {
    graph[src].emplace_back(Edge(dst, cost));
  }

  bool build() {
    order.clear();
    vector<int> state(sz, 0);
    REP(i, sz) if (state[i] == 0 && !dfs(i, state)) return false;
    reverse(ALL(order));
    return true;
  }

private:
  int sz;

  bool dfs(int ver, vector<int> &state) {
    state[ver] = 1;
    for (Edge e : graph[ver]) {
      if (state[e.dst] == 1)
        return false;
      if (state[e.dst] == 0 && !dfs(e.dst, state))
        return false;
    }
    order.emplace_back(ver);
    state[ver] = -1;
    return true;
  }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  // freopen("input.txt", "r", stdin);

  int n, m;
  cin >> n >> m;
  TopologicalSort<int> ts(n);
  REP(i, m) {
    int x, y;
    cin >> x >> y;
    --x;
    --y;
    ts.add(x, y);
  }
  ts.build();
  vector<int> toposo = ts.order;
  reverse(ALL(toposo));
  vector<int> dp(n, 0);
  for (int i : toposo) {
    for (auto e : ts.graph[i])
      dp[i] = max(dp[i], dp[e.dst] + 1);
  }
  int ans = 0;
  REP(i, n) ans = max(ans, dp[i]);
  cout << ans << '\n';
}