prithivMLmods/Coder-Stat huggingface dataset:
Problem p02366 (C++)

Buggy submission (status: Runtime Error):
/*
??¢??????
lowlink
*/
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define all(c) c.begin(), c.end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
using namespace std;
const int MAX_V = 100;
vector<int> G[MAX_V];
int ord[MAX_V], low[MAX_V];
int I;
int sum[MAX_V];
vector<int> arts;
void dfs(int v, int p) {
  ord[v] = ++I;
  low[v] = ord[v];
  sum[v] = 1;
  int mx = 0, c = 0;
  for (int u : G[v])
    if (u != p) {
      if (ord[u])
        chmin(low[v], ord[u]);
      else {
        dfs(u, v);
        c++;
        sum[v] += sum[u];
        chmin(low[v], low[u]);
        chmax(mx, low[u]);
      }
    }
  if (p < 0) {
    if (c > 1)
      arts.pb(v);
  } else {
    //		cout<<"v="<<v<<"  mx="<<mx<<endl;
    if (ord[v] <= mx)
      arts.pb(v);
  }
}
void arti(int N) { rep(i, N) if (ord[i] == 0) dfs(i, -1); }
int main() {
  int N, M;
  cin >> N >> M;
  rep(i, M) {
    int a, b;
    cin >> a >> b;
    G[a].pb(b);
    G[b].pb(a);
  }
  arti(N);
  sort(all(arts));
  for (int a : arts)
    cout << a << endl;
}

Fixed submission:
/*
??¢??????
lowlink
*/
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define all(c) c.begin(), c.end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
using namespace std;
const int MAX_V = 100000;
vector<int> G[MAX_V];
int ord[MAX_V], low[MAX_V];
int I;
int sum[MAX_V];
vector<int> arts;
void dfs(int v, int p) {
  ord[v] = ++I;
  low[v] = ord[v];
  sum[v] = 1;
  int mx = 0, c = 0;
  for (int u : G[v])
    if (u != p) {
      if (ord[u])
        chmin(low[v], ord[u]);
      else {
        dfs(u, v);
        c++;
        sum[v] += sum[u];
        chmin(low[v], low[u]);
        chmax(mx, low[u]);
      }
    }
  if (p < 0) {
    if (c > 1)
      arts.pb(v);
  } else {
    //		cout<<"v="<<v<<"  mx="<<mx<<endl;
    if (ord[v] <= mx)
      arts.pb(v);
  }
}
void arti(int N) { rep(i, N) if (ord[i] == 0) dfs(i, -1); }
int main() {
  int N, M;
  cin >> N >> M;
  rep(i, M) {
    int a, b;
    cin >> a >> b;
    G[a].pb(b);
    G[b].pb(a);
  }
  arti(N);
  sort(all(arts));
  for (int a : arts)
    cout << a << endl;
}
Problem p02366 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <set>
#include <vector>
using namespace std;

const int MAX_V = 100000;

vector<int> G[MAX_V];
int vertex, edge;
bool visited[MAX_V];
int prenum[MAX_V], parent[MAX_V], lowest[MAX_V];
int timer;
set<int> ap;

void dfs(int current, int prev) {
  prenum[current] = lowest[current] = timer;
  timer++;

  visited[current] = true;

  int next;
  for (auto i = 0; i < G[current].size(); i++) {
    next = G[current][i];
    if (!visited[next]) {
      parent[next] = current;
      dfs(next, current);
      lowest[current] = min(lowest[current], lowest[next]);
    } else if (next != prev) {
      lowest[current] = min(lowest[current], prenum[next]);
    }
  }
}

void ArticulationPoints(void) {
  timer = 1;
  dfs(0, -1);

  int np = 0;
  for (int i = 1; i < vertex; i++) {
    int p = parent[i];
    if (p == 0)
      np++;
    else if (prenum[p] <= lowest[i])
      ap.insert(p);
  }
  if (np > 1)
    ap.insert(0);
}

int main(void) {
  cin >> vertex >> edge;
  for (int i = 0; i < vertex; i++) {
    visited[i] = false;
  }

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

  ArticulationPoints();

  for (auto it = ap.begin(); it != ap.end(); it++) {
    cout << *it << endl;
  }

  return 0;
}

