alexjercan/bugnet huggingface dataset:
Problem p02369 (C++) — buggy submission, verdict: 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;
  } 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");
}


Error: TLE

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;
  }
  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, verdict: 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 (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;
}


Error: TLE

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 (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, verdict: 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 (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;
}

Error: TLE

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 (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, verdict: Time Limit Exceeded

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

int n, e, s, t, ans = 0;
bool visited[101] = {};
bool checked[101] = {};
vector<int> v[101];

void dfs(int num) {
  if (visited[num]) {
    ans = 1;
    return;
  }
  visited[num] = true;
  for (int i = 0; i < v[num].size(); i++) {
    if (!checked[v[num][i]])
      dfs(v[num][i]);
  }
  checked[num] = true;
  visited[num] = false;
}

int main() {
  cin >> n >> e;
  for (int i = 0; i < e; i++) {
    cin >> s >> t;
    v[s].push_back(t);
  }
  for (int i = 0; i < n; i++) {
    if (!checked[i])
      dfs(i);
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int n, e, s, t, ans = 0;
bool visited[101] = {};
vector<int> v[101];

void dfs(int num) {
  if (visited[num]) {
    ans = 1;
    return;
  }
  visited[num] = true;
  for (int i = 0; i < v[num].size(); i++) {
    dfs(v[num][i]);
  }
  visited[num] = false;
}

int main() {
  cin >> n >> e;
  for (int i = 0; i < e; i++) {
    cin >> s >> t;
    v[s].push_back(t);
  }
  for (int i = 0; i < n; i++)
    dfs(i);
  cout << ans << endl;
  return 0;
}
Problem p02369 (C++) — buggy submission, verdict: 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;
    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;
}

Error: 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;
    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, verdict: Time Limit Exceeded

#include <algorithm>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <sys/time.h>
#include <vector>

using namespace std;

typedef long long ll;

// ????????°????????§??°
#define MAX_V 100000

int V;
vector<int> graph[MAX_V];
bool vis[MAX_V];

bool cycledetectDirected() {
  for (int i = 0; i < V; i++) {
    memset(vis, false, sizeof(vis));

    stack<int> st;
    st.push(i);
    while (!st.empty()) {
      int p = st.top();
      st.pop();
      vis[p] = true;

      for (int j = 0; j < graph[p].size(); j++) {
        int np = graph[p][j];
        if (np == i)
          return true;
        if (vis[np])
          continue;

        st.push(np);
      }
    }
  }

  return false;
}

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

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <sys/time.h>
#include <vector>

using namespace std;

typedef long long ll;

// ????????°????????§??°
#define MAX_V 100000

int V;
vector<int> graph[MAX_V];
bool vis[MAX_V];

bool cycledetectDirected() {
  for (int i = 0; i < V; i++) {
    memset(vis, false, sizeof(vis));

    stack<int> st;
    st.push(i);
    while (!st.empty()) {
      int p = st.top();
      st.pop();
      vis[p] = true;

      for (int i = 0; i < graph[p].size(); i++) {
        int np = graph[p][i];
        if (np == i)
          return true;

        st.push(np);
      }
    }
  }

  return false;
}

int main() {
  int E;
  cin >> V >> E;
  for (int i = 0; i < E; i++) {
    int s, t;
    cin >> s >> t;
    graph[s].push_back(t);
  }
  cout << cycledetectDirected() << endl;
}
Problem p02369 (C++) — buggy submission, verdict: Runtime Error

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

#define rep(i, x, y) for (int i = (x); i < (y); ++i)
#define mp(a, b) make_pair((a), (b))
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define dump(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define dump(x)
#endif

typedef long long int ll;
typedef pair<int, int> pii;
// template<typename T> using vec=std::vector<T>;

const int INF = 1 << 30;
const long long int INFLL = 1LL << 58;
const double EPS = 1e-9;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

vector<int> graph[100];
bool used[100];

bool dfs(int v, int p, int s) {
  if (p != -1 and v == s)
    return true;
  if (used[v])
    return false;
  used[v] = true;
  for (int u : graph[v]) {
    if (u == p)
      continue;
    if (dfs(u, v, s))
      return true;
  }
  return false;
}

void solve() {
  int v, e;
  cin >> v >> e;
  rep(i, 0, e) {
    int s, t;
    cin >> s >> t;
    graph[s].push_back(t);
  }

  rep(i, 0, v) {
    fill_n((bool *)used, 100, false);
    if (dfs(i, -1, i)) {
      cout << 1 << endl;
      return;
    }
  }

  cout << 0 << endl;
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  solve();
  return 0;
}

Error: 0

Fixed submission:

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

#define rep(i, x, y) for (int i = (x); i < (y); ++i)
#define mp(a, b) make_pair((a), (b))
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define dump(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define dump(x)
#endif

typedef long long int ll;
typedef pair<int, int> pii;
// template<typename T> using vec=std::vector<T>;

const int INF = 1 << 30;
const long long int INFLL = 1LL << 58;
const double EPS = 1e-9;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

vector<int> graph[100];

bool dfs(int v, int p) {
  if (p != -1 and v == p)
    return true;
  for (int u : graph[v]) {
    if (u == p)
      continue;
    if (dfs(u, v))
      return true;
  }
  return false;
}

void solve() {
  int v, e;
  cin >> v >> e;
  rep(i, 0, e) {
    int s, t;
    cin >> s >> t;
    graph[s].push_back(t);
  }

  rep(i, 0, v) {
    if (dfs(i, -1)) {
      cout << 1 << endl;
      return;
    }
  }

  cout << 0 << endl;
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  solve();
  return 0;
}
Problem p02369 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define DUMP(x) cerr << #x << "=" << x << endl
#define DUMP2(x, y)                                                            \\
  cerr << "(" << #x << ", " << #y << ") = (" << x << ", " << y << ")" << endl
#define BINARY(x) static_cast<bitset<16>>(x)

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, m, n) for (int i = m; i < (int)(n); i++)

#define in_range(x, y, w, h)                                                   \\
  (0 <= (int)(x) && (int)(x) < (int)(w) && 0 <= (int)(y) && (int)(y) < (int)(h))
#define ALL(a) (a).begin(), (a).end()

typedef long long ll;
const int INF = 1e9;
typedef pair<int, int> PII;
int dx[4] = {0, -1, 1, 0}, dy[4] = {-1, 0, 0, 1};

int V, E;
vector<vector<int>> G;
vector<int> in_deg;
vector<bool> done;

bool solve() {
  priority_queue<int, vector<int>, greater<int>> Q;

  rep(i, V) {
    if (in_deg[i] == 0)
      Q.push(i);
  }

  while (Q.size()) {
    int v = Q.top();
    Q.pop();
    done[v] = true;

    for (int u : G[v]) {
      if (--in_deg[u] == 0)
        Q.push(u);
    }
  }

  rep(i, V) {
    if (in_deg[i] > 0)
      return true;
  }

  return false;
}

int main() {
  cin >> V >> E;
  G.resize(V);
  in_deg.resize(V);
  done.resize(V);

  rep(i, E) {
    int S, T;
    cin >> S >> T;
    G[S].push_back(T);
    in_deg[T]++;
  }

  cout << solve() << endl;
}

Error: TLE

Fixed submission:

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

#define DUMP(x) cerr << #x << "=" << x << endl
#define DUMP2(x, y)                                                            \\
  cerr << "(" << #x << ", " << #y << ") = (" << x << ", " << y << ")" << endl
#define BINARY(x) static_cast<bitset<16>>(x)

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, m, n) for (int i = m; i < (int)(n); i++)

#define in_range(x, y, w, h)                                                   \\
  (0 <= (int)(x) && (int)(x) < (int)(w) && 0 <= (int)(y) && (int)(y) < (int)(h))
#define ALL(a) (a).begin(), (a).end()

typedef long long ll;
const int INF = 1e9;
typedef pair<int, int> PII;
int dx[4] = {0, -1, 1, 0}, dy[4] = {-1, 0, 0, 1};

int V, E;
vector<vector<int>> G;
vector<bool> vis;

bool solve(int u, int prev) {
  if (vis[u] == true)
    return true;
  vis[u] = true;

  bool res = false;
  for (auto v : G[u]) {
    if (v == prev)
      continue;
    res |= solve(v, u);
  }

  vis[u] = false;
  return res;
}

int main() {
  cin >> V >> E;
  G.resize(V);
  vis.resize(V);

  rep(i, E) {
    int S, T;
    cin >> S >> T;
    G[S].push_back(T);
  }

  bool ok = false;
  rep(i, V) {
    fill(vis.begin(), vis.end(), false);
    ok |= solve(i, -1);
  }
  cout << ok << endl;
}
Problem p02369 (C++) — buggy submission, verdict: 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 >= 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;
}

Error: MLE

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

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

struct Scc {
  const int V;
  vector<vector<int>> G, revG;
  vector<int> order;

  vector<int> vs;
  vector<bool> used;

  Scc(int v) : V(v), G(v), revG(v), order(v), used(v) {}

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

  void dfs(int v) {
    used[v] = true;
    for (int to : G[v]) {
      if (used[to] == false)
        dfs(to);
    }
    vs.push_back(v);
  }

  void rdfs(int v, int k) {
    used[v] = true;
    order[v] = k;
    for (int to : revG[v]) {
      if (used[to] == false)
        rdfs(to, k);
    }
  }

  int compute() {
    vs.clear();
    used.assign(V, false);
    for (int i = 0; i < V; i++) {
      if (used[i] == false)
        dfs(i);
    }

    int k = 0;
    reverse(vs.begin(), vs.end());
    used.assign(V, false);

    for (int v : vs) {
      if (used[v] == false)
        rdfs(v, k++);
    }

    return k;
  }
};

int main() {
  int V, E;
  cin >> V >> E;
  Scc g(V);
  for (int i = 0; i < E; i++) {
    int f, t;
    cin >> f >> t;
    g.add_edge(f, t);
  }
  int v = g.compute();
  cout << (v != V ? 1 : 0) << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

const int MAX_V = 100;
vector<vector<int>> G;
bool vis[MAX_V];
bool vis2[MAX_V];
bool ans;

void dfs(int v, int parent) {
  vis2[v] = true;
  if (vis[v] == true) {
    ans = true;
    return;
  }
  vis[v] = true;
  for (int i = 0; i < (int)G[v].size(); i++) {
    if (G[v][i] == parent)
      continue;
    dfs(G[v][i], v);
    if (ans)
      return;
  }
  vis[v] = false;
}

int main() {
  int V, E;
  cin >> V >> E;
  G.assign(V, vector<int>());
  for (int i = 0; i < E; i++) {
    int s, t;
    cin >> s >> t;
    G[s].push_back(t);
  }
  fill(&vis[0], &vis[V], false);
  fill(&vis2[0], &vis2[V], false);
  ans = false;
  for (int i = 0; i < V; i++) {
    if (vis2[i] == false)
      dfs(i, -1);
  }
  cout << (ans ? 1 : 0) << endl;
}
Problem p02369 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
struct edge {
  int from, to;
};
int V, E;
vector<int> e[100];
int used[100];
int checked[100];
int dfs(int v) {
  if (used[v] == 1)
    return 1;
  used[v] = 1;
  for (int j = 0; j < e[v].size(); j++) {
    int u = e[v][j];
    if (!checked[u])
      if (dfs(u))
        return 1;
  }
  used[v] = 0;
  checked[v] = 1;
  return 0;
}
int main() {
  scanf("%d %d", &V, &E);
  for (int i = 0; i < E; i++) {
    int x, y;
    scanf("%d %d", &x, &y);
    e[x].push_back(y);
  }
  int f;
  for (int i = 0; i < V; i++) {
    f = 0;
    memset(used, 0, sizeof(used));
    if (!checked[i]) {
      f = dfs(i);
    }
    if (f)
      break;
  }
  printf("%d\\n", f);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
struct edge {
  int from, to;
};
int V, E;
vector<int> e[100];
int used[100];
int checked[100];
int dfs(int v) {
  if (used[v] == 1)
    return 1;
  used[v] = 1;
  checked[v] = 1;
  for (int j = 0; j < e[v].size(); j++) {
    int u = e[v][j];
    if (dfs(u))
      return 1;
  }
  used[v] = 0;
  return 0;
}
int main() {
  scanf("%d %d", &V, &E);
  for (int i = 0; i < E; i++) {
    int x, y;
    scanf("%d %d", &x, &y);
    e[x].push_back(y);
  }
  int f;
  for (int i = 0; i < V; i++) {
    f = 0;
    memset(used, 0, sizeof(used));
    if (!checked[i]) {
      f = dfs(i);
    }
    if (f)
      break;
  }
  printf("%d\\n", f);
  return 0;
}
Problem p02369 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>
#include <vector>

using namespace std;

vector<int> M[10000];
vector<int> m[10000];
int mark[10000] = {0};

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

  int j;
  for (int i = 0; i < V; i++) {
    for (j = 0; j < V; j++) {
      if (m[j].size() == 0 && mark[j] != 1) {
        mark[j] = 1;
        for (int k = 0; k < M[j].size(); k++) {
          for (int l = 0; l < m[M[j][k]].size(); l++) {
            if (m[M[j][k]][l] == j) {
              m[M[j][k]].erase(m[M[j][k]].begin() + l);
            }
          }
        }
        break;
      }
    }
    if (j == V) {
      cout << "1" << endl;
      return 0;
    }
  }
  cout << "0" << endl;
  return 0;
}

Error: MLE

Fixed submission:

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

using namespace std;

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

  for (int i = 0; i < V; i++) {
    Q.push(i);
  }
  int F;
  for (int i = 0; i < 10000000 && Q.size() != 0; i++) {
    F = Q.front();
    Q.pop();
    for (int j = 0; j < map[F].size(); j++) {
      Q.push(map[F][j]);
    }
  }
  if (Q.size() != 0) {
    cout << "1" << endl;
  } else {
    cout << "0" << endl;
  }
  return 0;
}
Problem p02369 (C++) — buggy submission, verdict: 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 < n; i += 5)
    dfs(i);
  if (Cyc)
    cout << 1 << endl;
  else
    cout << 0 << endl;
}

Error: TLE

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 < min(50, n); i++)
    dfs(i);
  if (Cyc)
    cout << 1 << endl;
  else
    cout << 0 << endl;
}