alexjercan/bugnet huggingface dataset:
Problem p00442 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
using namespace std;
#define F first
#define S second
const int INF = 100000000;

struct Node {
  vector<int> to, from;
};

bool e[5000][5000];

int main() {
  int n, m;
  cin >> n >> m;
  memset(e, 0, sizeof(e));
  // vector<vector<int> > e(n,vector<int>(n));
  vector<Node> node(n);
  for (int i = 0; i < m; ++i) {
    int a, b;
    cin >> a >> b;
    e[--a][--b] = 1;
    node[a].to.push_back(b);
    node[b].from.push_back(a);
  }
  queue<int> S;
  vector<int> L;
  for (int i = 0; i < n; ++i) {
    // cout << node[i].from.size() << endl;
    if (node[i].from.size() == 0)
      S.push(i);
  }
  /*
  for(int i=0; i<S.size(); ++i){
          cout << S[i] << endl;
  }
  */
  bool other = false;
  while (!S.empty()) {
    if (S.size() > 1)
      other = true;
    L.push_back(S.front());
    int ls = S.front();
    S.pop();
    for (int i = 0; i < node[ls].to.size(); ++i) {
      int nn = node[ls].to[i];
      e[ls][nn] = 0;
      bool flag = true;
      for (int j = 0; j < node[nn].from.size(); ++j) {
        if (e[node[nn].from[j]][nn] == 1) {
          flag = false;
          break;
        }
      }
      if (flag)
        S.push(nn);
      // if(node[node[ls].to[i]].from.size()==0) S.push_back(node[ls].to[i]);
    }
  }
  for (int i = 0; i < L.size(); ++i) {
    cout << L[i] + 1 << endl;
  }
  if (other)
    cout << 1 << endl;
  else
    cout << 0 << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
using namespace std;
#define F first
#define S second
const int INF = 100000000;

struct Node {
  vector<int> to, from;
};

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> e(n, vector<int>(n));
  vector<Node> node(n);
  for (int i = 0; i < m; ++i) {
    int a, b;
    cin >> a >> b;
    e[--a][--b] = 1;
    node[a].to.push_back(b);
    node[b].from.push_back(a);
  }
  queue<int> S;
  vector<int> L;
  for (int i = 0; i < n; ++i) {
    // cout << node[i].from.size() << endl;
    if (node[i].from.size() == 0)
      S.push(i);
  }
  /*
  for(int i=0; i<S.size(); ++i){
          cout << S[i] << endl;
  }
  */
  bool other = false;
  while (!S.empty()) {
    if (S.size() > 1)
      other = true;
    L.push_back(S.front());
    int ls = S.front();
    S.pop();
    for (int i = 0; i < node[ls].to.size(); ++i) {
      int nn = node[ls].to[i];
      e[ls][nn] = 0;
      bool flag = true;
      for (int j = 0; j < node[nn].from.size(); ++j) {
        if (e[node[nn].from[j]][nn] == 1) {
          flag = false;
          break;
        }
      }
      if (flag)
        S.push(nn);
      // if(node[node[ls].to[i]].from.size()==0) S.push_back(node[ls].to[i]);
    }
  }
  for (int i = 0; i < L.size(); ++i) {
    cout << L[i] + 1 << endl;
  }
  if (other)
    cout << 1 << endl;
  else
    cout << 0 << endl;
  return 0;
}
Problem p00442 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <vector>
using namespace std;
struct FSD {
  int f, s, d;
};
bool p[5000][5000] = {false};
struct FSD q[5000], t;
int main() {
  int i, j, n, m, a, b, cnt, c = 0;
  scanf("%d%d", &n, &m);
  for (i = 0; i < m; i++) {
    scanf("%d%d", &a, &b);
    p[b - 1][a - 1] = true;
  }
  for (i = 0; i < n; i++) {
    q[i].f = i;
    q[i].d = i;
    for (j = 0; j < n; j++) {
      if (p[i][j])
        q[i].s++;
    }
  }
  for (i = 0; i < n; i++) {
    t.f = q[i].f;
    t.s = q[i].s;
    t.d = q[i].d;
    for (j = i + 1; j < n; j++) {
      if (t.s >= q[j].s && !p[q[j].f][t.f]) {
        t.f = q[j].f;
        t.s = q[j].s;
        t.d = q[j].d;
      }
    }
    cnt = 0;
    for (j = i; j < n; j++) {
      if (q[j].s == t.s)
        cnt++;
      if (p[q[j].f][t.f])
        q[j].s--;
    }
    if (cnt >= 2)
      c = 1;
    q[t.d].f = q[i].f;
    q[t.d].s = q[i].s;
    q[i].f = t.f;
    q[i].s = t.s;
  }
  for (i = 0; i < n; i++)
    printf("%d\\n", q[i].f + 1);
  printf("%d\\n", c);
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <stdio.h>
using namespace std;
#define p first
#define o second
bool q[5000][5000] = {false}, thor = true;
pair<int, int> u[5000], tmp;
int main() {
  int i, j, n, m, c = 0, cnt, a, b, l, r;
  scanf("%d%d", &n, &m);
  for (i = 0; i < m; i++) {
    scanf("%d%d", &a, &b);
    u[b - 1].p++;
    q[b - 1][a - 1] = true;
  }
  for (i = 0; i < n; i++)
    u[i].o = i + 1;
  for (i = 0; i < n; i++) {
    if (thor) {
      for (r = 0; r < n; r++) {
        for (l = r + 1; l < n; l++) {
          if (u[r].p > u[l].p && !q[u[l].o - 1][u[r].o - 1]) {
            tmp.o = u[r].o;
            tmp.p = u[r].p;
            u[r].o = u[l].o;
            u[r].p = u[l].p;
            u[l].o = tmp.o;
            u[l].p = tmp.p;
          }
        }
      }
    }
    thor = false;
    cnt = 1;
    for (j = i + 1; j < n; j++) {
      if (q[u[j].o - 1][u[i].o - 1]) {
        thor = true;
        u[j].p--;
      } else if (u[i].p == u[j].p)
        cnt++;
    }
    if (cnt >= 2)
      c = 1;
  }
  for (i = 0; i < n; i++)
    printf("%d\\n", u[i].o);
  printf("%d\\n", c);
  return 0;
}
Problem p00442 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <stdio.h>

char tuyo[5000][5000];
int kazu[5000];

int main() {
  int n, m;
  scanf("%d", &n);
  scanf("%d", &m);
  for (int i = 0; i < n; i++) {
    for (int ii = 0; ii < n; ii++) {
      tuyo[i][ii] = 1;
    }
  }
  for (int i = 0; i < m; i++) {
    int w, l;
    scanf("%d %d", &w, &l);
    tuyo[l - 1][w - 1] = 0;
  }
  for (int i = 0; i < n; i++) {
    kazu[i] = 0;
    for (int ii = 0; ii < n; ii++) {
      kazu[i] += tuyo[i][ii];
    }
  }
  bool w = false;
  for (int i = 0; i < n; i++) {
    bool max = false;
    int maxb;
    for (int ii = 0; ii < n; ii++) {
      if (kazu[ii] == n) {
        if (max) {
          w = true;
        }
        maxb = ii;
        max = true;
      }
    }
    for (int ii = 0; ii < n; ii++) {
      if (tuyo[maxb][ii] == 1 && tuyo[ii][maxb] == 0) {
        tuyo[ii][maxb] = 1;
        kazu[ii]++;
      }
    }
    kazu[maxb] = -10000;
    printf("%d\\n", maxb + 1);
  }
  if (w) {
    printf("1\\n");
  } else {
    printf("0\\n");
  }
  getchar();
  getchar();
  return 0;
}

Error: MLE

Fixed submission:

#include <stdio.h>

int tuyo[5000][5000];
int kazu[5000];

int main() {
  int n, m;
  scanf("%d", &n);
  scanf("%d", &m);
  for (int i = 0; i < n; i++) {
    for (int ii = 0; ii < n; ii++) {
      tuyo[i][ii] = 1;
    }
  }
  for (int i = 0; i < m; i++) {
    int w, l;
    scanf("%d %d", &w, &l);
    tuyo[l - 1][w - 1] = 0;
  }
  for (int i = 0; i < n; i++) {
    kazu[i] = 0;
    for (int ii = 0; ii < n; ii++) {
      kazu[i] += tuyo[i][ii];
    }
  }
  bool w = false;
  for (int i = 0; i < n; i++) {
    bool max = false;
    int maxb;
    for (int ii = 0; ii < n; ii++) {
      if (kazu[ii] == n) {
        if (max) {
          w = true;
        }
        maxb = ii;
        max = true;
      }
    }
    for (int ii = 0; ii < n; ii++) {
      if (tuyo[maxb][ii] == 1 && tuyo[ii][maxb] == 0) {
        tuyo[ii][maxb] = 1;
        kazu[ii]++;
      }
    }
    kazu[maxb] = -10000;
    printf("%d\\n", maxb + 1);
  }
  if (w) {
    printf("1\\n");
  } else {
    printf("0\\n");
  }
  return 0;
}
Problem p00442 (C++) — buggy submission, verdict: Memory Limit Exceeded

// 0519-Worst Sportwriter
#include <bits/stdc++.h>
using namespace std;
short data[5000][5000];
vector<int> win(5000);
vector<int> unknown(5000);
int main() {
  int n, m;
  cin >> n >> m;
  for (int t = 0; t < m; t++) {
    int i, j;
    cin >> i >> j;
    i--;
    j--;
    data[i][j] = 1;
    data[j][i] = -1;
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (i == j)
        continue;
      if (data[i][j] == 1)
        win[i]++;
      else if (data[i][j] == 0)
        unknown[i]++;
    }
  }
  vector<int> rank(n, 0);
  int another = 0;
  for (int i = 1; i <= n; i++) {
    int next = -1;
    int nwin = 0;
    for (int j = 0; j < n; j++) {
      if (win[j] == (n - i) && unknown[j] == 0) {
        rank[j] = i;
        cout << j + 1 << endl;
        next = -1;
        break;
      } else if (win[j] + unknown[j] == (n - i)) {
        if (rank[j] != 0)
          continue;
        if (next == -1) {
          next = j;
          nwin = win[j];
        } else if (nwin < win[j]) {
          next = j;
          nwin = win[j];
          another = 1;
        } else
          another = 1;
      }
    }
    if (next != -1) {
      rank[next] = i;
      cout << next + 1 << endl;
      for (int j = 0; j < n; j++) {
        if (next == j)
          continue;
        if (data[next][j] == 0) {
          data[next][j] = 1;
          data[j][next] = -1;
          unknown[j]--;
        }
      }
      win[next] += unknown[next];
      unknown[next] = 0;
    }
  }
  cout << another << endl;
  return 0;
}