Fixed submission:
#include <iostream>
#include <set>
#include <vector>
using namespace std;

const int MAX_V = 100000;

vector<int> G[MAX_V];
int vertex, edge;
bool visited[MAX_V];
int prenum[MAX_V], parent[MAX_V], lowest[MAX_V];
int timer;
set<int> ap;

void dfs(int current, int prev) {
  prenum[current] = lowest[current] = timer;
  timer++;

  visited[current] = true;

  int next;
  for (auto i = 0; i < G[current].size(); i++) {
    next = G[current][i];
    if (!visited[next]) {
      parent[next] = current;
      dfs(next, current);
      lowest[current] = min(lowest[current], lowest[next]);
    } else if (next != prev) {
      lowest[current] = min(lowest[current], prenum[next]);
    }
  }
}

void ArticulationPoints(void) {
  timer = 1;
  dfs(0, -1);

  int np = 0;
  for (int i = 1; i < vertex; i++) {
    int p = parent[i];
    if (p == 0)
      np++;
    else if (prenum[p] <= lowest[i])
      ap.insert(p);
  }
  if (np > 1)
    ap.insert(0);
}

int main(void) {
  cin >> vertex >> edge;
  for (int i = 0; i < vertex; i++) {
    visited[i] = false;
  }

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

  ArticulationPoints();

  for (auto it = ap.begin(); it != ap.end(); it++) {
    cout << *it << endl;
  }

  return 0;
}
Problem p02367 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
template <typename A, typename B> inline bool chmax(A &a, B b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename A, typename B> inline bool chmin(A &a, B b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const ll INF = 1ll << 29;
const ll MOD = 1000000007;
const double EPS = 1e-9;
const bool debug = 0;
//---------------------------------//

int V, E;
vector<int> g[112345];
vector<int> depth, low;
set<pii> bridge;

int dfs(int u, int p, int d) {
  depth[u] = d;
  low[u] = d;

  for (int v : g[u]) {
    if (v == p)
      continue;

    if (depth[v] == -1) {
      chmin(low[u], dfs(v, u, d + 1));
      if (depth[v] == low[v])
        bridge.insert(pii(min(u, v), max(u, v)));
    } else
      chmin(low[u], low[v]);
  }

  return low[u];
}

int main() {
  cin >> V >> E;
  REP(i, E) {
    int s, t;
    scanf("%d %d", &s, &t);
    g[s].push_back(t);
    g[t].push_back(s);
  }

  depth.resize(V, -1);
  low.resize(V, -1);

  dfs(0, -1, 0);

  for (set<pii>::iterator it = bridge.begin(); it != bridge.end(); ++it) {
    printf("%d %d\n", it->first, it->second);
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
template <typename A, typename B> inline bool chmax(A &a, B b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename A, typename B> inline bool chmin(A &a, B b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const ll INF = 1ll << 29;
const ll MOD = 1000000007;
const double EPS = 1e-9;
const bool debug = 0;
//---------------------------------//

int V, E;
vector<int> g[112345];
vector<int> depth, low;
set<pii> bridge;

int dfs(int u, int p, int d) {
  depth[u] = d;
  low[u] = d;

  REP(i, g[u].size()) {
    int v = g[u][i];
    if (v == p)
      continue;

    if (depth[v] == -1) {
      chmin(low[u], dfs(v, u, d + 1));
      if (depth[v] == low[v])
        bridge.insert(pii(min(u, v), max(u, v)));
    } else
      chmin(low[u], low[v]);
  }

  return low[u];
}

int main() {
  cin >> V >> E;
  REP(i, E) {
    int s, t;
    scanf("%d %d", &s, &t);
    g[s].push_back(t);
    g[t].push_back(s);
  }

  depth.resize(V, -1);
  low.resize(V, -1);

  dfs(0, -1, 0);

  for (set<pii>::iterator it = bridge.begin(); it != bridge.end(); ++it) {
    printf("%d %d\n", it->first, it->second);
  }
  return 0;
}
Problem p02367 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <stdio.h>
#include <vector>

using namespace std;

struct edge {
  int f, t;
};

const int V_MAX = (int)1e5 + 10;
vector<int> edges[V_MAX];
int ord[V_MAX];
int low[V_MAX];
bool used_v[V_MAX];
vector<edge> ans;

bool comp(edge e1, edge e2) {
  if (e1.f == e2.f) {
    return e1.t < e2.t;
  } else {
    return e1.f < e2.f;
  }
}

void dfs(int u, int prev, int k) {
  low[u] = ord[u] = k;
  k += 1;
  used_v[u] = true;

  for (int i = 0; i < edges[u].size(); i++) {
    int next = edges[u][i];

    if (not used_v[next]) {
      dfs(next, u, k);
      low[u] = min(low[u], low[next]);

      if (low[next] > ord[u]) {
        edge e;
        e.f = min(u, next);
        e.t = max(u, next);
        ans.push_back(e);
      }
    } else if (prev != next) {
      low[u] = min(low[u], ord[next]);
    }
  }
  return;
}

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

  int v, e;
  cin >> v >> e;

  for (int i = 0; i < e; i++) {
    int a, b;
    scanf("%d %d", &a, &b);
    edges[a].push_back(b);
    edges[b].push_back(a);
  }

  dfs(0, -1, 1);

  sort(ans.begin(), ans.end(), comp);
  for (int i = 0; i < ans.size(); i++) {
    cout << ans[i].f << " " << ans[i].t << "\n";
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <stdio.h>
#include <vector>

using namespace std;

struct edge {
  int f, t;
};

const int V_MAX = (int)1e5 + 10;
vector<int> edges[V_MAX];
int ord[V_MAX];
int low[V_MAX];
bool used_v[V_MAX];
vector<edge> ans;

bool comp(edge e1, edge e2) {
  if (e1.f == e2.f) {
    return e1.t < e2.t;
  } else {
    return e1.f < e2.f;
  }
}

void dfs(int u, int prev, int k) {
  low[u] = ord[u] = k;
  k += 1;
  used_v[u] = true;

  for (int i = 0; i < edges[u].size(); i++) {
    int next = edges[u][i];

    if (not used_v[next]) {
      dfs(next, u, k);
      low[u] = min(low[u], low[next]);

      if (low[next] > ord[u]) {
        edge e;
        e.f = min(u, next);
        e.t = max(u, next);
        ans.push_back(e);
      }
    } else if (prev != next) {
      low[u] = min(low[u], ord[next]);
    }
  }
  return;
}

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

  int v, e;
  cin >> v >> e;

  for (int i = 0; i < e; i++) {
    int a, b;
    //        scanf("%d %d", &a, &b);
    cin >> a >> b;
    edges[a].push_back(b);
    edges[b].push_back(a);
  }

  dfs(0, -1, 1);

  sort(ans.begin(), ans.end(), comp);
  for (int i = 0; i < ans.size(); i++) {
    cout << ans[i].f << " " << ans[i].t << "\n";
  }

  return 0;
}
Problem p02368 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>

using namespace std;

const int MAX_N = 10000;
const int V = 10000;

vector<int> G[MAX_N];
vector<int> rG[MAX_N];
vector<int> vs;   //??°?????????????????????
bool used[MAX_N]; //?????§????????????????????????
int cmp[MAX_N];   //?±????????????£????????????????????????????????????

void add_edge(int from, int to) {
  G[from].push_back(to);
  rG[to].push_back(from);
}

void dfs(int v) {
  used[v] = true;
  for (int i = 0; i < G[v].size(); i++) {
    if (!used[G[v][i]]) {
      dfs(G[v][i]);
    }
  }
  vs.push_back(v);
}

void rdfs(int v, int k) {
  used[v] = true;
  cmp[v] = k;
  for (int i = 0; i < rG[v].size(); i++) {
    if (!used[rG[v][i]]) {
      rdfs(rG[v][i], k);
    }
  }
}

int scc(int n) //?????£??????????????°?????????
{
  memset(used, 0, sizeof(used));
  vs.clear();
  for (int v = 0; v < n; v++) {
    if (!used[v]) {
      dfs(v);
    }
  }
  memset(used, 0, sizeof(used));
  int k = 0;
  for (int i = vs.size() - 1; i >= 0; i--) {
    if (!used[vs[i]]) {
      rdfs(vs[i], k++);
    }
  }
  return k;
}

int main() {
  int n, m, from, to, k, l, u, v;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> from >> to;
    add_edge(from, to);
  }
  k = scc(m);
  cin >> l;
  for (int i = 0; i < l; i++) {
    cin >> u >> v;
    printf("%d\n", (cmp[u] == cmp[v]));
  }
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>

using namespace std;

const int MAX_N = 10000;
const int V = 10000;

vector<int> G[MAX_N];
vector<int> rG[MAX_N];
vector<int> vs;   //??°?????????????????????
bool used[MAX_N]; //?????§????????????????????????
int cmp[MAX_N];   //?±????????????£????????????????????????????????????

void add_edge(int from, int to) {
  G[from].push_back(to);
  rG[to].push_back(from);
}

void dfs(int v) {
  used[v] = true;
  for (int i = 0; i < G[v].size(); i++) {
    if (!used[G[v][i]]) {
      dfs(G[v][i]);
    }
  }
  vs.push_back(v);
}

void rdfs(int v, int k) {
  used[v] = true;
  cmp[v] = k;
  for (int i = 0; i < rG[v].size(); i++) {
    if (!used[rG[v][i]]) {
      rdfs(rG[v][i], k);
    }
  }
}

int scc(int n) //?????£??????????????°?????????
{
  memset(used, 0, sizeof(used));
  vs.clear();
  for (int v = 0; v < n; v++) {
    if (!used[v]) {
      dfs(v);
    }
  }
  memset(used, 0, sizeof(used));
  int k = 0;
  for (int i = vs.size() - 1; i >= 0; i--) {
    if (!used[vs[i]]) {
      rdfs(vs[i], k++);
    }
  }
  return k;
}

int main() {
  int n, m, from, to, k, l, u, v;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> from >> to;
    add_edge(from, to);
  }
  k = scc(n);
  cin >> l;
  for (int i = 0; i < l; i++) {
    cin >> u >> v;
    printf("%d\n", (cmp[u] == cmp[v]));
  }
}
Problem p02369 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

int n, m;
vector<int> adj[101];
bitset<101> mark;
bool Cyc;

void dfs(int x) {
  if (mark[x])
    Cyc = 1;
  if (Cyc)
    return;
  mark[x] = 1;
  for (int i = 0; i < adj[x].size(); i++)
    dfs(adj[x][i]);
  mark[x] = 0;
}

int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    adj[x].push_back(y);
  }
  for (int i = 0; i < min(50, n); i++)
    dfs(i);
  if (Cyc)
    cout << 1 << endl;
  else
    cout << 0 << endl;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

int n, m;
vector<int> adj[101];
bitset<101> mark;
bool Cyc;

void dfs(int x) {
  if (mark[x])
    Cyc = 1;
  if (Cyc)
    return;
  mark[x] = 1;
  for (int i = 0; i < adj[x].size(); i++)
    dfs(adj[x][i]);
  mark[x] = 0;
}

int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    adj[x].push_back(y);
  }
  for (int i = 0; i < n; i += 5)
    dfs(i);
  if (Cyc)
    cout << 1 << endl;
  else
    cout << 0 << endl;
}
Problem p02369 (C++)

Buggy submission (status: 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 (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;
}

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 (!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;
}
Problem p02369 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bitset>
#include <stdio.h>
#include <vector>
using namespace std;
#define PB push_back
const int N = 1e2 + 10;
bitset<N> went, in;
vector<int> graph[N];
bool f = true;
void dfs(int now) {
  if (in[now]) {
    f = false;
    return;
  }
  in[now] = true;
  went[now] = true;
  for (int i = 0; i < graph[now].size(); i++)
    dfs(graph[now][i]);
  in[now] = false;
  return;
}
int main() {
  int n, m, l, r;
  scanf("%d%d", &n, &m);
  while (m--) {
    scanf("%d%d", &l, &r);
    graph[l].PB(r);
  }
  in.reset();
  went.reset();
  for (int i = 0; i < n; i++)
    if (!went[i])
      dfs(i);

  if (f)
    printf("0\n");
  else
    printf("1\n");
}


Fixed submission:
#include <bitset>
#include <stdio.h>
#include <vector>
using namespace std;
#define PB push_back
const int N = 1e2 + 10;
bitset<N> went, in;
vector<int> graph[N];
bool f = true;
void dfs(int now) {
  if (in[now]) {
    f = false;
    return;
  } else if (went[now])
    return;
  in[now] = true;
  went[now] = true;
  for (int i = 0; i < graph[now].size(); i++)
    dfs(graph[now][i]);
  in[now] = false;
  return;
}
int main() {
  int n, m, l, r;
  scanf("%d%d", &n, &m);
  while (m--) {
    scanf("%d%d", &l, &r);
    graph[l].PB(r);
  }
  in.reset();
  went.reset();
  for (int i = 0; i < n; i++)
    if (!went[i])
      dfs(i);

  if (f)
    printf("0\n");
  else
    printf("1\n");
}
Problem p02369 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <climits>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

vector<int> edges[1000];
bool used[100];
bool active[100];

bool dfs(int cur) {
  active[cur] = true;
  used[cur] = true;
  for (int i = 0; i < edges[cur].size(); i++) {
    if (active[edges[cur][i]]) {
      return true;
    }
    if (dfs(edges[cur][i])) {
      return true;
    }
  }
  active[cur] = false;
  return false;
}

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

  for (int i = 0; i < N; i++) {
    if (used[i]) {
      continue;
    }
    if (dfs(i)) {
      cout << 1 << endl;
      return 0;
    }
  }
  cout << 0 << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

vector<int> edges[1000];
bool used[100];
bool active[100];

bool dfs(int cur) {
  active[cur] = true;
  used[cur] = true;
  for (int i = 0; i < edges[cur].size(); i++) {
    if (active[edges[cur][i]]) {
      return true;
    }
    if (used[edges[cur][i]]) {
      continue;
    }
    if (dfs(edges[cur][i])) {
      return true;
    }
  }
  active[cur] = false;
  return false;
}

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

  for (int i = 0; i < N; i++) {
    if (used[i]) {
      continue;
    }
    if (dfs(i)) {
      cout << 1 << endl;
      return 0;
    }
  }
  cout << 0 << endl;
  return 0;
}
Problem p02369 (C++)

Buggy submission (status: 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) {
      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;
}


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)
      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;
}
Problem p02369 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
vector<int> X[10000];
int N, M, A, B;
bool ok = false;
queue<char> Q;
int main() {
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    cin >> A >> B;
    if (A > B) {
      ok = true;
    }
    X[A].push_back(B);
  }
  if (ok == false) {
    cout << "0" << endl;
    return 0;
  }
  for (int i = 0; i < N; i++) {
    Q.push(i);
    int cnt = 0;
    while (!Q.empty()) {
      cnt++;
      if (cnt >= 7000000) {
        cout << "1" << endl;
        return 0;
      }
      int a1 = Q.front();
      Q.pop();
      for (int j = 0; j < X[a1].size(); j++) {
        Q.push(X[a1][j]);
      }
    }
  }
  cout << "0" << endl;
  return 0;
}

