Problem p03166 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <queue>
#define qmin(x, y) (x = min(x, y))
#define qmax(x, y) (x = max(x, y))
using namespace std;

inline char gc() {
  //	static char buf[100000],*p1,*p2;
  //	return
  //(p1==p2)&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
  return getchar();
}

template <class T> int read(T &ans) {
  ans = 0;
  char ch = gc();
  T f = 1;
  while (!isdigit(ch)) {
    if (ch == EOF)
      return -1;
    if (ch == '-')
      f = -1;
    ch = gc();
  }
  while (isdigit(ch))
    ans = ans * 10 + ch - '0', ch = gc();
  ans *= f;
  return 1;
}

template <class T1, class T2> int read(T1 &a, T2 &b) {
  return read(a) != EOF && read(b) != EOF ? 2 : EOF;
}

template <class T1, class T2, class T3> int read(T1 &a, T2 &b, T3 &c) {
  return read(a, b) != EOF && read(c) != EOF ? 3 : EOF;
}

typedef long long ll;
const int Maxn = 3100;
const int inf = 0x3f3f3f3f;

int to[Maxn], nxt[Maxn], first[Maxn], tot = 1, d[Maxn], n, m, u, v, f[Maxn];
queue<int> q;

inline void add(int u, int v) {
  to[tot] = v;
  nxt[tot] = first[u];
  first[u] = tot++;
}

signed main() {
  //	freopen("test.in","r",stdin);
  read(n, m);
  for (int i = 1; i <= m; i++) {
    read(u, v);
    add(u, v);
    d[v]++;
  }
  for (int i = 1; i <= n; i++)
    if (d[i] == 0)
      q.push(i);
  while (!q.empty()) {
    int now = q.front();
    q.pop();
    for (int i = first[now]; i; i = nxt[i]) {
      qmax(f[to[i]], f[now] + 1);
      d[to[i]]--;
      if (d[to[i]] == 0)
        q.push(to[i]);
    }
  }
  int ans = 0;
  for (int i = 1; i <= n; i++)
    qmax(ans, f[i]);
  printf("%d\n", ans);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <queue>
#define qmin(x, y) (x = min(x, y))
#define qmax(x, y) (x = max(x, y))
using namespace std;

inline char gc() {
  //	static char buf[100000],*p1,*p2;
  //	return
  //(p1==p2)&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
  return getchar();
}

template <class T> int read(T &ans) {
  ans = 0;
  char ch = gc();
  T f = 1;
  while (!isdigit(ch)) {
    if (ch == EOF)
      return -1;
    if (ch == '-')
      f = -1;
    ch = gc();
  }
  while (isdigit(ch))
    ans = ans * 10 + ch - '0', ch = gc();
  ans *= f;
  return 1;
}

template <class T1, class T2> int read(T1 &a, T2 &b) {
  return read(a) != EOF && read(b) != EOF ? 2 : EOF;
}

template <class T1, class T2, class T3> int read(T1 &a, T2 &b, T3 &c) {
  return read(a, b) != EOF && read(c) != EOF ? 3 : EOF;
}

typedef long long ll;
const int Maxn = 110000;
const int inf = 0x3f3f3f3f;

int to[Maxn], nxt[Maxn], first[Maxn], tot = 1, d[Maxn], n, m, u, v, f[Maxn];
queue<int> q;

inline void add(int u, int v) {
  to[tot] = v;
  nxt[tot] = first[u];
  first[u] = tot++;
}

signed main() {
  //	freopen("test.in","r",stdin);
  read(n, m);
  for (int i = 1; i <= m; i++) {
    read(u, v);
    add(u, v);
    d[v]++;
  }
  for (int i = 1; i <= n; i++)
    if (d[i] == 0)
      q.push(i);
  while (!q.empty()) {
    int now = q.front();
    q.pop();
    for (int i = first[now]; i; i = nxt[i]) {
      qmax(f[to[i]], f[now] + 1);
      d[to[i]]--;
      if (d[to[i]] == 0)
        q.push(to[i]);
    }
  }
  int ans = 0;
  for (int i = 1; i <= n; i++)
    qmax(ans, f[i]);
  printf("%d\n", ans);
  return 0;
}