Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}

typedef long long int64;
typedef pair<int, int> ii;
const int INF = 1 << 29;
const int MOD = 1e9 + 7;

const int N = 1e5 + 10;
vector<int> a[N];
bool visit[N];
vector<int> topo;
int dp[N];

void DFS(int u) {
  visit[u] = true;
  for (auto &v : a[u]) {
    DFS(v);
  }
  topo.push_back(u);
}

int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  while (m--) {
    int x, y;
    scanf("%d%d", &x, &y);
    --x;
    --y;
    a[x].push_back(y);
  }
  for (int i = 0; i < n; ++i) {
    if (!visit[i])
      DFS(i);
  }
  for (auto &u : topo) {
    dp[u] = 0;
    for (auto &v : a[u]) {
      dp[u] = max(dp[u], dp[v] + 1);
    }
  }
  int ret = 0;
  for (int i = 0; i < n; ++i)
    ret = max(ret, dp[i]);
  printf("%d\n", ret);
  return 0;
}


Fixed submission:
#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}

typedef long long int64;
typedef pair<int, int> ii;
const int INF = 1 << 29;
const int MOD = 1e9 + 7;

const int N = 1e5 + 10;
vector<int> a[N];
bool visit[N];
vector<int> topo;
int dp[N];

void DFS(int u) {
  visit[u] = true;
  for (auto &v : a[u]) {
    if (!visit[v])
      DFS(v);
  }
  topo.push_back(u);
}

int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  while (m--) {
    int x, y;
    scanf("%d%d", &x, &y);
    --x;
    --y;
    a[x].push_back(y);
  }
  for (int i = 0; i < n; ++i) {
    if (!visit[i])
      DFS(i);
  }
  for (auto &u : topo) {
    dp[u] = 0;
    for (auto &v : a[u]) {
      dp[u] = max(dp[u], dp[v] + 1);
    }
  }
  int ret = 0;
  for (int i = 0; i < n; ++i)
    ret = max(ret, dp[i]);
  printf("%d\n", ret);
  return 0;
}