Fixed submission:
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
vector<int> X[10000];
int N, M, A, B;
bool ok = false;
queue<char> Q;
int main() {
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    cin >> A >> B;
    if (A > B) {
      ok = true;
    }
    X[A].push_back(B);
  }
  if (ok == false) {
    cout << "0" << endl;
    return 0;
  }
  for (int i = 0; i < N; i++) {
    Q.push(i);
    int cnt = 0;
    while (!Q.empty()) {
      cnt++;
      if (cnt >= 1000000) {
        cout << "1" << endl;
        return 0;
      }
      int a1 = Q.front();
      Q.pop();
      for (int j = 0; j < X[a1].size(); j++) {
        Q.push(X[a1][j]);
      }
    }
  }
  cout << "0" << endl;
  return 0;
}
Problem p02369 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

int V, E;

bool dfs(vector<vector<int>> &G, int u, vector<int> &color) {
  color[u] = 1;
  for (auto v : G[u]) {
    if (color[v] == 1)
      return true;
    if (dfs(G, v, color))
      return true;
  }
  color[u] = 2;
  return false;
}

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

  bool has_cycle = false;
  vector<bool> done(V);

  for (int i = 0; i < V; i++)
    if (!done[i]) {
      vector<int> color(V, 0);
      has_cycle |= dfs(G, i, color);
      for (int i = 0; i < V; i++)
        if (color[i] == 2)
          done[i] = true;
    }
  cout << has_cycle << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int V, E;

