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

#include <algorithm>
#include <iostream>

using namespace std;

const int INF = 10000;

int main() {
  vector<vector<int>> g;

  int n, e;
  cin >> n >> e;

  g.resize(n);

  for (int i = 0; i < n; ++i) {
    g[i].resize(n);
    for (int j = 0; j < n; ++j) {
      g[i][j] = (i == j ? 0 : INF);
    }
  }

  int s, t;
  for (int i = 0; i < e; ++i) {
    cin >> s >> t;
    g[s][t] = -1;
  }

  for (int k = 0; k < n; ++k)
    for (int i = 0; i < n; ++i)
      for (int j = 0; j < n; ++j)
        g[i][j] = min(g[i][j], g[i][k] + g[k][j]);

  for (int i = 0; i < n; ++i) {
    if (g[i][i] < 0) {
      cout << 1 << endl;
      return 0;
    }
  }

  cout << 0 << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <cstdio>
#include <cstring>
#include <vector>

using namespace std;

int *visit;
int v, e;
vector<vector<int>> g;

int Search(int curr) {

  if (visit[curr])
    return 1;

  visit[curr] = 1;

  for (int j : g[curr]) {

    if (Search(j))
      return 1;

    visit[j] = 0;
  }

  return 0;
}

void solve() {

  scanf("%d %d", &v, &e);

  g.resize(v);
  visit = new int[v];

  int s, t;
  for (int i = 0; i < e; ++i) {
    scanf("%d %d", &s, &t);
    g[s].push_back(t);
  }

  for (int i = 0; i < v; ++i) {

    memset(visit, 0, sizeof(int) * v);

    visit[i] = 1;

    for (int j : g[i]) {

      if (Search(j)) {
        printf("1\\n");
        return;
      }

      visit[j] = 0;
    }
  }

  printf("0\\n");
}

int main() {
  solve();
  return 0;
}
Problem p02369 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>

using namespace std;
short int result = 0;
short int *color; /*
    0 - black
    1 - grey
    2 - white
*/

vector<short int> *g; // Graph

void DFS(short int v) {
  int node;
  color[v] = 1;

  for (short int i = 0; i < g[v].size(); i++) {
    node = g[v].at(i);

    if (color[node] == 1) {
      result = 1;
    }
    if (color[node] == 2) {
      DFS(node);
    }
  }
  color[v] = 0;
};

int main() {

  short int E;    // number of transitions
  short int s, t; // transitions
  short int V;    // number of nodes

  // get properties of Graph
  cin >> V >> E;

  color = new short int[V];
  g = new vector<short int>[V];

  // fill Graph
  for (short int i = 0; i < E; i++) {
    cin >> s >> t;
    g[s].push_back(t);
  }
  for (short int i = 0; i < V; i++) {
    color[i] = 2;
  }
  for (short int i = 0; i < V; i++) {
    if (color[i] == 2) {
      DFS(i);
    }
  }

  cout << result << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
#include <list>
#include <vector>

using namespace std;

class Graph {
  int V;
  list<int> *adj;
  bool isCyclicUtil(int v, bool visited[], bool *rs);

public:
  Graph(int V, int E);
  void addEdge(int v, int w);
  bool isCyclic();
};

Graph::Graph(int V, int E) {
  this->V = V;
  adj = new list<int>[E];
}

void Graph::addEdge(int v, int w) {
  adj[v].push_back(w); // Add w to v’s list.
}

bool Graph::isCyclicUtil(int v, bool visited[], bool *recStack) {
  if (visited[v] == false) {

    visited[v] = true;
    recStack[v] = true;

    list<int>::iterator i;
    for (i = adj[v].begin(); i != adj[v].end(); ++i) {
      if (!visited[*i] && isCyclicUtil(*i, visited, recStack))
        return true;
      else if (recStack[*i])
        return true;
    }
  }
  recStack[v] = false;
  return false;
}

bool Graph::isCyclic() {

  bool *visited = new bool[V];
  bool *recStack = new bool[V];
  for (int i = 0; i < V; i++) {
    visited[i] = false;
    recStack[i] = false;
  }

  for (int i = 0; i < V; i++)
    if (isCyclicUtil(i, visited, recStack))
      return true;

  return false;
}

int main() {
  // vector<int> s;
  // vector<int> t;

  int vSize;
  int eSize;

  int s, t;

  cin >> vSize >> eSize;
  Graph g(vSize, eSize);
  /* Not bad multyline input system but it does not fit this case
  while (!cin.eof())
  {
      string line;
      int num1;
      int num2;

      getline(cin, line);

      if(line.empty()){
          break;
      }

      if (cin.fail())
      {
          cout << "error" << endl;
          break;
      }

      for(int i = 0; i < line.length(); i++){
          if(line[i] == ' '){
              num1 = std::atoi(line.substr(0,i).c_str());
              num2 = std::atoi(line.substr(i+1,line.length()).c_str());
              s.push_back(num1);
              t.push_back(num2);
          }
      }
  }
  */
  for (int i = 0; i < eSize; i++) {
    cin >> s >> t;
    g.addEdge(s, t);
    // cout << "num1: " << s[i] << " num2: " << t[i]  << endl;
  }

  if (g.isCyclic())
    cout << 1 << endl;
  else
    cout << 0 << endl;
  return 0;
}
Problem p02369 (C++) — buggy submission, verdict: Time Limit Exceeded

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

const int vmax = 10010;
const int inf = 1 << 25;

int N, M;
int dist[vmax][vmax];

bool warshall_floyd() {
  rep(k, N) rep(i, N) rep(j, N) dist[i][j] =
      min(dist[i][j], dist[i][k] + dist[k][j]);
  rep(i, N) if (dist[i][i] < 0) return true;
  return false;
}

int main(void) {
  cin >> N >> M;
  rep(i, N) rep(j, N) dist[i][j] = (i == j) ? 0 : inf;
  rep(i, M) {
    int a, b;
    cin >> a >> b;
    dist[a][b] = -1;
  }
  cout << warshall_floyd() << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

const int vmax = 10010;

int N, M;
vector<int> graph[vmax];

bool visited[vmax];
bool used[vmax];

bool dfs(int v) {
  visited[v] = true;
  used[v] = true;
  bool res = false;
  for (auto &u : graph[v]) {
    if (used[u])
      res = true;
    else
      res |= dfs(u);
  }
  used[v] = false;
  return res;
}

int main(void) {
  cin >> N >> M;
  rep(i, M) {
    int a, b;
    cin >> a >> b;
    graph[a].push_back(b);
  }
  bool ans = false;
  rep(i, N) if (!visited[i]) ans |= dfs(i);
  cout << ans << endl;
  return 0;
}
Problem p02369 (C++) — buggy submission, verdict: Time Limit Exceeded

#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];

bool rec(int i) {
  isVisited[i] = true;
  isChecked[i] = true;
  for (auto &e : V[i].E) {
    if (isVisited[e]) {
      return true;
    } else if (!isChecked[e]) {
      if (rec(e)) {
        return true;
      }
    }
  }
  isVisited[i] = false;
  return false;
}

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(isVisited, isVisited + MAX_V, false);
  fill(isChecked, isChecked + MAX_V, false);
  for (int i = 0; i < nv; i++) {
    if (isChecked[i])
      continue;
    if (rec(i)) {
      cout << 1 << endl;
      return 0;
    }
  }
  cout << 0 << endl;
  return 0;
}

Error: TLE

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];

bool rec(int i) {
  isVisited[i] = true;
  isChecked[i] = true;
  for (auto &e : V[i].E) {
    if (isVisited[e]) {
      return true;
    } else {
      if (rec(e)) {
        return true;
      }
    }
  }
  isVisited[i] = false;
  return false;
}

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(isVisited, isVisited + MAX_V, false);
  fill(isChecked, isChecked + MAX_V, false);
  for (int i = 0; i < nv; i++) {
    if (isChecked[i])
      continue;
    if (rec(i)) {
      cout << 1 << endl;
      return 0;
    }
  }
  cout << 0 << endl;
  return 0;
}
Problem p02369 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define MAX_V 100