Error: MLE

Fixed submission:

// 0519-Worst Sportwriter
#include <bits/stdc++.h>
using namespace std;
int data[5000][5000];
vector<int> win(5000);
vector<int> unknown(5000);
int main() {
  int n, m;
  cin >> n >> m;
  for (int t = 0; t < m; t++) {
    int i, j;
    cin >> i >> j;
    i--;
    j--;
    data[i][j] = 1;
    data[j][i] = -1;
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (i == j)
        continue;
      if (data[i][j] == 1)
        win[i]++;
      else if (data[i][j] == 0)
        unknown[i]++;
    }
  }
  vector<int> rank(n, 0);
  int another = 0;
  for (int i = 1; i <= n; i++) {
    int next = -1;
    int nwin = 0;
    for (int j = 0; j < n; j++) {
      if (win[j] == (n - i) && unknown[j] == 0) {
        rank[j] = i;
        cout << j + 1 << endl;
        next = -1;
        break;
      } else if (win[j] + unknown[j] == (n - i)) {
        if (rank[j] != 0)
          continue;
        if (next == -1) {
          next = j;
          nwin = win[j];
        } else if (nwin < win[j]) {
          next = j;
          nwin = win[j];
          another = 1;
        } else
          another = 1;
      }
    }
    if (next != -1) {
      rank[next] = i;
      cout << next + 1 << endl;
      for (int j = 0; j < n; j++) {
        if (next == j)
          continue;
        if (data[next][j] == 0) {
          data[next][j] = 1;
          data[j][next] = -1;
          unknown[j]--;
        }
      }
      win[next] += unknown[next];
      unknown[next] = 0;
    }
  }
  cout << another << endl;
  return 0;
}
Problem p00442 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
#define pb push_back
#define all(c) c.begin(), c.end()