bool dfs(vector<vector<int>> &G, int u, vector<int> &color) {
  color[u] = 1;
  for (auto v : G[u]) {
    if (color[v] == 1)
      return true;
    if (color[v] == 2)
      continue;
    if (dfs(G, v, color))
      return true;
  }
  color[u] = 2;
  return false;
}

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

  bool has_cycle = false;
  vector<bool> done(V);

  for (int i = 0; i < V; i++)
    if (!done[i]) {
      vector<int> color(V, 0);
      has_cycle |= dfs(G, i, color);
      for (int i = 0; i < V; i++)
        if (color[i] == 2)
          done[i] = true;
    }
  cout << has_cycle << endl;

  return 0;
}
Problem p02369 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int N, M;
vector<int> G[100];
bool used[100];
bool dfs(int p, int s) {
  used[p] = true;
  bool ret = false;
  for (int i = 0; i < G[p].size(); i++) {
    int to = G[p][i];
    if (to == s)
      return true;
    ret |= dfs(to, s);
  }

  return ret;
}

int main() {
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    G[a].push_back(b);
  }

  bool f = false;
  for (int i = 0; i < N; i++) {
    fill_n(used, N, false);
    f |= dfs(i, i);
  }

  cout << f << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int N, M;
vector<int> G[100];
bool used[100];
bool dfs(int p, int s) {
  used[p] = true;
  bool ret = false;
  for (int i = 0; i < G[p].size(); i++) {
    int to = G[p][i];
    if (to == s)
      return true;
    if (used[to])
      continue;
    ret |= dfs(to, s);
  }

  return ret;
}

