Problem p00442 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;

struct team {
  int num, dep;
};
#define MAX_N 5000
#define MAX_M 100000
int n, m;
vector<int> beat[MAX_N + 1];
team dat[MAX_N + 1];
team win[MAX_N + 1];
bool used[MAX_N + 1];
bool point[MAX_N + 1];
bool exi[MAX_N + 1] = {false};
bool check[MAX_N + 1];

bool comp(const team &t1, const team &t2) { return t1.dep < t2.dep; }
bool comp2(const team &t1, const team &t2) { return t1.dep > t2.dep; }

int dfs(int x) {
  for (int i = 0; i < beat[x].size(); i++) {
    dat[x].dep = max(dat[x].dep, dfs(beat[x][i]) + 1);
    used[beat[x][i]] = true;
  }
  return dat[x].dep + 1;
}

int main() {
  int w, l;

  for (int i = 1; i <= MAX_N; i++) {
    win[i].num = i;
    dat[i].num = i;
  }

  scanf("%d", &n);
  scanf("%d", &m);

  for (int i = 0; i < m; i++) {
    scanf("%d%d", &w, &l);
    beat[w].push_back(l);
    win[w].dep++;
    exi[w] = true;
    exi[l] = true;
  }

  for (int i = 1; i <= n; i++) {
    if (!used[i] && !beat[i].empty()) {
      dat[i].dep = dfs(i) + 1;
      used[i] = true;
    }
  }

  sort(dat + 1, dat + n + 1, comp);

  for (int i = 1; i <= n; i++) {
    int id = dat[i].num;
    for (int j = 0; j < beat[id].size(); j++) {
      win[id].dep = max(win[id].dep, win[beat[id][j]].dep + 1);
    }
    point[win[id].dep] = true;
  }

  sort(win + 1, win + n + 1, comp2);

  // 順位の出力
  int pt1 = 1, pt2 = 1;
  for (int i = n - 1; i >= 0; i--) {
    if (point[i]) {
      while (win[pt1].dep == i && pt1 <= n) {
        printf("%d\n", win[pt1].num);
        pt1++;
      }
    } else {
      while (exi[pt2] && pt2 <= n) {
        pt2++;
      }
      if (pt2 <= n)
        printf("%d\n", pt2);
      pt2++;
    }
  }
  for (int i = 1; i <= n; i++) {
    if (!check[win[i].dep])
      check[win[i].dep] = true;
    else {
      printf("1\n");
      return 0;
    }
  }

  printf("0\n");

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;

struct team {
  int num, dep;
};
#define MAX_N 5000
#define MAX_M 100000
int n, m;
vector<int> beat[MAX_N + 1];
team dat[MAX_N + 1];
team win[MAX_N + 1];
bool used[MAX_N + 1];
bool point[MAX_N + 1];
bool exi[MAX_N + 1] = {false};
bool check[MAX_N + 1];

bool comp(const team &t1, const team &t2) { return t1.dep < t2.dep; }
bool comp2(const team &t1, const team &t2) { return t1.dep > t2.dep; }

int dfs(int x) {
  if (used[x])
    return dat[x].dep;
  for (int i = 0; i < beat[x].size(); i++) {
    dat[x].dep = max(dat[x].dep, dfs(beat[x][i]) + 1);
    used[beat[x][i]] = true;
  }
  return dat[x].dep + 1;
}

int main() {
  int w, l;

  for (int i = 1; i <= MAX_N; i++) {
    win[i].num = i;
    dat[i].num = i;
  }

  scanf("%d", &n);
  scanf("%d", &m);

  for (int i = 0; i < m; i++) {
    scanf("%d%d", &w, &l);
    beat[w].push_back(l);
    win[w].dep++;
    exi[w] = true;
    exi[l] = true;
  }

  for (int i = 1; i <= n; i++) {
    if (!used[i] && !beat[i].empty()) {
      dat[i].dep = dfs(i) + 1;
      used[i] = true;
    }
  }

  sort(dat + 1, dat + n + 1, comp);

  for (int i = 1; i <= n; i++) {
    int id = dat[i].num;
    for (int j = 0; j < beat[id].size(); j++) {
      win[id].dep = max(win[id].dep, win[beat[id][j]].dep + 1);
    }
    point[win[id].dep] = true;
  }

  sort(win + 1, win + n + 1, comp2);

  // 順位の出力
  int pt1 = 1, pt2 = 1;
  for (int i = n - 1; i >= 0; i--) {
    if (point[i]) {
      while (win[pt1].dep == i && pt1 <= n) {
        printf("%d\n", win[pt1].num);
        pt1++;
      }
    } else {
      while (exi[pt2] && pt2 <= n) {
        pt2++;
      }
      if (pt2 <= n)
        printf("%d\n", pt2);
      pt2++;
    }
  }
  for (int i = 1; i <= n; i++) {
    if (!check[win[i].dep])
      check[win[i].dep] = true;
    else {
      printf("1\n");
      return 0;
    }
  }

  printf("0\n");

  return 0;
}