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

#include <algorithm>
#include <cstdio>
#include <map>
#include <set>
#include <vector>

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
using namespace std;

typedef map<int, vector<int>> Graph;

bool visit(Graph &g, int v, vector<int> &order, map<int, int> &color) {
  color[v] = 1;
  FOR(e, g[v]) {
    if (color[*e /*->dst*/] == 2)
      continue;
    if (color[*e /*->dst*/] == 1)
      return false;
    if (!visit(g, *e /*->dst*/, order, color))
      return false;
  }
  order.push_back(v);
  color[v] = 2;
  return true;
}
bool topologicalSort(Graph &g, vector<int> &order) {
  // int n = g.size();
  map<int, int> color; //(n);
  FOR(u, g)
  if (!color[u->first] && !visit(g, u->first, order, color)) return false;
  reverse(ALL(order));
  return true;
}

int main() {
  int i, V, E, s, t;
  // for(scanf("%d",&T);T;putchar(--T?' ':'\\n')){
  scanf("%d%d", &V, &E);
  Graph g; //(V);
  set<int> orphan;
  for (int i = 0; i < V; i++)
    orphan.insert(i);
  for (; E--;)
    scanf("%d%d", &s, &t), g[s].push_back(t), orphan.erase(s), orphan.erase(t);
  // for(;!g.empty();){
  vector<int> order;
  topologicalSort(g, order);
  for (i = 0; i < order.size(); i++) {
    printf("%d\\n", order[i]);
    g.erase(order[i]);
  }
  //}
  FOR(u, orphan)
  printf("%d\\n", *u); // 孤立した頂点が存在するデータがあるので注意
  //}
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <map>
#include <vector>

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
using namespace std;

typedef map<int, vector<int>> Graph;

bool visit(Graph &g, int v, vector<int> &order, map<int, int> &color) {
  color[v] = 1;
  FOR(e, g[v]) {
    if (color[*e /*->dst*/] == 2)
      continue;
    if (color[*e /*->dst*/] == 1)
      return false;
    if (!visit(g, *e /*->dst*/, order, color))
      return false;
  }
  order.push_back(v);
  color[v] = 2;
  return true;
}
bool topologicalSort(Graph &g, vector<int> &order) {
  // int n = g.size();
  map<int, int> color; //(n);
  FOR(u, g)
  if (!color[u->first] && !visit(g, u->first, order, color)) return false;
  reverse(ALL(order));
  return true;
}

int main() {
  int i, V, E, s, t;
  // for(scanf("%d",&T);T;putchar(--T?' ':'\\n')){
  scanf("%d%d", &V, &E);
  Graph g; //(V);
  for (; E--;)
    scanf("%d%d", &s, &t), g[s].push_back(t);
  // for(;!g.empty();){
  vector<int> order;
  topologicalSort(g, order);
  if (V != order.size())
    return 1;
  for (i = 0; i < V; i++) {
    printf("%d\\n", order[i]);
    g.erase(order[i]);
  }
  //}
  //}
}
Problem p02370 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

using namespace std;

const int MAX = 10000;
int n, color[MAX], indeg[MAX];
vector<int> v, DAG[MAX];

void bfs(int s);

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

void bfs(int s) {
  queue<int> q;
  q.push(s);
  color[s] = 1;

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

    for (int i = 0; i < DAG[u].size(); i++) {
      int w = DAG[u][i];
      indeg[w]--;
      if (indeg[w] == 0 && color[w] == 0) {
        q.push(w);
        color[w] = 1;
      }
    }
  }
}

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

  topologicalSort();

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


Error: MLE

Fixed submission:

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

using namespace std;

const int MAX = 10000;
int n, DAG[MAX][MAX], color[MAX], indeg[MAX];
vector<int> v;
// queue<int> q;

void bfs(int s);

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

void bfs(int s) {
  queue<int> q;
  q.push(s);
  color[s] = 1;

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

    for (int i = 0; i < n; i++) {
      if (DAG[u][i] == 1) {
        indeg[i]--;
        if (indeg[i] == 0 && color[i] == 0) {
          q.push(i);
          color[i] = 1;
        }
      }
    }
  }
}