int main() {
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    G[a].push_back(b);
  }

  bool f = false;
  for (int i = 0; i < N; i++) {
    fill_n(used, N, false);
    f |= dfs(i, i);
  }

  cout << f << endl;
  return 0;
}
Problem p02369 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
#define REP(i, N) for (int i = 0; i < (int)N; i++)

const int VISITED = 1, CURRENTLY_VISITED = 2;

int V, E;
bool findCycle(int node);

vector<vector<int>> adjacentList;
vector<int> states;

bool hasCycle() {
  vector<int> states(V, 0);
  for (int i = 0; i < V; i++) {
    if (states[i] != VISITED && findCycle(i)) {
      return true;
    }
  }
  return false;
}

bool findCycle(int node) {
  if (states[node] == CURRENTLY_VISITED)
    return true;
  states[node] = CURRENTLY_VISITED;
  for (int nn : adjacentList[node]) {
    if (states[node] != VISITED && findCycle(nn))
      return true;
  }
  states[node] = VISITED;
  return false;
}

int main() {
  cin >> V >> E;
  adjacentList = vector<vector<int>>(V);
  states = vector<int>(V, 0);

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

  cout << (hasCycle() ? 1 : 0) << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
#define REP(i, N) for (int i = 0; i < (int)N; i++)

const int VISITED = 1, CURRENTLY_VISITED = 2;

int V, E;
bool findCycle(int node);

vector<vector<int>> adjacentList;
vector<int> states;

bool hasCycle() {
  vector<int> states(V, 0);
  for (int i = 0; i < V; i++) {
    if (states[i] != VISITED && findCycle(i)) {
      return true;
    }
  }
  return false;
}

bool findCycle(int node) {
  if (states[node] == CURRENTLY_VISITED)
    return true;
  states[node] = CURRENTLY_VISITED;
  for (int nn : adjacentList[node]) {
    if (states[nn] != VISITED && findCycle(nn))
      return true;
  }
  states[node] = VISITED;
  return false;
}

int main() {
  cin >> V >> E;
  adjacentList = vector<vector<int>>(V);
  states = vector<int>(V, 0);

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

  cout << (hasCycle() ? 1 : 0) << endl;
  return 0;
}
Problem p02370 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

vector<int> edge[110];
int h[110];

int V;
int E;

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

  for (int i = 0; i < E; i++) {
    int s;
    int t;

    cin >> s >> t;

    edge[s].push_back(t);

    h[t]++;
  }

  stack<int> st;

  for (int i = 0; i < V; i++) {
    if (h[i] == 0)
      st.push(i);
  }

  vector<int> topolo;

  while (!st.empty()) {
    int v = st.top();
    st.pop();

    topolo.push_back(v);

    for (int to : edge[v]) {
      h[to]--;

      if (h[to] == 0) {
        st.push(to);
      }
    }
  }

  for (int ans : topolo) {
    cout << ans << endl;
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

vector<int> edge[10010];
int h[10010];

int V;
int E;

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

  for (int i = 0; i < E; i++) {
    int s;
    int t;

    cin >> s >> t;

    edge[s].push_back(t);

    h[t]++;
  }

  stack<int> st;

  for (int i = 0; i < V; i++) {
    if (h[i] == 0)
      st.push(i);
  }

  vector<int> topolo;

  while (!st.empty()) {
    int v = st.top();
    st.pop();

    topolo.push_back(v);

    for (int to : edge[v]) {
      h[to]--;

      if (h[to] == 0) {
        st.push(to);
      }
    }
  }

  for (int ans : topolo) {
    cout << ans << endl;
  }

  return 0;
}
Problem p02370 (C++)

Buggy submission (status: 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 < 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;
}


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 < 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;
}
Problem p02370 (C++)

