alexjercan/bugnet huggingface dataset:
Problem p02368 (C++) — buggy submission, verdict: 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(n);
  cin >> l;
  for (int i = 0; i < l; i++) {
    cin >> u >> v;
    printf("%d\\n", (cmp[u] == cmp[v]));
  }
}

Error: 0

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(m);
  cin >> l;
  for (int i = 0; i < l; i++) {
    cin >> u >> v;
    printf("%d\\n", (cmp[u] == cmp[v]));
  }
}
Problem p02368 (C++) — buggy submission, verdict: Runtime Error

#include <cstring>
#include <iostream>
#include <vector>

using namespace std;

#define MAX_V 10010

int V, cmp[MAX_V];
bool used[MAX_V];
vector<int> G[MAX_V], rG[MAX_V], vs;

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 < (int)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 < (int)rG[v].size(); i++) {
    if (!used[rG[v][i]]) {
      rdfs(rG[v][i], k);
    }
  }
}

int scc() {
  memset(used, false, sizeof(used));
  vs.clear();
  for (int v = 0; v < V; v++) {
    if (!used[v]) {
      dfs(v);
    }
  }
  memset(used, false, 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 E, s, t;
  cin >> V >> E;
  while (E--) {
    cin >> s >> t;
    add_edge(s, t);
  }

  scc();

  int q, u, v;
  cin >> q;
  while (q--) {
    cin >> u >> v;
    cout << (cmp[u] == cmp[v]) << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <cstring>
#include <iostream>
#include <vector>

using namespace std;

#define MAX_V 1000

int V, cmp[MAX_V];
bool used[MAX_V];
vector<int> G[MAX_V], rG[MAX_V], vs;

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 < (int)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 < (int)rG[v].size(); i++) {
    if (!used[rG[v][i]]) {
      rdfs(rG[v][i], k);
    }
  }
}

int scc() {
  memset(used, false, sizeof(used));
  vs.clear();
  for (int v = 0; v < V; v++) {
    if (!used[v]) {
      dfs(v);
    }
  }
  memset(used, false, 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 E;
  cin >> V >> E;
  while (E--) {
    int s, t;
    cin >> s >> t;
    add_edge(s, t);
  }
  scc();
  int Q;
  cin >> Q;
  while (Q--) {
    int u, v;
    cin >> u >> v;
    cout << (cmp[u] == cmp[v]) << endl;
  }
  return 0;
}
Problem p02368 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>

#include <map>
#include <vector>
using namespace std;

vector<int> G[20000];
vector<int> rG[20000];
int id[20000];
int rid[20000];
int cid[20000];
void enume(int s, int &n) {
  if (id[s])
    return;
  id[s] = -1;
  for (int t : G[s])
    enume(t, n);
  id[s] = ++n;
  rid[n] = s;
}
void dfs(int s, int n) {
  if (cid[s])
    return;
  cid[s] = n;
  for (int t : rG[s])
    dfs(t, n);
}

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

  int k = 0;
  for (int i = 0; i < m; i++)
    enume(i, k);

  for (int i = m; i; i--)
    dfs(rid[i], i);

  int q;
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    printf("%d\\n", cid[u] == cid[v]);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>

#include <map>
#include <vector>
using namespace std;

vector<int> G[20000];
vector<int> rG[20000];
int id[20000];
int visited[20000];
int rid[20000];
int cid[20000];
void enume(int s, int &n) {
  if (visited[s])
    return;
  visited[s] = 1;
  for (int t : G[s])
    enume(t, n);
  id[s] = ++n;
  rid[n] = s;
}
void dfs(int s, int n) {
  if (cid[s])
    return;
  cid[s] = n;
  for (int t : rG[s])
    dfs(t, n);
}

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

  int k = 0;
  for (int i = 0; i < m; i++)
    enume(i, k);
  for (int i = n; i; i--)
    dfs(rid[i], i);

  int q;
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    printf("%d\\n", cid[u] == cid[v]);
  }
  return 0;
}
Problem p02368 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
vector<int> X[100003];
bool COLOR[100003];
int N, M, Q1, A, B;
pair<int, int> R[100003];
vector<pair<int, int>> S[100003];
int T[100003];
int main() {
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    scanf("%d%d", &A, &B);
    X[A].push_back(B);
  }
  cin >> Q1;
  for (int i = 0; i < Q1; i++) {
    scanf("%d%d", &R[i].first, &R[i].second);
    S[R[i].first].push_back(make_pair(R[i].second, i));
    S[R[i].second].push_back(make_pair(R[i].first, i));
  }
  for (int i = 0; i < N; i++) {
    if (S[i].size() >= 1) {
      queue<int> Q;
      Q.push(i);
      for (int i = 0; i < N; i++) {
        COLOR[i] = false;
      }
      COLOR[i] = true;
      while (!Q.empty()) {
        int B = Q.front();
        Q.pop();
        for (int j = 0; j < X[B].size(); j++) {
          if (COLOR[X[B][j]] == false) {
            COLOR[X[B][j]] = true;
            Q.push(X[B][j]);
          }
        }
      }
      for (int j = 0; j < S[i].size(); j++) {
        int a1 = S[i][j].first, a2 = S[i][j].second;
        if (COLOR[a1] == 1) {
          T[a2]++;
        }
      }
    }
  }
  for (int i = 0; i < Q1; i++) {
    if (T[i] == 2) {
      printf("1\\n");
    } else {
      printf("0\\n");
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define MAX_N 100003
vector<int> X[MAX_N];
bool COLOR[MAX_N];
int N, M, Q1, A, B;
pair<int, int> R[MAX_N];
vector<pair<int, int>> S[MAX_N];
int T[MAX_N];
int main() {
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    cin >> A >> B;
    X[A].push_back(B);
  }
  cin >> Q1;
  for (int i = 0; i < Q1; i++) {
    cin >> R[i].first >> R[i].second;
    S[R[i].first].push_back(make_pair(R[i].second, i));
    S[R[i].second].push_back(make_pair(R[i].first, i));
  }
  for (int i = 0; i < N; i++) {
    if (S[i].size() >= 1) {
      queue<int> Q;
      Q.push(i);
      for (int i = 0; i < N; i++) {
        COLOR[i] = false;
      }
      COLOR[i] = true;
      while (!Q.empty()) {
        int B = Q.front();
        Q.pop();
        for (int j = 0; j < X[B].size(); j++) {
          if (COLOR[X[B][j]] == false) {
            COLOR[X[B][j]] = true;
            Q.push(X[B][j]);
          }
        }
      }
      for (int j = 0; j < S[i].size(); j++) {
        int a1 = S[i][j].first, a2 = S[i][j].second;
        if (COLOR[a1] == 1) {
          T[a2]++;
        }
      }
    }
  }
  for (int i = 0; i < Q1; i++) {
    if (T[i] == 2) {
      cout << '1' << endl;
    } else {
      cout << '0' << endl;
    }
  }
  return 0;
}
Problem p02368 (C++) — buggy submission, verdict: Runtime Error

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

typedef std::vector<std::vector<int>> graph;

std::vector<int> makescc(const graph &g) {
  const int n = g.size();
  std::vector<int> num(n), low(n), scc(n, -1);
  std::vector<bool> visited(n);
  std::stack<int> r, s;
  int index = 0;

  for (int u = 0; u < n; ++u) {
    if (visited[u]) {
      continue;
    }
    r.push(u);
    while (!r.empty()) {
      const int v = r.top();
      if (num[v] == 0) {
        s.push(v);
        num[v] = low[v] = ++index;
        for (const int w : g[v]) {
          if (num[w] == 0) {
            r.push(w);
          }
        }
        continue;
      }
      r.pop();
      if (visited[v]) {
        continue;
      }
      visited[v] = true;
      for (const int w : g[v]) {
        if (scc[w] == -1) {
          low[v] = std::min(low[v], low[w]);
        }
      }
      if (num[v] == low[v]) {
        int w;
        do {
          w = s.top();
          s.pop();
          scc[w] = v;
        } while (w != v);
      }
    }
  }

  return scc;
}

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

  int V, E;
  std::cin >> V >> E;
  graph g(V);
  for (int i = 0; i < E; ++i) {
    int s, t;
    std::cin >> s >> t;
    g[s].push_back(t);
  }
  auto scc = makescc(g);

  int Q;
  std::cin >> Q;
  for (int i = 0; i < Q; ++i) {
    int u, v;
    std::cin >> u >> v;
    std::cout << (scc[u] == scc[v]) << '\\n';
  }
}