int main() {
  int e;
  cin >> n >> e;
  int s, t;
  for (int i = 0; i < e; i++) {
    cin >> s >> t;
    DAG[s][t] = 1;
    indeg[t]++;
  }

  topologicalSort();

  for (int i = 0; i < n; i++)
    cout << v[i] << endl;
  return 0;
}
Problem p02370 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define MAX_V 100000
#define rep(i, n) for (int i = 0; i < n; i++)
vector<int> ans;
vector<int> g[MAX_V];

void dfs(vector<bool> &used, int s) {
  used[s] = false;
  rep(i, g[s].size()) {
    if (used[g[s][i]]) {
      dfs(used, g[s][i]);
      //	cout<<i<<endl;
    }
  }
  ans.push_back(s);
  return;
}
void tsort(int v) {
  vector<bool> used(v, true);
  rep(i, v) {
    if (used[i]) {
      dfs(used, i);
    }
  }
  // rep(i,v)if(used[i])cout<<i<<endl;
  return;
}
int main() {
  int v, e;
  cin >> v >> e;
  // vector<vector<int> > g(v);
  int s, t;
  rep(i, e) {
    cin >> s >> t;
    g[s].push_back(t);
  }
  tsort(v);
  for (int i = ans.size() - 1; i >= 0; i--)
    cout << ans[i] << endl;

  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
vector<int> ans;
int pass = 0;
void dfs(vector<vector<int>> g, vector<int> &used, int s) {
  used[s] = 0;
  pass++;
  // auto it = lower_bound(g[s].begin(),g[s].end(),s);
  // int k = (int)(it-g[s].begin());
  // for(int i=k;g[s].size();i++ ){
  rep(i, g[s].size()) {
    if (used[g[s][i]]) {
      dfs(g, used, g[s][i]);
      //	cout<<i<<endl;
    }
  }
  ans.push_back(s);
  return;
}
void topological(vector<vector<int>> g, int v) {
  vector<int> used(v, 1);
  auto it = lower_bound(used.begin(), used.end(), 1);
  int i = (it - used.begin());
  // rep(i,v){
  // if(used[i]){
  while (pass <= v - 1) {
    // int i = it-used.begin();
    dfs(g, used, i);
    // cout<<i<<endl;
    auto it = lower_bound(used.begin(), used.end(), 1);
    i = (it - used.begin());
    //	}
  }
  // rep(i,v)if(used[i])cout<<i<<endl;
  return;
}
int main() {
  int v, e;
  cin >> v >> e;
  vector<vector<int>> g(v);
  int s, t;
  rep(i, e) {
    cin >> s >> t;
    g[s].push_back(t);
  }
  /*rep(i,e){
  sort(g[i].begin(),g[i].end());
  }*/
  /*rep(i,e){
          rep(j,g[i].size()){
                  cout<<g[i][j]<<' ';
          }
          cout<<endl;
  }*/
  topological(g, v);
  for (int i = ans.size() - 1; i >= 0; i--)
    cout << ans[i] << endl;

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

#include <cstring>
#include <iostream>
#include <list>
#include <queue>
#include <vector>
#define MAXV 10005
#define MAXE 100005
using namespace std;

int N, E;
bool V[MAXV];
vector<int> G[MAXV], out;
int indeg[MAXV];

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

void tsort() {
  int i;
  for (i = 0; i < N; i++) {
    if (!V[i] && indeg[i] == 0) {
      bfs(i);
    }
  }
  for (i = 0; i < out.size(); i++)
    cout << out[i] << endl;
}

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

  memset(indeg, 0, sizeof(indeg));
  for (i = 0; i < N; i++)
    V[i] = false;
  for (i = 0; i < N; i++) {
    for (j = 0; j < G[i].size(); j++) {
      indeg[G[i][j]]++;
    }
  }
  tsort();
  return 0;
}

Error: -11

Fixed submission:

#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
#define MAXV 10005
#define MAXE 100005
using namespace std;

int N, E;
bool V[MAXV];
vector<int> G[MAXV], out;
int indeg[MAXV];

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

void tsort() {
  int i;
  for (i = 0; i < N; i++) {
    if (!V[i] && indeg[i] == 0)
      bfs(i);
  }
  for (vector<int>::iterator it = out.begin(); it != out.end(); it++)
    cout << *it << endl;
}

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

  memset(indeg, 0, sizeof(indeg));
  for (i = 0; i < N; i++)
    V[i] = false;
  for (i = 0; i < N; i++) {
    for (j = 0; i < G[i].size(); j++) {
      indeg[G[i][j]]++;
    }
  }
  tsort();
  return 0;
}
Problem p02370 (C++) — buggy submission, verdict: Runtime Error

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