using namespace std;

typedef long long ll;

vector<int> G[MAX_V];

bool cycle_exists = false;
bool activated[MAX_V];
bool checked[MAX_V];

void dfs(int i) {
  if (activated[i]) {
    cycle_exists = true;
  } else {
    activated[i] = true;
    for (int j = 0; j < G[i].size(); ++j)
      if (!checked[G[i][j]])
        dfs(G[i][j]);
    activated[i] = false;
  }
  checked[i] = true;
}

int main() {
  fill(activated, activated + MAX_V, false);
  fill(checked, checked + MAX_V, false);

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

  for (int i = 0; i < V; ++i)
    if (!checked[i])
      dfs(i);

  if (cycle_exists)
    cout << 1;
  else
    cout << 0;
  cout << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define MAX_V 100

using namespace std;

typedef long long ll;

vector<int> G[MAX_V];

bool cycle_exists = false;
bool activated[MAX_V];
bool checked[MAX_V];

void dfs(int i) {
  if (activated[i]) {
    cycle_exists = true;
  } else {
    activated[i] = true;
    for (int j = 0; j < G[i].size(); ++j) {
      dfs(G[i][j]);
    }
    activated[i] = false;
  }
  checked[i] = true;
}

int main() {
  fill(activated, activated + MAX_V, false);
  fill(checked, checked + MAX_V, false);

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

  for (int i = 0; i < V; ++i)
    if (!checked[i])
      dfs(i);

  if (cycle_exists)
    cout << 1;
  else
    cout << 0;
  cout << endl;

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

#include <iostream>
#include <stack>
#include <vector>

#define FOR(i, n) for (int i = 0; i < n; i++)
#define FORI(i, z, n) for (int i = z; i < n; i++)

using namespace std;

vector<vector<int>> graph;
vector<bool> seen;
vector<bool> localSeen;

bool hasLoop(int i) {
  stack<pair<int, bool>> Q;
  Q.push(make_pair(i, true));
  while (!Q.empty()) {
    auto x = Q.top();
    Q.pop();
    int u = x.first;
    bool goingDown = x.second;

    if (goingDown) {
      if (!seen[u]) {
        Q.push(make_pair(u, false));
        localSeen[u] = true;
        for (int v : graph[u])
          if (localSeen[v])
            return true;
          else
            Q.push(make_pair(v, true));
      }
    } else {
      localSeen[u] = false;
      seen[u] = true;
    }
  }
  return false;
}

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

  graph.resize(V);
  seen.resize(V, false);
  localSeen.resize(V, false);

  FOR(i, E) {
    int f, t;
    cin >> f >> t;
    graph[f].push_back(t);
  }

  FOR(i, V)
  if (!seen[i])
    if (hasLoop(i)) {
      cout << 1 << endl;
      return 0;
    }

  cout << 0 << endl;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <stack>
#include <vector>

#define FOR(i, n) for (int i = 0; i < n; i++)
#define FORI(i, z, n) for (int i = z; i < n; i++)

using namespace std;

vector<vector<int>> graph;
vector<bool> seen;
vector<bool> localSeen;

bool hasLoop(int i) {
  stack<pair<int, bool>> Q;
  Q.push(make_pair(i, true));
  while (!Q.empty()) {
    auto x = Q.top();
    Q.pop();
    int u = x.first;
    bool goingDown = x.second;

    if (goingDown) {
      Q.push(make_pair(u, false));
      seen[u] = true;
      localSeen[u] = true;
      for (int v : graph[u])
        if (localSeen[v])
          return true;
        else
          Q.push(make_pair(v, true));
    } else {
      localSeen[u] = false;
    }
  }
  return false;
}

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

  graph.resize(V);
  seen.resize(V, false);
  localSeen.resize(V, false);

  FOR(i, E) {
    int f, t;
    cin >> f >> t;
    graph[f].push_back(t);
  }

  FOR(i, V)
  if (!seen[i])
    if (hasLoop(i)) {
      cout << 1 << endl;
      return 0;
    }

  cout << 0 << endl;
}
Problem p02369 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int V, E, s, t, f, vis[103];
vector<int> G[103];
void rec(int pos, int c, int s) {
  if (pos == s && c) {
    f |= 1;
    return;
  }
  if (c)
    vis[pos] = true;
  for (auto &e : G[pos]) {
    if (!vis[e])
      rec(e, c + 1, s);
  }
}
int main() {
  cin >> V >> E;
  for (int i = 0; i < E; i++) {
    cin >> s >> t;
    G[s].push_back(t);
  }
  for (int i = 0; i < V; i++) {
    fill(vis, vis + V, false);
    if (!vis[i])
      rec(i, 0, i);
  }
  cout << (f ? 1 : 0) << endl;
}

Error: 0

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
int V, E, s, t, f, vis[103];
vector<int> G[103];
void rec(int pos, int s) {
  if (vis[s]) {
    f |= 1;
    return;
  }
  for (auto &e : G[pos])
    rec(e, s);
}
int main() {
  cin >> V >> E;
  for (int i = 0; i < E; i++) {
    cin >> s >> t;
    G[s].push_back(t);
  }
  for (int i = 0; i < V; i++) {
    if (!vis[i])
      rec(i, i);
  }
  cout << (f ? 1 : 0) << endl;
}
Problem p02369 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <vector>

using namespace std;

const int INF = 1000;

int v, e;

// edge src and dst
vector<int> s, t;

// bellman-ford
bool check(int start) {

  vector<int> dist(v, INF);

  dist[start] = 0;

  int n = 0;
  for (; n < v; ++n) {

    bool update = false;

    for (int i = 0; i < e; ++i) {

      int from = s[i];
      int to = t[i];

      if (dist[from] != INF && dist[from] - 1 < dist[to]) {
        dist[to] = dist[from] - 1;
        update = true;
      }
    }

    if (!update)
      break;
  }

  return n == v;
}

int main() {

  scanf("%d %d", &v, &e);

  s.resize(e);
  t.resize(e);

  for (int i = 0; i < e; ++i) {
    scanf("%d %d", &s[i], &t[i]);
  }

  for (int i = 0; i < v; ++i) {
    if (check(i)) {
      printf("1\\n");
      return 0;
    }
  }

  printf("0\\n");

  return 0;
}


Error: 0

Fixed submission:

#include <cstdio>
#include <vector>

using namespace std;

const int INF = 10000;

int v, e;

// edge src and dst
vector<int> s, t;

// bellman-ford
bool check(int start) {

  vector<int> dist(v, INF);

  dist[start] = 0;

  int n = 0;
  for (; n < v; ++n) {

    bool update = false;

    for (int i = 0; i < e; ++i) {

      int from = s[i];
      int to = t[i];

      if (dist[from] != INF && dist[from] - 1 < dist[to]) {
        dist[to] = dist[from] - 1;
        update = true;
      }
    }

    if (!update)
      break;
  }

  return n == v;
}

int main() {

  scanf("%d %d", &v, &e);

  s.resize(v);
  t.resize(v);

  for (int i = 0; i < e; ++i) {
    scanf("%d %d", &s[i], &t[i]);
  }

  for (int i = 0; i < v; ++i) {
    if (check(i)) {
      printf("1\\n");
      return 0;
    }
  }

  printf("0\\n");

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

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

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, j) FOR(i, 0, j)
#define mp std::make_pair

const int INF = 1 << 24;
const int dx[8] = {0, 0, 1, -1, 1, 1, -1, -1},
          dy[8] = {1, -1, 0, 0, 1, -1, 1, -1};

typedef unsigned long long ull;
typedef std::pair<int, int> P;

const int MAX_V = 100;
int V, E;
std::vector<int> G[MAX_V];
int used[MAX_V];
int isConnected[MAX_V][MAX_V];

bool bfs(int s) {
  std::fill(used, used + V, 0);

  std::queue<int> q;
  q.push(s);
  used[s] = 1;

  while (!q.empty()) {
    int u = q.front();
    q.pop();

    REP(i, G[u].size()) {
      int v = G[u][i];
      if (!used[v]) {
        if (isConnected[v][s]) {
          return true;
        }
        q.push(v);
        used[v] = 1;
        isConnected[s][v] = 1;
      }
    }
  }

  return false;
}

int main() {
  std::cin >> V >> E;

  REP(i, E) {
    int u, v;
    std::cin >> u >> v;

    G[u].push_back(v);
  }

  bool f = false;
  REP(v, V) { f |= bfs(v); }

  if (f) {
    puts("1");
  } else {
    puts("0");
  }
}

Error: TLE

Fixed submission:

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

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, j) FOR(i, 0, j)
#define mp std::make_pair