Error: 0

Fixed submission:

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

typedef std::vector<std::vector<int>> graph;

std::vector<int> makescc(const graph &g) {
  const int n = g.size();
  std::vector<int> num(n), low(n), scc(n, -1);
  std::vector<bool> visited(n);
  std::stack<int> r, s;
  int index = 0;

  for (int u = 0; u < n; ++u) {
    if (num[u] == 0) {
      r.push(u);
      while (!r.empty()) {
        const int v = r.top();
        if (!visited[v]) {
          visited[v] = true;
          s.push(v);
          num[v] = low[v] = ++index;
          for (const int w : g[v]) {
            if (!visited[w]) {
              r.push(w);
            }
          }
        } else {
          r.pop();
          for (const int w : g[v]) {
            if (scc[w] == -1) {
              low[v] = std::min(low[v], low[w]);
            }
          }
          if (num[v] == low[v]) {
            int w;
            do {
              w = s.top();
              s.pop();
              scc[w] = v;
            } while (w != v);
          }
        }
      }
    }
  }

  return scc;
}

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

  int V, E;
  std::cin >> V >> E;
  graph g(V);
  for (int i = 0; i < E; ++i) {
    int s, t;
    std::cin >> s >> t;
    g[s].push_back(t);
  }
  auto scc = makescc(g);

  int Q;
  std::cin >> Q;
  for (int i = 0; i < Q; ++i) {
    int u, v;
    std::cin >> u >> v;
    std::cout << (scc[u] == scc[v]) << '\\n';
  }
}
Problem p02368 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <set>
#include <stack>
#include <vector>
using namespace std;
vector<vector<int>> G1(100000), G2(100000);
vector<bool> visit1(100000, false), visit2(100000, false);
int timer = 0;