Error: WA

Fixed submission:

#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;
}
Problem p02370 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

int V, E;
vector<unordered_set<int>> redge;
// bool redge[10000][10000];

// bool memo[10000];
unordered_set<int> memo;
void topological_sort(int u) {
  //   if(memo[u]) return;
  //   memo[u] = true;
  //   for(int v = 0; v < V; ++v) if(redge[u][v]) topological_sort(v);
  if (memo.count(u))
    return;
  memo.insert(u);
  for (int v : redge[u])
    topological_sort(v);
  cout << u << endl;
}

int main() {
  cin >> V >> E;
  redge.resize(V);
  for (int i = 0; i < E; ++i) {
    int u, v;
    cin >> u >> v;
    redge[v].insert(u);
    //     redge[v][u] = true;
  }
  for (int i = 0; i < V; ++i)
    topological_sort(i);
}

Error: MLE

Fixed submission:

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

int V, E;
bool redge[10000][10000];

bool memo[10000];
void topological_sort(int u) {
  if (memo[u])
    return;
  memo[u] = true;
  for (int v = 0; v < V; ++v)
    if (redge[u][v])
      topological_sort(v);
  cout << u << endl;
}

int main() {
  cin >> V >> E;
  for (int i = 0; i < E; ++i) {
    int u, v;
    cin >> u >> v;
    redge[v][u] = true;
  }
  for (int i = 0; i < V; ++i)
    topological_sort(i);
}
Problem p02370 (C++) — buggy submission, verdict: Runtime Error

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

using namespace std;
typedef long long ll;

/* topologicalSort */
#define topo_SIZE 10000

vector<int> topologicalSort(vector<vector<int>> G, int n = -1) {
  int indeg[topo_SIZE] = {0}, u;
  vector<int> ret, ret2;
  queue<int> que;

  if (n == -1)
    n = (int)G.size();

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

  for (int i = 0; i < n; i++) {
    if (indeg[i] == 0) {
      que.push(i);
    }
  }

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

    ret.push_back(u);

    for (int i = 0; i < G[u].size(); i++) {
      indeg[G[u][i]]--;
      if (indeg[G[u][i]] == 0) {
        que.push(G[u][i]);
      }
    }
  }

  if (n == (int)ret.size()) {
    return ret;
  } else {
    return ret2;
  }
}

int main() {
  int v, e, a, b;
  vector<int> ans;
  vector<vector<int>> in(10000);

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

  for (int i = 0; i < e; i++) {
    scanf("%d%d", &a, &b);

    in[a].push_back(b);
  }

  ans = topologicalSort(in, v);

  if (ans.size() == 0) {
    puts("-1");
    return 0;
  }

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

  return 0;
}

Error: 0

Fixed submission:

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

using namespace std;
typedef long long ll;

const int INF = 2000000000;

/* topologicalSort */
#define topo_SIZE 1000

