alexjercan/bugnet huggingface dataset:
Problem p02370 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <list>
#include <queue>
#include <vector>
using namespace std;
static const int MAX = 100000;
static const int INFTY = (1 << 29);

vector<int> G[MAX];
list<int> out;
bool V[MAX];
int N;
int indeg[MAX];

void bfs(int s) {
  queue<int> q;
  q.push(s);
  V[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 && !V[v]) {
        V[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 i = 0; i < G[u].size(); i++) {
      int v = G[u][i];
      indeg[v]++;
    }

  for (int u = 0; u < N; u++)
    if (indeg[u] == 0 && !V[u])
      bfs(u);

  for (list<int>::iterator it = out.begin(); it != out.end(); it++)
    cout << *it << endl;
}

int main() {
  int s, t, M;

  cin >> N >> M;

  for (int i = 0; i < N; i++)
    V[i] = false;

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

  tsort();

  return 0;
}


Error: -11

Fixed submission:

#include <algorithm>
#include <iostream>
#include <list>
#include <queue>
#include <vector>
using namespace std;
static const int MAX = 100000;
static const int INFTY = (1 << 29);

vector<int> G[MAX];
list<int> out;
bool V[MAX];
int N;
int indeg[MAX];

void bfs(int s) {
  queue<int> q;
  q.push(s);
  V[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 && !V[v]) {
        V[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 i = 0; i < N; i++) {
      int v = G[u][i];
      indeg[v]++;
    }

  for (int u = 0; u < N; u++)
    if (indeg[u] == 0 && !V[u])
      bfs(u);

  for (list<int>::iterator it = out.begin(); it != out.end(); it++)
    cout << *it << endl;
}

int main() {
  int s, t, M;

  cin >> N >> M;

  for (int i = 0; i < N; i++)
    V[i] = false;

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

  tsort();

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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <list>
#include <queue>
#include <string>
#include <vector>
using namespace std;
const int INF = 1 << 30;
const int NIL = -1;
const int MAXV = 10005;
const int MAXE = 100005;

struct Edge {
  int to, next;
  Edge() { next = -1; }
} edge[MAXE];

list<int> ans;
int head[MAXE];
int indegree[MAXV];
bool vis[MAXV];
int cnt;

void bfs(int u) {
  queue<int> q;
  vis[u] = true;
  q.push(u);
  while (!q.empty()) {
    u = q.front();
    q.pop();
    ans.push_back(u);
    for (int i = head[u]; i != NIL; i = edge[i].next) {
      u = edge[i].to;
      if (!vis[u] && --indegree[u] == 0) {
        vis[u] = true;
        q.push(u);
      }
    }
  }
}

void topologicalSort(int n) {
  for (int i = 0; i < n; ++i)
    vis[i] = false;

  for (int i = 0; i < n; ++i) {
    if (indegree[i] == 0 && !vis[i])
      bfs(i);
  }
}

void add(int u, int v) {
  edge[cnt].to = v;
  edge[cnt].next = head[u];
  head[u] = cnt++;
}

int main(void) {

  int V, E, s, t;
  scanf("%d%d", &V, &E);
  cnt = 0;
  memset(head, NIL, sizeof(int) * (E + 1));
  for (int i = 0; i < E; ++i) {
    scanf("%d%d", &s, &t);
    ++indegree[t];
    add(s, t);
  }
  topologicalSort(V);
  for (list<int>::iterator it = ans.begin(); it != ans.end(); ++it)
    printf("%d\\n", *it);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <list>
#include <queue>
#include <string>
#include <vector>
using namespace std;
const int INF = 1 << 30;
const int NIL = -1;
const int MAXV = 10005;
const int MAXE = 100005;

struct Edge {
  int to, next;
} edge[MAXE];

list<int> ans;
int head[MAXE];
int indegree[MAXV];
bool vis[MAXV];
int cnt;

void bfs(int u) {
  queue<int> q;
  vis[u] = true;
  q.push(u);
  while (!q.empty()) {
    u = q.front();
    q.pop();
    ans.push_back(u);
    for (int i = head[u]; i != NIL; i = edge[i].next) {
      u = edge[i].to;
      if (!vis[u] && --indegree[u] == 0) {
        vis[u] = true;
        q.push(u);
      }
    }
  }
}

void topologicalSort(int n) {
  for (int i = 0; i < n; ++i)
    vis[i] = false;

  for (int i = 0; i < n; ++i) {
    if (indegree[i] == 0 && !vis[i])
      bfs(i);
  }
}

void add(int u, int v) {
  edge[cnt].to = v;
  edge[cnt].next = head[u];
  head[u] = cnt++;
}

int main(void) {

  int V, E, s, t;
  scanf("%d%d", &V, &E);
  cnt = 0;
  memset(head, NIL, sizeof(int) * (E + 1));
  for (int i = 0; i < E; ++i) {
    scanf("%d%d", &s, &t);
    ++indegree[t];
    add(s, t);
  }
  topologicalSort(V);
  for (list<int>::iterator it = ans.begin(); it != ans.end(); ++it)
    printf("%d\\n", *it);
  return 0;
}
Problem p02370 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
#define N 10005
using namespace std;

queue<int> S;       // ??\\???????????????????????¨???????????????
vector<int> in[N];  // ??\\??????
vector<int> out[N]; // ?????????
vector<int> G[N];   // ??\\?????????????????????????????\\???

void init(int V) { // ?????°??????????????°
  for (int i = 0; i < V; i++)
    in[i].clear();
  for (int i = 0; i < V; i++) {
    out[i] = G[i];
    for (int j = 0; j < G[i].size(); j++)
      in[G[i][j]].push_back(i);
  }
  for (int i = 0; i < V; i++)
    if (!in[i].size())
      S.push(i);
}

vector<int> Topological_Sort(int V) {
  vector<int> res;
  while (!S.empty()) {
    int pos = S.front();
    S.pop();
    res.push_back(pos);
    for (int i = 0; i < out[pos].size(); i++) {
      int nx = out[pos][i];
      if (in[nx].size() == 1)
        S.push(nx);
      for (int j = 0; j < in[nx].size(); j++)
        if (pos == in[nx][j]) {
          in[nx].erase(in[nx].begin() + j);
          break;
        }
      out[pos].erase(out[pos].begin() + i);
      i--;
    }
  }
  for (int i = 0; i < V; i++)
    if (in[i].size() || out[i].size())
      res.clear();
  return res;
}

int main() {
  int v, e, s, t;
  cin >> v >> e;
  for (int i = 0; i < e; i++) {
    cin >> s >> t;
    G[s].push_back(t);
  }
  init(v);
  vector<int> ans = Topological_Sort(v);
  for (int i = 0; i < ans.size(); i++)
    cout << ans[i] << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define N 1000000
using namespace std;

queue<int> S;       // ??\\???????????????????????¨???????????????
vector<int> in[N];  // ??\\??????
vector<int> out[N]; // ?????????
vector<int> G[N];   // ??\\?????????????????????????????\\???

void init(int V) { // ?????°??????????????°
  for (int i = 0; i < V; i++)
    in[i].clear();
  for (int i = 0; i < V; i++) {
    out[i] = G[i];
    for (int j = 0; j < G[i].size(); j++)
      in[G[i][j]].push_back(i);
  }
  for (int i = 0; i < V; i++)
    if (!in[i].size())
      S.push(i);
}

vector<int> Topological_Sort(int V) {
  vector<int> res;
  while (!S.empty()) {
    int pos = S.front();
    S.pop();
    res.push_back(pos);
    for (int i = 0; i < out[pos].size(); i++) {
      int nx = out[pos][i];
      if (in[nx].size() == 1)
        S.push(nx);
      for (int j = 0; j < in[nx].size(); j++)
        if (pos == in[nx][j]) {
          in[nx].erase(in[nx].begin() + j);
          break;
        }
      out[pos].erase(out[pos].begin() + i);
      i--;
    }
  }
  for (int i = 0; i < V; i++)
    if (in[i].size() || out[i].size())
      res.clear();
  return res;
}

int main() {
  int v, e, s, t;
  cin >> v >> e;
  for (int i = 0; i < e; i++) {
    cin >> s >> t;
    G[s].push_back(t);
  }
  init(v);
  vector<int> ans = Topological_Sort(v);
  for (int i = 0; i < ans.size(); i++)
    cout << ans[i] << endl;
  return 0;
}
Problem p02370 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

class TSort {
public:
  int solve();
  void dfs(vector<vector<int>> &adj, vector<bool> &vd, vector<int> &rt, int u);
};

int TSort::solve() {
  int nv, ne;
  cin >> nv >> ne;

  vector<vector<int>> adj(nv);
  vector<bool> visited(nv, false);
  vector<int> ret;

  for (auto i = 0; i < ne; ++i) {
    int s, t;
    cin >> s >> t;
    adj[s].push_back(t);
  }

  for (auto i = 0; i < nv; ++i) {
    if (!visited[i])
      dfs(adj, visited, ret, i);
  }

  for (auto i = 0; i < nv; ++i)
    cout << ret[nv - 1 - i] << endl;

  return 0;
}

void TSort::dfs(vector<vector<int>> &adj, vector<bool> &vd, vector<int> &rt,
                int u) {
  vd[u] = true;

  int n = adj[u].size();

  for (auto i = 0; i < n; ++i) {
    int v = adj[u][i];

    if (!vd[v]) {
      dfs(adj, vd, rt, v);
    }
  }

  rt.push_back(u);
}

int main() {
  TSort ts;

  ts.solve();

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

class TSort {
public:
  int solve();
  void dfs(vector<vector<int>> &adj, vector<bool> &vd, vector<int> &rt, int u);
};

int TSort::solve() {
  int nv, ne;
  cin >> nv >> ne;

  vector<vector<int>> adj(nv, vector<int>(nv, 0));
  vector<bool> visited(nv, false);
  vector<int> ret;

  for (auto i = 0; i < ne; ++i) {
    int s, t;
    cin >> s >> t;
    adj[s][t] = 1;
  }

  for (auto i = 0; i < nv; ++i) {
    if (!visited[i])
      dfs(adj, visited, ret, i);
  }

  for (auto i = 0; i < nv; ++i)
    cout << ret[nv - 1 - i] << endl;

  return 0;
}

void TSort::dfs(vector<vector<int>> &adj, vector<bool> &vd, vector<int> &rt,
                int u) {
  vd[u] = true;

  int nv = adj.size();

  for (auto v = 0; v < nv; ++v) {
    if (vd[v] == false && adj[u][v] == 1) {
      dfs(adj, vd, rt, v);
    }
  }

  rt.push_back(u);
}

int main() {
  TSort ts;

  ts.solve();

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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <list>
#include <queue>
#include <vector>
using namespace std;

const int maxn = 100000;
const int infly = (1 << 29);

vector<int> G[maxn + 5];
list<int> out;
bool c[maxn];
int N;
int indge[maxn];

void bfs(int s) {
  queue<int> q;
  q.push(s);
  c[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];
      indge[v]--;
      if (indge[v] == 0 && !c[v]) {
        c[v] = true;
        q.push(v);
      }
    }
  }
}

void tsort() {
  for (int i = 0; i < N; i++) {
    indge[i] = 0;
  }

  for (int u = 0; u < N; u++) {
    for (int i = 0; i < G[u].size(); i++) {
      int v = G[u][i];
      indge[v]++;
    }
  }

  for (int u = 0; u < N; u++) {
    if (indge[u] == 0 && !c[u]) {
      bfs(u);
    }
  }

  for (list<int>::iterator it = out.begin(); it != out.end(); it++) {
    cout << *it << endl;
  }
}

int main() {
  int s, t, M;

  scanf("%d%d", &N, &M);

  for (int i = 0; i < N; i++)
    c[i] = false;

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

  tsort();

  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <memory>
#include <queue>
#include <set>
#include <stack>
#include <string>
#define mk(a, b) make_pair(a, b)
#define pii pair<int, int>
using namespace std;
#define MAX 1005

struct Node {
  bool vis;
  set<int> e;
  Node() { vis = false; }
};
Node V[MAX];
int n;
stack<int> ans;

void dfs(int v) {
  if (V[v].vis)
    return;
  V[v].vis = true;
  set<int>::iterator it;
  set<int> &e = V[v].e;

  for (it = e.begin(); it != e.end(); it++) {
    dfs(*it);
  }
  ans.push(v);
}

void solve() {
  for (int i = 0; i < n; i++) {
    dfs(i);
  }
  while (!ans.empty()) {
    printf("%d\\n", ans.top());
    ans.pop();
  }
}

void get_in() {
  int ne;
  scanf("%d%d", &n, &ne);
  int a, b;
  while (ne--) {
    scanf("%d%d", &a, &b);
    V[a].e.insert(b);
  }
}
int main() {
  //  freopen("in","r",stdin);
  get_in();
  solve();
  return 0;
}
Problem p02370 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

using namespace std;

int N, E;
vector<int> g[10000];
vector<int> out;
char col[10000];

void toposort(int s) {
  stack<int> chain;
  col[s] = 'g';
  chain.push(s);
  while (!chain.empty()) {
    int now = chain.top();
    int hasnext = false;
    for (int i = 0; i < g[now].size(); ++i) {
      if (col[g[now].at(i)] == 'w') {
        chain.push(g[now].at(i));
        col[g[now].at(i)] = 'g';
        hasnext = true;
        break;
      }
    }
    if (!hasnext) {
      col[now] = 'b';
      chain.pop();
      out.push_back(now);
    }
  }
}
int main() {
  cin >> N >> E;
  for (int i = 0; i < E; ++i) {
    int s, t;
    cin >> s >> t;
    g[s].push_back(t);
    //        g[s][t] = 1;
  }

  for (int j = 0; j < N; ++j) {
    col[j] = 'w';
  }
  for (int k = 0; k < N; ++k) {
    int sum = 0;
    sum = g[k].size();
    if (col[k] == 'w') {
      toposort(k);
    }
  }

  for (int l = N - 1; l >= 0; --l) {
    if (l != N - 1)
      cout << " ";
    cout << out.at(l);
  }
  cout << endl;

  return 0;
}


Error: MLE

Fixed submission:

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

using namespace std;

int N, E;
int g[10000][10000];
vector<int> out;
char col[10000];

void toposort(int s) {
  stack<int> chain;
  col[s] = 'g';
  chain.push(s);
  while (!chain.empty()) {
    int now = chain.top();
    int hasnext = false;
    for (int i = 0; i < N; ++i) {
      if (g[now][i] && col[i] == 'w') {
        chain.push(i);
        col[i] = 'g';
        hasnext = true;
        break;
      }
    }
    if (!hasnext) {
      col[now] = 'b';
      chain.pop();
      out.push_back(now);
    }
  }
}
int main() {
  cin >> N >> E;
  for (int i = 0; i < E; ++i) {
    int s, t;
    cin >> s >> t;
    g[s][t] = 1;
  }

  for (int j = 0; j < N; ++j) {
    col[j] = 'w';
  }
  for (int k = 0; k < N; ++k) {
    int sum = 0;
    for (int i = 0; i < N; ++i) {
      sum += g[i][k];
    }
    if (!sum && col[k] == 'w') {

      toposort(k);
    }
  }

  for (int l = N - 1; l >= 0; --l) {
    if (l != N - 1)
      cout << " ";
    cout << out.at(l);
  }
  cout << endl;

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

#include <bits/stdc++.h>

using namespace std;

const int MAXN = 10010;

typedef vector<int> vec;
typedef vector<vec> Graph;

Graph G(MAXN);
vec d(MAXN);

int main() {
  int n, e;
  scanf("%d%d", &n, &e);
  for (int i = 0; i < e; i++) {
    int s, t;
    scanf("%d%d", &s, &t);
    G[s].push_back(t);
    d[t]++;
  }
  queue<int> que;
  for (int i = 0; i < n; i++)
    if (d[i] == 0)
      que.push(i);
  while (!que.empty()) {
    int t = que.front();
    que.pop();
    printf("%d\\n", t);
    d[t] = INT_MAX;
    for (int i = 0; i < G[t].size(); i++) {
      d[G[t][i]]--;
      if (d[G[t][i]] == 0)
        que.push(G[t][i]);
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const int MAXN = 1010;

typedef vector<int> vec;
typedef vector<vec> Graph;

Graph G(MAXN);
vec d(MAXN);

int main() {
  int n, e;
  scanf("%d%d", &n, &e);
  for (int i = 0; i < e; i++) {
    int s, t;
    scanf("%d%d", &s, &t);
    G[s].push_back(t);
    d[t]++;
  }
  queue<int> que;
  for (int i = 0; i < n; i++)
    if (d[i] == 0)
      que.push(i);
  while (!que.empty()) {
    int t = que.front();
    que.pop();
    printf("%d\\n", t);
    d[t] = INT_MAX;
    for (int i = 0; i < G[t].size(); i++) {
      d[G[t][i]]--;
      if (d[G[t][i]] == 0)
        que.push(G[t][i]);
    }
  }
  return 0;
}
Problem p02370 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define r(i, n) for (int i = 0; i < n; i++)
using namespace std;
const int MAX = 100000;
const int INF = 1 << 29;
vector<int> G[MAX];
list<int> out;
int n;
bool V[MAX];
int indeg[MAX];
void bfs(int s) {
  queue<int> q;
  q.push(s);
  V[s] = true;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    out.push_back(u);
    r(i, G[u].size()) {
      int v = G[u][i];
      indeg[v]--;
      if (!indeg[v] && !V[v]) {
        V[v] = true;
        q.push(v);
      }
    }
  }
}
void Tsort() {
  r(u, n) r(i, G[u].size()) indeg[G[u][i]]++;
  r(u, n) if (!indeg[u] && !V[u]) bfs(u);
  for (list<int>::iterator it = out.begin(); it != out.end(); it++)
    cout << *it << endl;
}
int main() {
  int s, t, m;
  cin >> n >> m;
  r(i, m) {
    cin >> s >> t;
    G[s].push_back(t);
  }
  Tsort();
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define r(i, n) for (int i = 0; i < n; i++)
using namespace std;
const int MAX = 100000;
const int INF = 1 << 29;
vector<int> G[MAX];
list<int> out;
int n;
bool V[MAX];
int indeg[MAX];
void bfs(int s) {
  queue<int> q;
  q.push(s);
  V[s] = true;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    out.push_back(u);
    r(i, G[u].size()) {
      int v = G[u][i];
      indeg[v]--;
      if (!indeg[v] && !V[v]) {
        V[v] = true;
        q.push(v);
      }
    }
  }
}
void Tsort() {
  r(i, n) indeg[i] = 0;
  r(u, n) r(i, G[u].size()) indeg[G[u][i]]++;
  r(u, n) if (!indeg[u] && !V[u]) bfs(u);
  for (list<int>::iterator it = out.begin(); it != out.end(); it++)
    cout << *it << endl;
}
int main() {
  int s, t, m;
  cin >> n >> m;
  r(i, n) {
    V[i] = false;
    cin >> s >> t;
    G[s].push_back(t);
  }
  Tsort();
}
Problem p02370 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <queue>
#include <unordered_map>
#include <unordered_set>

using namespace std;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  int n, m;
  cin >> n >> m;

  queue<int> q;
  unordered_map<int, int> mm;
  for (int i = 0; i < n; i++)
    mm[i] = 0;

  int f, t;
  unordered_set<int> g[n];
  for (int i = 0; i < m; i++) {
    cin >> f >> t;
    g[f].insert(t);
    mm[t]++;
  }

  for (const auto &p : mm) {
    if (!p.second)
      q.push(p.first);
  }

  while (!q.empty()) {
    int v = q.front();
    q.pop();
    cout << v << "\\n";

    for (int vv : g[v]) {
      int d = mm[vv] - 1;
      if (!d)
        q.push(vv);
      mm[vv] = d;
    }
  }

  cout << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <queue>
#include <unordered_map>
#include <unordered_set>

using namespace std;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  int n, m;
  cin >> n >> m;

  unordered_set<int> g[n];
  int f, t;

  for (int i = 0; i < m; i++) {
    cin >> f >> t;
    g[f].insert(t);
  }

  queue<int> q;
  unordered_map<int, int> mm;

  for (int i = 0; i < n; i++) {
    int d = 0;
    for (int j = 0; j < n; j++) {
      if (g[j].find(i) != g[j].end())
        d++;
    }

    if (!d)
      q.push(i);

    mm[i] = d;
  }

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

    cout << v << "\\n";
    for (int i = 0; i < n; i++) {
      if (g[v].find(i) != g[v].end()) {
        int e = mm[i] - 1;
        if (!e)
          q.push(i);

        mm[i] = e;
      }
    }
  }

  cout << endl;

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

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

using namespace std;

static const long long FAR = (1LL << 29);
static const int N = 100000;

int n, m;
bool visited[N];
int p[N];

vector<int> G[N];
vector<int> sorted;

void bs(int i) {
  queue<int> q;
  q.push(i);
  visited[i] = true;

  while (!q.empty()) {
    int u = q.front();
    q.pop();
    visited[u] = true;
    sorted.push_back(u);

    for (int i = 0; i < G[u].size(); ++i) {
      int v = G[u][i];
      p[v]--;
      if (p[v] == 0 && !visited[v]) {
        q.push(v);
        visited[v] = true;
      }
    }
  }
}

void ts() {
  for (int i = 0; i < n; ++i) {
    visited[i] = false;
    p[i] = 0;
  }

  for (int u = 0; u < n; ++u) {
    for (int i = 0; i < G[u].size(); ++i) {
      int v = G[u][i];
      p[v]++;
    }
  }
  for (int i = 0; i < n; ++i) {
    if (p[i] == 0 && !visited[i])
      bs(i);
  }

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

int main() {
  cin >> n >> m;

  for (int i = 0; i < m; ++i) {
    int u, v;
    cin >> u >> v;
    G[u].push_back(v);
  }

  ts();

  return 0;
}

Error: 0

Fixed submission:

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

using namespace std;

static const long long FAR = (1LL << 29);
static const int N = 100;

int n, m;
bool visited[N];
int p[N];

vector<int> G[N];
vector<int> sorted;

void bs(int i) {
  queue<int> q;
  q.push(i);
  visited[i] = true;

  while (!q.empty()) {
    int u = q.front();
    q.pop();
    visited[u] = true;
    sorted.push_back(u);

    for (int i = 0; i < G[u].size(); ++i) {
      int v = G[u][i];
      p[v]--;
      if (p[v] == 0 && !visited[v]) {
        q.push(v);
        visited[v] = true;
      }
    }
  }
}

void ts() {
  for (int i = 0; i < n; ++i) {
    visited[i] = false;
    p[i] = 0;
  }

  for (int u = 0; u < n; ++u) {
    for (int i = 0; i < G[u].size(); ++i) {
      int v = G[u][i];
      p[v]++;
    }
  }
  for (int i = 0; i < n; ++i) {
    if (p[i] == 0 && !visited[i])
      bs(i);
  }

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

int main() {
  cin >> n >> m;

  for (int i = 0; i < m; ++i) {
    int u, v;
    cin >> u >> v;
    G[u].push_back(v);
  }

  ts();

  return 0;
}