const int INF = 1 << 24;
const int dx[8] = {0, 0, 1, -1, 1, 1, -1, -1},
          dy[8] = {1, -1, 0, 0, 1, -1, 1, -1};

typedef unsigned long long ull;
typedef std::pair<int, int> P;

const int MAX_V = 100;
int V, E;
std::vector<int> G[MAX_V];
int used[MAX_V];
int isConnected[MAX_V][MAX_V];

bool bfs(int s) {
  std::fill(used, used + V, 0);

  std::queue<int> q;
  q.push(s);
  used[s] = 1;

  while (!q.empty()) {
    int u = q.front();
    q.pop();

    REP(i, G[u].size()) {
      int v = G[u][i];
      if (isConnected[v][s]) {
        return true;
      }
      q.push(v);
      used[v] = 1;
      isConnected[s][v] = 1;
    }
  }

  return false;
}

int main() {
  std::cin >> V >> E;

  REP(i, E) {
    int u, v;
    std::cin >> u >> v;

    G[u].push_back(v);
  }

  bool f = false;
  REP(v, V) { f |= bfs(v); }

  if (f) {
    puts("1");
  } else {
    puts("0");
  }
}
Problem p02369 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>
#define INF 2147483647
using namespace std;
struct Edge {
  int to, cost;
  Edge(int to, int cost = -1) : to(to), cost(cost) {}
};
typedef vector<vector<Edge>> P;
P graph;
vector<int> dist;