//?????????????????????DFS?????????????????????????????????????????????????????????
void dfs1(int u, vector<int> &prenum) {
  visit1[u] = true;
  for (int v : G1[u]) {
    if (visit1[v] == false) {
      dfs1(v, prenum);
    }
  }
  prenum[timer++] = u;
}

//?????????????????????????????????????????°????????????DFS
//????§???????????????§??????????????????????????????????????????????????????
//????????§????????¢???????????¶????????£?????????????????§?????°????????????CSS????????£????????????
//??????????´?????????£??????????????????????¨????????????????????????§???????????????????????????????????????
//?????????DFS?????????
//??????????????°?????????CSS????§£?????°???????????§???????????????

// O(V+E)
void dfs2(int u, vector<int> &Ans) {
  visit2[u] = true;
  Ans[u] = timer;
  for (int v : G2[u]) {
    if (visit2[v] == false) {
      dfs2(v, Ans);
    }
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int V, E;
  cin >> V >> E;
  vector<int> Ans(V);
  vector<int> prenum(V);
  for (int i = 0; i < E; i++) {
    int s, t;
    cin >> s >> t;
    G1[s].emplace_back(t);
    G2[t].emplace_back(s);
  }

  for (int i = 0; i < V; i++) {
    if (visit1[i] == false) {
      dfs1(i, prenum);
    }
  }
  timer = 0;
  for (int i = V - 1; i >= 0; i--) {
    if (visit2[prenum[i]] == false) {
      dfs2(prenum[i], Ans);
      timer++;
    }
  }

  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int u, v;
    cin >> u >> v;
    if (Ans[u] == Ans[v]) {
      cout << 1 << endl;
    } else {
      cout << 0 << endl;
    }
  }

  /*
          cout << "prenum" << endl;
          for (int i = 0; i < V; i++) {
                  cout << i << " " << prenum[i] << endl;
          }
          cout << "Ans" << endl;
          for (int i = 0; i < V;i++) {
                  cout << i << " " << Ans[i] << endl;
          }
  */
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <vector>
using namespace std;
vector<vector<int>> G(100000);
vector<bool> visit(100000, false);

bool dfs(int u, int v) {
  visit[u] = true;
  for (int next : G[u]) {
    if (next == v) {
      return true;
    }
    if (visit[next] == false) {
      if (dfs(next, v)) {
        return true;
      }
    }
  }
  return false;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int V, E;
  cin >> V >> E;
  vector<int> Ans;
  for (int i = 0; i < E; i++) {
    int s, t;
    cin >> s >> t;
    G[s].emplace_back(t);
  }

  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int u, v;
    cin >> u >> v;
    bool f1 = dfs(u, v);
    visit = vector<bool>(100000, false);
    bool f2 = dfs(v, u);
    visit = vector<bool>(100000, false);
    if (f1 && f2) {
      Ans.emplace_back(1);
    } else {
      Ans.emplace_back(0);
    }
  }
  for (int i : Ans) {
    cout << i << endl;
  }
}
Problem p02369 (C++) — buggy submission, verdict: Time Limit Exceeded

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

// 閉路があることの判定
// dfs(再帰)による実装

bool dfs(int ex, int node, vector<vector<int>> adjlist, vector<bool> &used,
         vector<bool> &u) {

  for (int i = 0; i < adjlist[node].size(); i++) {
    // cout << node << " " << adjlist[node][i] << endl;
    // cout << u[0] << u[1] << u[2] << u[3] <<  endl;

    if (used[adjlist[node][i]] == true)
      return true;
    else if (u[adjlist[node][i]] == true)
      continue;
    else if (adjlist[node][i] == ex)
      continue;
    else if (used[adjlist[node][i]] == false) {
      used[adjlist[node][i]] = true;
      u[adjlist[node][i]] = true;
      bool flag = dfs(node, adjlist[node][i], adjlist, used, u);
      if (flag)
        return true;
      used[adjlist[node][i]] = false;
    }
  }
  return false;
}