const int MAX_N = 5000;

// win[i]にjが含まれるならiはjに勝った
// win[i]にjが含まれるならiはjに負けた
vector<int> win[MAX_N + 1];
vector<int> lose[MAX_N + 1];

vector<int> ord;
int used[MAX_N + 1] = {0};

// vがordに追加される時点で，
// vから到達可能な全ての頂点，すなわち，vより順位が低いチームは，必ずordに追加されている
void tsort(int v) {
  used[v] = true;
  for (int i = 0; i < lose[v].size(); i++) {
    if (!used[lose[v][i]])
      tsort(lose[v][i]);
  }
  ord.pb(v);
}

int main() {
  int n, m;
  cin >> n >> m;
  while (m--) {
    // 頂点w(winner), l(loser)を入力
    int w, l;
    cin >> w >> l;
    // win[w]にlを含める
    // lose[l]にwを含める
    win[w].pb(l);
    lose[l].pb(w);
  }
  // この時点では誰が1位か分からないので，
  // 誰かに勝っていれば，自分を1位としてトポロジカルソートを行う
  for (int i = 1; i <= n; i++)
    if (!win[i].size())
      tsort(i);

  // "他のソート結果が存在する"フラグ
  bool exi = 0;
  for (int i = 1; i < n; i++) {
    // "i位のチームにi+1位のチームが勝った"という情報が与えられているか？
    bool ok = find(all(lose[ord[i]]), ord[i - 1]) == lose[ord[i]].end();
    exi |= ok;
  }
  // 順位を出力
  for (int i = 0; i < ord.size(); i++) {
    cout << ord[i] << endl;
  }
  // 存在するかどうかを出力
  cout << exi << endl;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
#define pb push_back
#define all(c) c.begin(), c.end()

const int MAX_N = 5000;

// win[i]にjが含まれるならiはjに勝った
// win[i]にjが含まれるならiはjに負けた
vector<int> win[MAX_N + 1];
vector<int> lose[MAX_N + 1];

vector<int> ord;
int used[MAX_N + 1] = {0};

// vがordに追加される時点で，
// vから到達可能な全ての頂点，すなわち，vより順位が低いチームは，必ずordに追加されている
void tsort(int v) {
  used[v] = true;
  for (int i = 0; i < lose[v].size(); i++) {
    if (!used[lose[v][i]])
      tsort(win[v][i]);
  }
  ord.pb(v);
}

int main() {
  int n, m;
  cin >> n >> m;
  while (m--) {
    // 頂点w(winner), l(loser)を入力
    int w, l;
    cin >> w >> l;
    // win[w]にlを含める
    // lose[l]にwを含める
    win[w].pb(l);
    lose[l].pb(w);
  }
  // この時点では誰が1位か分からないので，
  // 誰かに勝っていれば，自分を1位としてトポロジカルソートを行う
  for (int i = 1; i <= n; i++)
    if (!win[i].size())
      tsort(i);

  // "他のソート結果が存在する"フラグ
  bool exi = 0;
  for (int i = 1; i < n; i++) {
    // "i位のチームにi+1位のチームが勝った"という情報が与えられているか？
    bool ok = find(all(lose[ord[i]]), ord[i - 1]) == lose[ord[i]].end();
    exi |= ok;
  }
  // 順位を出力
  for (int i = 0; i < ord.size(); i++) {
    cout << ord[i] << endl;
  }
  // 存在するかどうかを出力
  cout << exi << endl;
}
Problem p00442 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
int cou[5002];
short int lis[5002][5002];
int main() {
  int N, M;
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    cou[b]++;
    lis[a][b] = 1;
    lis[b][a] = -1;
  }
  int lastflg = 0;
  for (int i = 0; i < N; i++) {
    int mina = 100000, minb = 100000, flg = 0;
    for (int j = 0; j < N; j++) {
      if (cou[j] < minb)
        mina = j, minb = cou[j], flg = 0;
      else if (cou[j] == minb)
        flg = 1;
    }
    cout << mina + 1 << endl;
    lastflg = max(lastflg, flg);
    cou[mina] = 1000000;
    for (int j = 0; j < N; j++) {
      lis[mina][j] = 1;
    }
    for (int j = 0; j < N; j++) {
      if (lis[j][mina] == 0 && mina != j) {
        lis[j][mina] = -1;
        cou[j]++;
      }
    }
  }
  cout << lastflg << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <set>
using namespace std;
int cou[5002];
set<int> lis[5002];
int main() {
  int N, M;
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    cou[b]++;
    lis[b].insert(a);
  }
  int lastflg = 0;
  for (int i = 0; i < N; i++) {
    int mina = 100000, minb = 100000, flg = 0;
    for (int j = 0; j < N; j++) {
      if (cou[j] < minb)
        mina = j, minb = cou[j], flg = 0;
      else if (cou[j] == minb)
        flg = 1;
    }
    cout << mina + 1 << endl;
    lastflg = max(lastflg, flg);
    cou[mina] = 1000000;
    lis[mina].clear();
    for (int j = 0; j < N; j++) {
      if (lis[j].find(mina) == lis[j].end() && mina != j) {
        lis[j].insert(mina);
        cou[j]++;
      }
    }
  }
  cout << lastflg << endl;
  return 0;
}
Problem p00442 (C++) — buggy submission, verdict: Runtime Error

