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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, st, n) for (int i = st; i < n; ++i)
using pii = pair<int, int>;
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)
#define inf (int)(1e9 + 7)
#define mod (ll)(1e9 + 7)
#define eb emplace_back

int table[10007];
int main() {
  fill_n(table, 10007, inf);
  int V, E;
  cin >> V >> E;
  if (E == 0) {
    rep(i, V) cout << i << endl;
    return 0;
  }
  vector<int> v[V];
  rep(i, E) {
    int s, t;
    cin >> s >> t;
    v[t].eb(s);
  }
  vector<int> ans;
  int c = 0;
  rep(i, V) if (v[i].size() == 0) {
    table[i] = 1;
    ans.eb(i);
    c++;
  }
  while (1) {
    int wflg = 0;
    int flg = 0;
    rep(k, V) {
      // cout << "k " << k << endl;
      if (table[k] != inf)
        continue;
      flg = 0;
      rep(i, v[k].size()) {
        int bef = v[k][i];
        if (table[bef] == inf) {
          flg = 1;
          break;
        }
      }
      if (flg)
        continue;
      ans.eb(k);
      table[k] = 1;
      c++;
      if (c == V) {
        wflg = 1;
        break;
      }
    }
    // cout << "wflg " << wflg << endl;
    if (wflg)
      break;
  }
  rep(i, V) cout << ans[i] << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, st, n) for (int i = st; i < n; ++i)
using pii = pair<int, int>;
const int inf = 1e9 + 7;
int dy[] = {1, 1, 0, -1, -1, 0};
int dx[] = {1, 0, -1, -1, 0, 1};
#define ceil(a, b) a / b + !!(a % b)

struct Node {
  vector<int> parent;
};
int main() {
  int v, e;
  cin >> v >> e;
  Node node[v];
  rep(i, e) {
    int s, t;
    cin >> s >> t;
    node[t].parent.push_back(s);
  }
  int check[v];
  rep(i, v) check[i] = 0;
  // vector<int> comb; 1組だけ出せばいい
  queue<int> q;
  rep(i, v) if (node[i].parent.size() == 0) {
    q.push(i);
    cout << i << endl;
    check[i] = 1;
  }
  while (!q.empty()) {
    int now = q.front();
    // cout << now << endl;
    rep(i, v) {
      if (check[i] == 1)
        continue;
      rep(j, node[i].parent.size()) {
        if (node[i].parent[j] == now) {
          node[i].parent[j] = -1;
        }
      }
    }
    q.pop();
    rep(i, v) {
      if (check[i] == 1)
        continue;
      rep(j, node[i].parent.size()) {
        if (node[i].parent[j] != -1)
          break;
        if (j == node[i].parent.size() - 1) {
          q.push(i);
          cout << i << endl;
          check[i] = 1;
        }
      }
    }
  }
}
Problem p02370 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <queue>
#include <vector>
using namespace std;

static const int MAX = 10000;
static const int WHITE = 0;
static const int BLACK = 1;

int n, e;
int indeg[MAX], color[MAX];
vector<int> L[MAX];
vector<int> vec;

void bfs(int u) {
  queue<int> Q;
  Q.push(u);
  color[u] = BLACK;
  while (!Q.empty()) {
    int uu = Q.front();
    Q.pop();
    vec.push_back(uu);
    for (int i = 0; i < L[uu].size(); i++) {
      int v = L[uu][i];
      indeg[v]--;
      if (indeg[v] == 0) {
        color[v] = BLACK;
        Q.push(v);
      }
    }
  }
}

void TopoligicalSort() {
  for (int i = 0; i < n; i++) {
    if (color[i] == WHITE && indeg[i] == 0) {
      bfs(i);
    }
  }
}