vector<int> topologicalSort(vector<vector<int>> G, int n = -1) {
  int indeg[topo_SIZE] = {0}, u;
  vector<int> ret, ret2;
  queue<int> que;

  if (n == -1)
    n = (int)G.size();

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

  for (int i = 0; i < n; i++) {
    if (indeg[i] == 0) {
      que.push(i);
    }
  }

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

    ret.push_back(u);

    for (int i = 0; i < G[u].size(); i++) {
      indeg[G[u][i]]--;
      if (indeg[G[u][i]] == 0) {
        que.push(G[u][i]);
      }
    }
  }

  if (n == (int)ret.size()) {
    return ret;
  } else {
    return ret2;
  }
}

int main() {
  int v, e, a, b;
  vector<int> ans;
  vector<vector<int>> in(10000);

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

  for (int i = 0; i < e; i++) {
    scanf("%d%d", &a, &b);

    in[a].push_back(b);
  }

  ans = topologicalSort(in, v);

  if (ans.size() == 0) {
    puts("-1");
    return 0;
  }

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

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

#include <bits/stdc++.h>
using namespace std;
struct cww {
  cww() {
    ios::sync_with_stdio(false);
    cin.tie(0);
  }
} star;
#define P(x) cout << (x) << "\\n"
#define p(x) cout << (x)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define vv(type, c, m, n, i) vector<vector<type>> c(m, vector<type>(n, i));
#define rep(i, a, n) for (int i = (a), i##_len = (n); i < i##_len; ++i)
#define rrep(i, a, n) for (int i = (a), i##_len = (n); i > i##_len; --i)
#define len(x) ((int)(x).size())
#define mp make_pair
#define eb emplace_back
typedef long long ll;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<long long> vll;
typedef vector<string> vs;
typedef vector<bool> vb;
const constexpr int MAX = 1e5;
const constexpr ll INF = 1LL << 32;
vi G[MAX], indeg(MAX, 0), out;
vb V(MAX, false);
int n;
inline void bfs(int s) {
  deque<int> dq;
  dq.eb(s);
  V[s] = true;
  int u, v;
  while (!dq.empty()) {
    u = dq.front();
    dq.pop_front();
    out.eb(u);
    rep(i, 0, len(G[u])) {
      v = G[u][i];
      if (--indeg[v] == 0 && !V[v]) {
        V[v] = true;
        dq.eb(v);
      }
    }
  }
}
inline void tsort() {
  rep(u, 0, n) rep(i, 0, len(G[u]))++ indeg[G[u][i]];
  rep(u, 0, n) if (indeg[u] == 0 && !V[u]) bfs(u);
  rep(i, 0, len(out)) P(out[i]);
}
int main() {
  int m;
  cin >> n >> m;
  int s, t;
  rep(i, 0, m) {
    cin >> s >> t;
    G[s].eb(t);
  }
  tsort();
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
struct cww {
  cww() {
    ios::sync_with_stdio(false);
    cin.tie(0);
  }
} star;
#define P(x) cout << (x) << "\\n"
#define p(x) cout << (x)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define vv(type, c, m, n, i) vector<vector<type>> c(m, vector<type>(n, i));
#define rep(i, a, n) for (int i = (a), i##_len = (n); i < i##_len; ++i)
#define rrep(i, a, n) for (int i = (a), i##_len = (n); i > i##_len; --i)
#define len(x) ((int)(x).size())
#define mp make_pair
#define eb emplace_back
typedef long long ll;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<long long> vll;
typedef vector<string> vs;
typedef vector<bool> vb;
const constexpr int MAX = 100;
const constexpr ll INF = 1LL << 32;
vi G[MAX], indeg(MAX, 0), out;
vb V(MAX, false);
int n;
deque<int> dq;
inline void bfs(int s) {
  dq.clear();
  dq.eb(s);
  V[s] = true;
  int u, v;
  while (!dq.empty()) {
    u = dq.front();
    dq.pop_front();
    out.eb(u);
    rep(i, 0, len(G[u])) {
      v = G[u][i];
      if (--indeg[v] == 0 && !V[v]) {
        V[v] = true;
        dq.eb(v);
      }
    }
  }
}
inline void tsort() {
  rep(u, 0, n) rep(i, 0, len(G[u]))++ indeg[G[u][i]];
  rep(u, 0, n) if (indeg[u] == 0 && !V[u]) bfs(u);
  rep(i, 0, len(out)) P(out[i]);
}
int main() {
  int m;
  cin >> n >> m;
  int s, t;
  rep(i, 0, m) {
    cin >> s >> t;
    G[s].eb(t);
  }
  tsort();
  return 0;
}
Problem p02370 (C++) — buggy submission, verdict: Runtime Error

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

Error: 0

Fixed submission:

#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;
}
Problem p02370 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <list>
#include <vector>
#define N 10000
using namespace std;

list<int> l;
bool color[N] = {false};
vector<int> adj[N];
int n;

void topo(int k) {
  for (int i = 0; i < adj[k].size(); i++) {
    if (!color[adj[k][i]])
      topo(adj[k][i]);
  }
  if (!color[k])
    l.push_front(k);
  color[k] = true;
}

int main(void) {
  int u, v;
  int e;
  cin >> n >> e;
  for (int i = 0; i < e; i++) {
    cin >> u >> v;
    adj[u].push_back(v);
  }
  for (int i = 0; i < n; i++) {
    if (!color[i])
      topo(i);
  }
  for (list<int>::iterator it = l.begin(); it != l.end(); it++)
    cout << *it << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <list>
#include <vector>
#define N 10000
using namespace std;

list<int> l;
bool color[N] = {false};
vector<int> adj[N];
int n;

void topo(int k) {
  for (int i = 0; i < adj[k].size(); i++)
    topo(adj[k][i]);
  if (!color[k])
    l.push_front(k);
  color[k] = true;
}

int main(void) {
  int u, v;
  int e;
  cin >> n >> e;
  for (int i = 0; i < e; i++) {
    cin >> u >> v;
    adj[u].push_back(v);
  }
  for (int i = 0; i < n; i++) {
    if (!color[i])
      topo(i);
  }
  for (list<int>::iterator it = l.begin(); it != l.end(); it++)
    cout << *it << endl;
  return 0;
}
Problem p02371 (C++) — buggy submission, verdict: Runtime Error

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define INF 999999999

#define MAX_V 100000

struct edge {
  int to, weight;
};

vector<edge> G[MAX_V];

pii dfs(int prev, int v) { // searching farthest node
  pii r(0, v);

  rep(i, G[v].size()) {
    struct edge e = G[v][i];
    if (e.to == prev)
      continue;
    pii tmp = dfs(v, e.to);
    tmp.first += e.weight;
    if (r.first < tmp.first)
      r = tmp;
  }
  return r;
}

int diameter() {
  pii r = dfs(-1, 0);
  pii t = dfs(-1, r.second);
  return t.first; // (r.second, t.second) is farthest pair
}

int main() {
  int n;
  cin >> n;
  rep(i, n - 1) {
    int s, t, w;
    cin >> s >> t >> w;
    G[s].pb(edge{t, w});
    G[t].pb(edge{s, w});
  }
  cout << diameter() << endl;
}

Error: 0

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define INF 999999999

#define MAX_V 100

struct edge {
  int to, weight;
};

vector<edge> G[MAX_V];

pii dfs(int prev, int v) { // searching farthest node
  pii r(0, v);

  rep(i, G[v].size()) {
    struct edge e = G[v][i];
    if (e.to == prev)
      continue;
    pii tmp = dfs(v, e.to);
    tmp.first += e.weight;
    if (r.first < tmp.first)
      r = tmp;
  }
  return r;
}

int diameter() {
  pii r = dfs(-1, 0);
  pii t = dfs(-1, r.second);
  return t.first; // (r.second, t.second) is farthest pair
}

int main() {
  int n;
  cin >> n;
  rep(i, n - 1) {
    int s, t, w;
    cin >> s >> t >> w;
    G[s].pb(edge{t, w});
    G[t].pb(edge{s, w});
  }
  cout << diameter() << endl;
}