bool bellman_ford(int n, int s) {
  dist = vector<int>(n, 0);
  for (int i = 0; i < n; i++) {
    for (int v = 0; v < n; v++) {
      for (int k = 0; k < graph[v].size(); k++) {
        Edge e = graph[v][k];
        if (dist[e.to] > dist[v] + e.cost) {
          dist[e.to] = dist[v] + e.cost;
          if (i == n - 1)
            return true;
        }
      }
    }
  }
  return false;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int V, E;
  cin >> V >> E;
  graph = P(V);
  for (int i = 0; i < E; i++) {
    int from, to;
    cin >> from >> to;
    graph[from].push_back(Edge(to));
  }

  if (bellman_ford(V, 0)) {
    cout << 1 << endl;
  } else {
    cout << 0 << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <string.h>
#include <vector>
#define INF 1 << 30
using namespace std;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int V, E;
  cin >> V >> E;
  vector<vector<int>> g(V);
  vector<bool> dp(V, false);
  queue<int> q;

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

  for (int i = 0; i < V; i++) {
    q.push(i);
    dp[i] = true;
    while (!q.empty()) {
      int prev = q.front();
      q.pop();
      for (int i = 0; i < g[prev].size(); i++) {
        int next = g[prev][i];
        if (dp[next] == true) {
          cout << 1 << endl;
          return 0;
        }
        q.push(next);
      }
    }

    for (int i = 0; i < V; i++) {
      dp[i] = false;
    }
  }
  cout << 0 << endl;
}
Problem p02369 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define r(i, n) for (int i = 0; i < n; i++)
using namespace std;
int n, m, a[101][101], f, x, y;
int main() {
  cin >> n >> m;
  r(i, n) r(j, n) a[i][j] = 1e8;
  r(j, m) {
    cin >> x >> y;
    a[x][y] = -1;
  }
  r(k, n) r(i, n) r(j, n) a[i][j] = min(a[i][j], a[i][k] + a[k][j]);
  r(i, n) if (a[i][i] < 0) f++;
  cout << (f ? 1 : 0) << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define r(i, n) for (int i = 0; i < n; i++)
using namespace std;
vector<int> v[101];
int n, m, a, b, used[101];
int dfs(int x) {
  used[x] = 1;
  r(i, v[x].size()) if (dfs(v[x][i])) return 1;
  return 0;
}
int main() {
  cin >> n >> m;
  r(i, m) {
    cin >> a >> b;
    v[a].push_back(b);
  }
  r(i, n) {
    memset(used, 0, sizeof(used));
    if (dfs(i)) {
      cout << 1 << endl;
      return 0;
    }
  }
  cout << 0 << endl;
}
Problem p02370 (C++) — buggy submission, verdict: Memory Limit Exceeded

#define _CRT_SECURE_NO_WARNINGS
#include <climits>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

static const int MaxV = 10000;

vector<int> AdjL[MaxV];
int InDegree[MaxV];
queue<int> Q;

int main() {
  int V, E;
  int s, t;

  cin >> V >> E;
  for (int i = 0; i < V; i++) {
    InDegree[i] = 0;
  }
  for (int i = 0; i < E; i++) {
    cin >> s >> t;
    AdjL[s].push_back(t);
    InDegree[t]++;
  }
  for (int i = 0; i < V; i++) {
    if (InDegree[i] == 0) {
      Q.push(i);
    }
  }
  while (Q.empty() == false) {
    s = Q.front();
    Q.pop();
    cout << s << endl;
    for (unsigned int i = 0; i < AdjL[s].size(); i++) {
      t = AdjL[s][i];
      if (--InDegree[t] == 0) {
        Q.push(t);
      }
    }
  }

  return 0;
}

Error: MLE

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
#include <climits>
#include <iostream>
#include <queue>

using namespace std;

static const int MaxV = 10000;

int AdjL[MaxV][MaxV];
int OutDegree[MaxV];
int InDegree[MaxV];
queue<int> Q;

int main() {
  int V, E;
  int s, t;

  cin >> V >> E;
  for (int i = 0; i < V; i++) {
    OutDegree[i] = InDegree[i] = 0;
  }
  for (int i = 0; i < E; i++) {
    cin >> s >> t;
    AdjL[s][OutDegree[s]++] = t;
    InDegree[t]++;
  }
  for (int i = 0; i < V; i++) {
    if (InDegree[i] == 0) {
      Q.push(i);
    }
  }
  while (Q.empty() == false) {
    s = Q.front();
    Q.pop();
    cout << s << endl;
    for (int i = 0; i < OutDegree[s]; i++) {
      t = AdjL[s][i];
      if (--InDegree[t] == 0) {
        Q.push(t);
      }
    }
  }

  return 0;
}