// Solution for aoj 0519:Worst Sportswriter
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
using namespace std;
const int MAX_N = 5000, MAX_M = 100000;
int n, m, ran, jud;
vector<int> g[MAX_N + 1], s;
int save[MAX_N + 1], ans[MAX_N + 1];
bool save2[MAX_N + 1];
bool find(int f, int t) {
  for (int i = 0; i < g[f].size(); i++) {
    if (g[f][i] == t)
      return true;
  }
  return false;
}
void dfs(int now) {
  for (int i = 0; i < g[now].size(); i++) {
    if (!save2[g[now][i]]) {
      dfs(g[now][i]);
    }
  }
  ans[ran] = now;
  if (jud == 0 && ran != n && find(ans[ran], ans[ran + 1]) == false) {
    jud = 1;
  }
  ran--;
  save2[now] = 1;
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int winner, looser;
    cin >> winner >> looser;
    g[winner].push_back(looser);
    save[looser]++;
  }
  for (int i = 1; i <= n; i++) {
    if (save[i] == 0) {
      s.push_back(i);
    }
  }
  ran = n;
  for (int i = 0; i < s.size(); i++)
    dfs(s[i]);
  for (int i = 1; i <= n; i++)
    cout << ans[i] << endl;
  cout << jud << endl;
  return 0;
}