int main() {

  int V, E;
  cin >> V >> E;                  // V:= 頂点の数    E:= 辺の数
  vector<vector<int>> adjlist(V); // first := 行先　second := コスト

  // 入力
  for (int i = 0; i < E; i++) {
    int a, b;
    cin >> a >> b; // a ←→ b に コスト c の辺がある
    adjlist[a].push_back(b);
  }

  vector<bool> used(V, false);
  vector<bool> u(V, false);
  int node = 0;
  bool flag = false;
  for (int i = 0; i < V; i++) {
    if (u[i] == true)
      continue;
    else {
      used[i] = true;
      u[i] = true;
      flag = dfs(-1, i, adjlist, used, u);
      used[i] = false;
      if (flag)
        break;
    }
  }

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

  return 0;
}


Error: TLE

Fixed submission:

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

// 閉路があることの判定
// dfs(再帰)による実装

bool dfs(int ex, int node, vector<vector<int>> adjlist, vector<bool> &used,
         vector<bool> &u) {

  for (int i = 0; i < adjlist[node].size(); i++) {
    // cout << node << " " << adjlist[node][i] << endl;
    // cout << u[0] << u[1] << u[2] << u[3] <<  endl;
    if (adjlist[node][i] == ex)
      continue;
    else if (used[adjlist[node][i]] == false) {
      used[adjlist[node][i]] = true;
      u[adjlist[node][i]] = true;
      bool flag = dfs(node, adjlist[node][i], adjlist, used, u);
      if (flag)
        return true;
      used[adjlist[node][i]] = false;
    } else {
      return true;
    }
  }
  return false;
}

int main() {

  int V, E;
  cin >> V >> E;                  // V:= 頂点の数    E:= 辺の数
  vector<vector<int>> adjlist(V); // first := 行先　second := コスト

  // 入力
  for (int i = 0; i < E; i++) {
    int a, b;
    cin >> a >> b; // a ←→ b に コスト c の辺がある
    adjlist[a].push_back(b);
  }

  vector<bool> used(V, false);
  vector<bool> u(V, false);
  int node = 0;
  bool flag = false;
  for (int i = 0; i < V; i++) {
    if (u[i] == true)
      continue;
    else {
      used[i] = true;
      u[i] = true;
      flag = dfs(-1, i, adjlist, used, u);
      used[i] = false;
      if (flag)
        break;
    }
  }

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

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

#include <algorithm>
#include <iostream>
#include <map>
#include <stdio.h>
#include <utility>
#include <vector>
bool p;
using namespace std;
vector<int> st[120];
int h[1010];
bool hu[1010];
void loop(int a) {
  h[a] = 1;
  for (int q = 0; q < st[a].size(); q++) {

    if (h[st[a][q]] == 1) {
      h[a] = 10;
    } else {
      if (hu[st[a][q]] == 0) {
        hu[st[a][q]] = 1;
        loop(st[a][q]);
      }
    }
  }
  h[a] = h[a] - 1;

  if (h[a] > 2) {
    p = true;
  }
}

int main() {
  p = false;
  int e, v;
  cin >> v >> e;
  for (int u = 0; u < v; u++) {
    h[u] = 0;
    hu[u] = 0;
  }
  for (int y = 0; y < e; y++) {
    int s, g;
    cin >> s >> g;
    st[s].push_back(g);
  }
  for (int n = 0; n < v; n++) {
    if (hu[n] == 0) {
      loop(n);
    }
  }
  if (p == true) {
    cout << "1" << endl;
  } else {
    cout << "0" << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <stdio.h>
#include <utility>
#include <vector>
bool p;
using namespace std;
vector<int> st[120];
int h[1010];
bool hu[1010];
void loop(int a) {
  h[a] = 1;
  for (int q = 0; q < st[a].size(); q++) {

    if (h[st[a][q]] == 1) {
      h[a] = 10;
    } else {
      if (hu[st[a][q]] == 0) {
        loop(st[a][q]);
      }
      hu[st[a][q]] = 1;
    }
  }
  h[a] = h[a] - 1;

  if (h[a] > 2) {
    p = true;
  }
}

int main() {
  p = false;
  int e, v;
  cin >> v >> e;
  for (int u = 0; u < v; u++) {
    h[u] = 0;
    hu[u] = 0;
  }
  for (int y = 0; y < e; y++) {
    int s, g;
    cin >> s >> g;
    st[s].push_back(g);
  }
  for (int n = 0; n < v; n++) {
    if (hu[n] == 0) {
      loop(n);
    }
  }
  if (p == true) {
    cout << "1" << endl;
  } else {
    cout << "0" << endl;
  }

  return 0;
}
Problem p02369 (C++) — buggy submission, verdict: 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[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;
}

Error: TLE

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