int main() {
  cin >> n >> e;
  int s, t;

  for (int i = 0; i < n; i++) {
    color[i] = WHITE;
    indeg[i] = 0;
  }

  for (int i = 0; i < e; i++) {
    cin >> s >> t;
    indeg[t]++;
    L[s].push_back(t);
  }

  TopoligicalSort();

  for (int i = 0; i < n; i++) {
    cout << vec[i] << endl;
  }

  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
#include <queue>
#include <vector>
using namespace std;

static const int MAX = 1000;
static const int WHITE = 0;
static const int BLACK = 1;

int n, e;
int indeg[MAX], color[MAX];
vector<int> L[MAX];
vector<int> vec;

void bfs(int u) {
  queue<int> Q;
  Q.push(u);
  color[u] = BLACK;
  while (!Q.empty()) {
    int uu = Q.front();
    Q.pop();
    vec.push_back(uu);
    for (int i = 0; i < L[u].size(); i++) {
      int v = L[uu][i];
      indeg[v]--;
      if (indeg[v] == 0) {
        color[v] = BLACK;
        Q.push(v);
      }
    }
  }
}

void TopoligicalSort() {
  for (int i = 0; i < n; i++) {
    if (color[i] == WHITE && indeg[i] == 0) {
      bfs(i);
    }
  }
}

int main() {
  cin >> n >> e;
  int s, t;

  for (int i = 0; i < n; i++) {
    color[i] = WHITE;
  }

  for (int i = 0; i < e; i++) {
    cin >> s >> t;
    indeg[t]++;
    L[s].push_back(t);
  }

  TopoligicalSort();

  for (int i = 0; i < n; i++) {
    if (i)
      cout << ' ';
    cout << vec[i];
  }
  cout << endl;

  return 0;
}
Problem p02370 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define GET_MACRO(a, b, c, NAME, ...) NAME
#define rep(...) GET_MACRO(__VA_ARGS__, rep3, rep2)(__VA_ARGS__)
#define rep2(i, a) rep3(i, 0, a)
#define rep3(i, a, b) for (int i = (a); i < (b); i++)
#define repr(...) GET_MACRO(__VA_ARGS__, repr3, repr2)(__VA_ARGS__)
#define repr2(i, a) repr3(i, 0, a)
#define repr3(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define chmin(a, b) ((b) < a && (a = (b), true))
#define chmax(a, b) (a < (b) && (a = (b), true))
using namespace std;
typedef long long ll;

struct Toposort {
  vector<vector<int>> g;
  vector<int> ord, indeg;
  Toposort(int n) : g(n), ord(n), indeg(n) {}
  void add(int u, int v) { g[u].push_back(v); }
  void build() {
    for (auto &v : g)
      for (int w : v)
        indeg[w]++;
    queue<int> q;
    rep(i, g.size()) if (indeg[i] == 0) q.push(i);
    int k = 0;
    while (!q.empty()) {
      int v = q.front();
      q.pop();
      ord[k++] = v;
      for (int w : g[v]) {
        indeg[w]--;
        if (indeg[w] == 0)
          q.push(w);
      }
    }
  }
};

template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  rep(i, v.size()) {
    if (i)
      os << " ";
    os << v[i];
  }
  return os;
}