Error: 127
/tmp/f42d2762-ef3a-48f0-b619-8a84856ea8c8.out: error while loading shared libraries: libc.so.6: failed to map segment from shared object


Fixed submission:

// Solution for aoj 0519:Worst Sportswriter
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
using namespace std;
const int MAX_N = 5000, MAX_M = 100000;
int n, m, a, b, c, jud = 0;
int res[MAX_N + 1][MAX_N + 1], save[MAX_N + 1][3], ans[MAX_N + 1];
vector<int> v, v2;
void solve(int ran) {
  for (int i = 0; i < v.size(); i++) {
    if (save[v[i]][1] == n - ran) {
      ans[ran] = v[i];
      v.erase(v.begin() + i);
      return;
    }
  }
  for (int i = 0; i < v.size(); i++) {
    if (save[v[i]][2] <= ran - 1)
      v2.push_back(i);
  }
  a = v2.size();
  b = v2.size();
  for (int i = 0; i < a; i++) {
    for (int j = 0; j < b; j++) {
      if (i != j) {
        if (res[v[v2[i]]][v[v2[j]]] == -1) {
          v2.erase(v2.begin() + i);
          break;
        }
      }
    }
  }
  if (v2.size() >= 2)
    jud = 1;
  sort(v2.begin(), v2.end(), greater<int>());
  for (int i = 0; i < v2.size(); i++) {
    ans[ran + i] = v[v2[i]];
    v.erase(v.begin() + v2[i]);
    c++;
  }
  v2.clear();
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    res[a][b] = 1;
    res[b][a] = -1;
    save[a][1]++;
    save[a][0]++;
    save[b][2]++;
    save[b][0]++;
  }
  for (int i = 1; i <= n; i++) {
    v.push_back(i);
  }
  for (int i = 1; i <= n; i++) {
    c = 0;
    solve(i);
    if (c)
      i += c - 1;
  }
  for (int i = 1; i <= n; i++)
    cout << ans[i] << endl;
  cout << jud << endl;
}
Problem p00442 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <vector>

#define FOR(i, a) for (i = 0; i < (a); i++)
#define FORM(i, a) for (i = (a)i >= 0; i--)
using namespace std;

vector<int> E[5050];
int indeg[5050];
queue<int> q;

void bfs(int s) {
  int i, j;

  FOR(i, E[s].size()) {
    indeg[E[s][i]]--;
    if (indeg[E[s][i]] == 0) {
      q.push(E[s][i]);
    }
  }
}

void ranking(int N) {
  int i, j, k, l, flag = 0;

  FOR(i, N) {
    if (indeg[i + 1] == 0) {
      q.push(i + 1);
      indeg[i + 1]--;
    }
  }

  if (q.size() > 1)
    flag = 1;

  while (q.size() != 0) {
    cout << q.front() << endl;
    bfs(q.front());
    q.pop();
    if (q.size() > 1) {
      flag = 1;
    }
  }

  cout << flag << endl;
}