Buggy submission (status: 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[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;
}

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() { 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;
}
Problem p02370 (C++)

Buggy submission (status: Runtime Error):
#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);
  }
}

Fixed submission:
#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);
  }
}
Problem p02370 (C++)

Buggy submission (status: Runtime Error):
#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;
}

Fixed submission:
#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;
}
Problem p02370 (C++)

Buggy submission (status: 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 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;
}

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 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;
}
Problem p02370 (C++)

Buggy submission (status: Memory Limit Exceeded):
#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;
}

Fixed submission:
#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;
}
Problem p02370 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
#include <vector>
using namespace std;

const int V_MAX = 1000;

int main() {
  int v, e;
  int table[V_MAX] = {0};
  vector<int> edge[V_MAX];
  vector<int> ans;

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

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

  for (int i = 0; i < v; i++) {
    if (table[i] == 0) {
      ans.push_back(i);
    }
  }

  for (int i = 0; i < ans.size(); i++) {
    vector<int> e = edge[ans[i]];
    for (int j = 0; j < e.size(); j++) {
      if (--(table[e[j]]) == 0) {
        ans.push_back(e[j]);
      }
    }
  }

  for (vector<int>::iterator it = ans.begin(); it != ans.end(); it++) {
    printf("%d\n", *it);
  }

  return 0;
}

Fixed submission:
#include <stdio.h>
#include <vector>
using namespace std;

const int V_MAX = 10000;

int main() {
  int v, e;
  int table[V_MAX] = {0};
  vector<int> edge[V_MAX];
  vector<int> ans;

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

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

  for (int i = 0; i < v; i++) {
    if (table[i] == 0) {
      ans.push_back(i);
    }
  }

  for (int i = 0; i < ans.size(); i++) {
    vector<int> e = edge[ans[i]];
    for (int j = 0; j < e.size(); j++) {
      if (--(table[e[j]]) == 0) {
        ans.push_back(e[j]);
      }
    }
  }

  for (vector<int>::iterator it = ans.begin(); it != ans.end(); it++) {
    printf("%d\n", *it);
  }

  return 0;
}
Problem p02370 (C++)

Buggy submission (status: Runtime Error):
#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;
}

Fixed submission:
#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;
}