int main() {
  int n, m;
  cin >> n >> m;
  Toposort ts(n);
  rep(i, m) {
    int u, v;
    cin >> u >> v;
    ts.add(u, v);
  }
  ts.build();
  for (auto v : ts.ord) {
    cout << v << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <cassert>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

using Weight = int;
using Capacity = int;
struct Edge {
  int src, dst;
  Weight weight;
  Edge(int s, int d, Weight w) : src(s), dst(d), weight(w) {}
};
using Edges = vector<Edge>;
using Graph = vector<Edges>;
using Array = vector<Weight>;
using Matrix = vector<Array>;

vector<int> topologicalSort(const Graph &g) {
  int n = g.size();
  vector<int> in(n), res, vis(n);
  for (int i = 0; i < n; ++i)
    for (auto &e : g[i])
      ++in[e.dst];
  for (int i = 0; i < n; ++i) {
    if (in[i] || vis[i])
      continue;
    vis[i] = true;
    res.push_back(i);
    queue<int> q;
    q.push(i);
    while (!q.empty()) {
      int p = q.front();
      q.pop();
      for (auto &e : g[p]) {
        int np = e.dst;
        --in[np];
        if (in[np] || vis[np])
          continue;
        vis[np] = true;
        q.push(np);
        res.push_back(np);
      }
    }
  }
  return res;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  int n, m;
  cin >> n >> m;
  Graph g(n);
  for (int i = 0; i < n; i++) {
    int s, t;
    cin >> s >> t;
    g[s].emplace_back(s, t, 0);
  }
  vector<int> ord = topologicalSort(g);
  if (ord.size() != n)
    while (1)
      ;
  for (int i = 0; i < n; ++i) {
    cout << ord[i] << '\\n';
  }
}
Problem p02370 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
struct vertex {
  vector<int> E; // cost,to
};
#define MAX_V 10000
#define MAX_E 100000

bool isVisited[MAX_V];
bool isChecked[MAX_V];
vertex V[MAX_V];
vector<int> A;

void rec(int i) {
  isChecked[i] = true;
  for (auto &e : V[i].E) {
    if (!isChecked[e])
      rec(e);
  }
  A.push_back(i);
  return;
}

int main() {
  int nv, ne;
  cin >> nv >> ne;

  for (int i = 0; i < ne; i++) {
    int s, t;
    cin >> s >> t;
    V[s].E.emplace_back(t);
  }

  fill(isChecked, isChecked + MAX_V, false);
  for (int i = 0; i < nv; i++) {
    if (!isChecked[i])
      rec(i);
  }
  for (auto ite = A.rbegin(); ite != A.rend(); ite++)
    cout << *ite << endl;
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
struct vertex {
  vector<int> E; // cost,to
};
#define MAX_V 100
#define MAX_E 1000

bool isVisited[MAX_V];
bool isChecked[MAX_V];
vertex V[MAX_V];
vector<int> A;

void rec(int i) {
  isChecked[i] = true;
  for (auto &e : V[i].E) {
    if (!isChecked[e])
      rec(e);
  }
  A.push_back(i);
  return;
}

int main() {
  int nv, ne;
  cin >> nv >> ne;

  for (int i = 0; i < ne; i++) {
    int s, t;
    cin >> s >> t;
    V[s].E.emplace_back(t);
  }

  fill(isChecked, isChecked + MAX_V, false);
  for (int i = 0; i < nv; i++) {
    if (isChecked[i])
      continue;
    rec(i);
  }
  for (auto ite = A.rbegin(); ite != A.rend(); ite++)
    cout << *ite << endl;
  return 0;
}
Problem p02370 (C++) — buggy submission, verdict: Runtime Error


// O(|E| + |V|)

#include <algorithm>
#include <cassert>
#include <queue>
#include <vector>

using namespace std;

class TopologicalSort {
#define MAX_V 10001
private:
  struct Edge {
    int to;
    Edge(int to) : to(to) {}
  };

  int V;
  vector<Edge> G[MAX_V];

public:
  TopologicalSort(int V) : V(V) {
    if (V >= MAX_V) {
      assert(false);
    }
  }

  void AddEdge(int from, int to) { G[from].push_back(Edge(to)); }

  vector<int> Solve() {
    int k = 0;
    vector<int> ord(V), in(V);
    for (int i = 0; i < V; i++) {
      for (auto &e : G[i]) {
        in[e.to]++;
      }
    }
    queue<int> q;
    for (int i = 0; i < V; i++) {
      if (in[i] == 0) {
        q.push(i);
      }
    }
    while (!q.empty()) {
      int v = q.front();
      q.pop();
      ord[k++] = v;
      for (auto &e : G[v]) {
        if (--in[e.to] == 0) {
          q.push(e.to);
        }
      }
    }

    return (*max_element(in.begin(), in.end()) == 0) ? ord : vector<int>();
  }
};

#include <iostream>

int main() {
  int V, E;
  cin >> V >> E;
  TopologicalSort ts(V);

  for (int i = 0; i < E; i++) {
    int s, t;
    cin >> s >> t;
    ts.AddEdge(s, t);
  }

  vector<int> ans = ts.Solve();

  for (int i = 0; i < ans.size(); i++) {
    cout << ans[i] << endl;
  }
  return 0;
}

Error: 0

Fixed submission:


// O(|E| + |V|)

#include <algorithm>
#include <cassert>
#include <queue>
#include <vector>

using namespace std;

class TopologicalSort {
#define MAX_V 101
private:
  struct Edge {
    int to;
    Edge(int to) : to(to) {}
  };

  int V;
  vector<Edge> G[MAX_V];

public:
  TopologicalSort(int V) : V(V) {
    if (V >= MAX_V) {
      assert(false);
    }
  }

  void AddEdge(int from, int to) { G[from].push_back(Edge(to)); }

  vector<int> Solve() {
    int k = 0;
    vector<int> ord(V), in(V);
    for (int i = 0; i < V; i++) {
      for (auto &e : G[i]) {
        in[e.to]++;
      }
    }
    queue<int> q;
    for (int i = 0; i < V; i++) {
      if (in[i] == 0) {
        q.push(i);
      }
    }
    while (!q.empty()) {
      int v = q.front();
      q.pop();
      ord[k++] = v;
      for (auto &e : G[v]) {
        if (--in[e.to] == 0) {
          q.push(e.to);
        }
      }
    }

    return (*max_element(in.begin(), in.end()) == 0) ? ord : vector<int>();
  }
};

#include <iostream>

int main() {
  int V, E;
  cin >> V >> E;
  TopologicalSort ts(V);

  for (int i = 0; i < E; i++) {
    int s, t;
    cin >> s >> t;
    ts.AddEdge(s, t);
  }

  vector<int> ans = ts.Solve();

  for (int i = 0; i < ans.size(); i++) {
    cout << ans[i] << endl;
  }
  return 0;
}
Problem p02370 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = n - 1; i >= 0; i--)

#define PB push_back
#define INF INT_MAX / 3
#define ALL(a) (a).begin(), (a).end()
#define CLR(a) memset(a, 0, sizeof(a))

typedef long long int ll;

using namespace std;

int main() {
  int V, E;
  cin >> V >> E;
  vector<int> s(E);
  vector<int> t(E);
  vector<int> res;
  vector<vector<int>> v(V);
  vector<vector<int>> rv(V);
  vector<bool> used(V, false);
  int count = 0;

  REP(i, E) scanf("%d%d", &s[i], &t[i]);
  REP(i, E) v[s[i]].PB(t[i]);
  REP(i, E) rv[t[i]].PB(s[i]);
  REP(i, V) {
    REP(j, V) {
      if (used[j])
        continue;
      if (rv[j].size() == 0) {
        res.PB(j);
        used[j] = true;
        int size = v[j].size();
        REP(k, size) rv[v[j][k]].erase(rv[v[j][k]].begin());
        fill(ALL(v[j]), 0);
        break;
      }
    }
  }

  REP(i, V) cout << res[i] << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = n - 1; i >= 0; i--)

#define PB push_back
#define INF INT_MAX / 3
#define ALL(a) (a).begin(), (a).end()
#define CLR(a) memset(a, 0, sizeof(a))

typedef long long int ll;

using namespace std;

int main() {
  int V, E;
  cin >> V >> E;
  vector<int> s(E);
  vector<int> t(E);
  vector<int> res;
  vector<vector<int>> v(V, vector<int>(V, 0));
  vector<bool> used(V, false);
  int count = 0;

  REP(i, E) scanf("%d%d", &s[i], &t[i]);
  REP(i, E) v[s[i]][t[i]] = 1;
  REP(i, V) {
    REP(j, V) {
      if (used[j])
        continue;
      count = 0;
      REP(k, V) if (v[k][j]) count++;
      if (count == 0) {
        res.PB(j);
        used[j] = true;
        REP(k, V) v[j][k] = 0;
        break;
      }
    }
  }

  REP(i, V) cout << res[i] << endl;

  return 0;
}
Problem p02370 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;

#define N 100000
static const long long FAR = (1L << 31);

static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;

vector<int> G[N];
vector<int> out;
bool Visited[N];
int n, m;
int indeg[N];

void bfs(int s) {
  queue<int> q;
  q.push(s);
  Visited[s] = true;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    out.push_back(u);
    for (int i = 0; i < G[u].size(); ++i) {
      int v = G[u][i];
      indeg[v]--;
      if (indeg[v] == 0 && !Visited[v]) {
        Visited[v] = true;
        q.push(v);
      }
    }
  }
}

void tsort() {
  for (int i = 0; i < n; ++i)
    indeg[i] = 0;

  for (int u = 0; u < n; ++u) {
    for (int j = 0; j < G[u].size(); ++j) {
      int v = G[u][j];
      indeg[v]++;
    }
  }

  for (int u = 0; u < n; ++u)
    if (indeg[u] == 0 && !Visited[u])
      bfs(u);
  for (int i = 0; i < out.size(); ++i)
    cout << out[i] << endl;
}

int main() {
  cin >> n >> m;
  for (int i = 0; i < n; ++i)
    Visited[i] = false;
  for (int i = 0; i < m; ++i) {
    int u, v;
    cin >> u >> v;
    G[u].push_back(v);
  }
  tsort();
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;

#define N 100
static const long long FAR = (1L << 31);

static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;

vector<int> G[N];
vector<int> out;
bool Visited[N];
int n, m;
int indeg[N];

void bfs(int s) {
  queue<int> q;
  q.push(s);
  Visited[s] = true;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    out.push_back(u);
    for (int i = 0; i < G[u].size(); ++i) {
      int v = G[u][i];
      indeg[v]--;
      if (indeg[v] == 0 && !Visited[v]) {
        Visited[v] = true;
        q.push(v);
      }
    }
  }
}

void tsort() {
  for (int i = 0; i < n; ++i)
    indeg[i] = 0;

  for (int u = 0; u < n; ++u) {
    for (int j = 0; j < G[u].size(); ++j) {
      int v = G[u][j];
      indeg[v]++;
    }
  }

  for (int u = 0; u < n; ++u)
    if (indeg[u] == 0 && !Visited[u])
      bfs(u);
  for (int i = 0; i < out.size(); ++i)
    cout << out[i] << endl;
}

int main() {
  cin >> n >> m;
  for (int i = 0; i < n; ++i)
    Visited[i] = false;
  for (int i = 0; i < m; ++i) {
    int u, v;
    cin >> u >> v;
    G[u].push_back(v);
  }
  tsort();
  return 0;
}
Problem p02370 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <utility>
#include <vector>
using namespace std;

int main() {
  int V, E;
  cin >> V >> E;
  vector<pair<int, int>> edge(E);
  for (int i = 0; i < E; i++) {
    cin >> edge[i].first >> edge[i].second;
  }
  vector<int> ans;
  vector<int> state(V, 1);
  // 0: edge[i].secondに載っているため設定できない
  // 1: 設定できる状態
  // 2: すでに設定済みなので除外

  for (int k = 0; k < V; k++) {
    for (int i = 0; i < V; i++) {
      if (state[i] != 2)
        state[i] = 1;
    }
    for (int i = 0; i < edge.size(); i++) {
      state[edge[i].second] = 0;
    }
    for (int i = 0; i < V; i++) {
      if (state[i] == 1) {
        ans.push_back(i);
        state[i] = 2;
        for (int j = 0; j < edge.size(); j++) {
          if (edge[j].first == i) {
            edge[j] = edge.back();
            edge.pop_back();
            j--;
          }
        }
        break;
      }
    }
  }
  for (int i = 0; i < ans.size(); i++) {
    cout << ans[i] << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <list>
#include <utility>
#include <vector>
using namespace std;

int main() {
  int V, E;
  cin >> V >> E;
  list<pair<int, int>> edge;
  for (int i = 0; i < E; i++) {
    int s, t;
    cin >> s >> t;
    edge.push_back(make_pair(s, t));
  }
  vector<int> ans;
  vector<int> state(V, 1);
  // 0: edge[i].secondに載っているため設定できない
  // 1: 設定できる状態
  // 2: すでに設定済みなので除外

  for (int k = 0; k < V; k++) {
    for (int i = 0; i < V; i++) {
      if (state[i] != 2)
        state[i] = 1;
    }
    for (auto it = edge.begin(); it != edge.end(); it++) {
      state[it->second] = 0;
    }
    for (int i = 0; i < V; i++) {
      if (state[i] == 1) {
        ans.push_back(i);
        state[i] = 2;
        for (auto it = edge.begin(); it != edge.end();) {
          if (it->first == i) {
            it = edge.erase(it);
            continue;
          }
          it++;
        }
        break;
      }
    }
  }
  for (int i = 0; i < ans.size(); i++) {
    cout << ans[i] << endl;
  }

  return 0;
}
Problem p02370 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

vector<int> E[10000];
int in[10000];
bool used[10000];
int main() {
  int v, e;
  scanf("%d%d", &v, &e);
  rep(i, e) {
    int s, t;
    scanf("%d%d", &s, &t);
    E[s].push_back(t);
    in[t]++;
  }
  list<int> ls;
  queue<int> que;
  rep(i, v) {
    if (!used[i] && !in[i])
      que.push(i);
    while (!que.empty()) {
      int p = que.front();
      que.pop();
      used[p] = true;
      ls.push_back(p);
      for (int u : E[p]) {
        in[u]--;
        if (!used[u] && !in[u])
          que.push(u);
      }
    }
  }
  for (int i : ls) {
    printf("%d\\n", i);
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

vector<int> E[100];
int in[100];
bool used[100];
int main() {
  int v, e;
  scanf("%d%d", &v, &e);
  rep(i, e) {
    int s, t;
    scanf("%d%d", &s, &t);
    E[s].push_back(t);
    in[t]++;
  }
  list<int> ls;
  queue<int> que;
  rep(i, v) {
    if (!used[i] && !in[i])
      que.push(i);
    while (!que.empty()) {
      int p = que.front();
      que.pop();
      used[p] = true;
      ls.push_back(p);
      for (int u : E[p]) {
        in[u]--;
        if (!used[u] && !in[u])
          que.push(u);
      }
    }
  }
  for (int i : ls) {
    printf("%d\\n", i);
  }
}
Problem p02370 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>
#include <queue>
#include <vector>
using namespace std;

int indeg[10000], s, t, V, E;
vector<int> A[10000];
bool flag[10000] = {false};
vector<int> ans;

template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}

void bfs(int a) {
  queue<int> Q;
  Q.push(a);
  flag[a] = true;

  while (!Q.empty()) {
    int u = Q.front();
    Q.pop();
    ans.push_back(u);

    for (int i = 0; i < A[u].size(); ++i) {
      --indeg[A[u][i]];
      if (indeg[A[u][i]] == 0 && flag[A[u][i]] == false) {
        flag[A[u][i]] = true;
        Q.push(A[u][i]);
      }
    }
  }
}

void tp_srt() {

  for (int i = 0; i < V; ++i) {
    if (indeg[i] == 0 && flag[i] == false) {
      bfs(i);
    }
  }
}

int main() {

  cin >> V >> E;
  Fill(flag, false);

  for (int i = 0; i < E; ++i) {
    cin >> s >> t;
    A[s].push_back(t);
  }

  for (int i = 0; i < V; ++i) {
    for (int j = 0; j < A[i].size(); ++j) {
      ++indeg[A[i][j]];
    }
  }

  tp_srt();

  for (int i = 0; i < ans.size(); ++i) {
    cout << ans[i] << "\\n";
  }
}


Error: MLE

Fixed submission:

#include <iostream>
#include <queue>
#include <vector>
using namespace std;

int A[10000][10000], indeg[10000];
int s, t, V, E;
bool flag[10000] = {false};
queue<int> Q;
vector<int> ans;

template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}

void bfs(int a) {
  Q.push(a);
  flag[a] = true;

  while (!Q.empty()) {
    int u = Q.front();
    Q.pop();
    ans.push_back(u);

    for (int i = 0; i < V; ++i) {
      if (A[u][i] != 0) {
        --indeg[i];
        if (indeg[i] == 0 && flag[i] == false) {
          flag[i] = true;
          Q.push(i);
        }
      }
    }
  }
}

void tp_srt() {
  cin >> V >> E;

  Fill(A, 0);
  Fill(flag, false);

  for (int i = 0; i < E; ++i) {
    cin >> s >> t;
    A[s][t] = 1;
  }

  for (int i = 0; i < V; ++i) {
    indeg[i] = 0;
    for (int j = 0; j < V; ++j) {
      indeg[i] += A[j][i];
    }
  }

  for (int i = 0; i < V; ++i) {
    if (indeg[i] == 0 && flag[i] == false) {
      bfs(i);
    }
  }
}

int main() {

  tp_srt();

  for (int i = 0; i < ans.size(); ++i) {
    cout << ans[i] << "\\n";
  }
}