int main() {
  int i, j, N, K, m, n;

  cin >> n >> m;

  FOR(K, m) {
    cin >> i >> j;
    E[i].push_back(j);
    indeg[j]++;
  }

  ranking(n);

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <vector>

#define FOR(i, a) for (i = 0; i < (a); i++)
#define FORM(i, a) for (i = (a)i >= 0; i--)
using namespace std;

vector<int> E[1010];
int indeg[1010];
queue<int> q;

void bfs(int s) {
  int i, j;

  FOR(i, E[s].size()) {
    indeg[E[s][i]]--;
    if (indeg[E[s][i]] == 0) {
      q.push(E[s][i]);
    }
  }
}

void ranking(int N) {
  int i, j, k, l, flag = 0;

  FOR(i, N) {
    if (indeg[i + 1] == 0) {
      q.push(i + 1);
      indeg[i + 1]--;
    }
  }

  if (q.size() > 1)
    flag = 1;

  while (q.size() != 0) {
    cout << q.front() << endl;
    bfs(q.front());
    q.pop();
    if (q.size() > 1) {
      flag = 1;
    }
  }

  cout << flag << endl;
}

int main() {
  int i, j, N, K, m, n;

  cin >> n >> m;

  FOR(K, m) {
    cin >> i >> j;
    E[i].push_back(j);
    indeg[j]++;
  }

  ranking(n);

  return 0;
}
Problem p00442 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (int i = (j); (i) < (k); ++i)
#define BW(a, x, b) ((a) <= (x) && (x) <= (b))
#define ALL(v) (v).begin(), (v).end()
#define LENGTHOF(x) (sizeof(x) / sizeof(*(x)))
#define AFILL(a, b) fill((int *)a, (int *)(a + LENGTHOF(a)), b)
#define SQ(x) ((x) * (x))
#define Mod(x, mod) (((x)+(mod)%(mod))
#define MP make_pair
#define PB push_back
#define Fi first
#define Se second
#define INF (1 << 29)
#define EPS 1e-10
#define MOD 1000000007

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

short G[5001][5001];
int N, M;
int lose[5001];
int res[5001];

int main() {
  scanf("%d%d", &N, &M);
  int x, y;
  rep(i, M) {
    scanf("%d%d", &x, &y);
    x--;
    y--;
    G[x][y] = 1;
    G[y][x] = -1;
    lose[y]++;
  }

  int f = 0;
  int cnt, t;
  rep(i, N) {
    cnt = 0;
    rep(j, N) if (lose[j] == 0) {
      if (!cnt) {
        t = j;
        lose[j] = -1;
      }
      cnt++;
    }
    if (cnt > 1)
      f = 1;
    res[i] = t;
    rep(j, N) {
      if (G[j][t] == -1)
        lose[j]--;
    }
  }

  rep(i, N) printf("%d\\n", res[i] + 1);
  printf("%d\\n", f);
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (int i = (j); (i) < (k); ++i)
#define BW(a, x, b) ((a) <= (x) && (x) <= (b))
#define ALL(v) (v).begin(), (v).end()
#define LENGTHOF(x) (sizeof(x) / sizeof(*(x)))
#define AFILL(a, b) fill((int *)a, (int *)(a + LENGTHOF(a)), b)
#define SQ(x) ((x) * (x))
#define Mod(x, mod) (((x)+(mod)%(mod))
#define MP make_pair
#define PB push_back
#define Fi first
#define Se second
#define INF (1 << 29)
#define EPS 1e-10
#define MOD 1000000007

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

int G[5001][5001];
int N, M;
int lose[5001];

int main() {
  scanf("%d%d", &N, &M);
  int x, y;
  rep(i, M) {
    scanf("%d%d", &x, &y);
    x--;
    y--;
    G[x][y] = 1;
    G[y][x] = -1;
    lose[y]++;
  }

  int res[5001];
  int f = 0;
  rep(i, N) {
    int cnt = 0, t;
    rep(j, N) if (lose[j] == 0) {
      if (!cnt) {
        t = j;
        lose[j] = -1;
      }
      cnt++;
    }
    if (cnt > 1)
      f = 1;
    res[i] = t;
    rep(j, N) {
      if (G[j][t] == -1)
        lose[j]--;
    }
  }

  rep(i, N) printf("%d\\n", res[i] + 1);
  printf("%d\\n", f);
  return 0;
}