alexjercan/bugnet huggingface dataset:
Problem p00442 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

int N, M;
vector<int> paths[100010], lis;
bool isin[100010], used[100010];

void toposo(int now) {
  if (used[now])
    return;
  used[now] = true;
  for (vector<int>::iterator it = paths[now].begin(); it != paths[now].end();
       it++) {
    if (!used[*it])
      toposo(*it);
  }
  lis.push_back(now);
}

int main() {
  cin >> N >> M;
  int a, b;
  for (int i = 1; i <= M; i++) {
    cin >> a >> b;
    paths[a].push_back(b);
    isin[b] = true;
  }
  int cnt = 0;
  vector<int> aaa;
  for (int i = 1; i <= N; i++) {
    if (!isin[i]) {
      aaa.push_back(i);
    }
  }

  fill(used, used + 100010, false);
  for (vector<int>::iterator it = aaa.begin(); it != aaa.end(); it++) {
    cout << ""; // debug
    toposo(*it);
  }
  int res = 0;
  for (int i = N - 1; i > 0; i--) {
    bool hog = false;
    for (vector<int>::iterator it = paths[lis[i]].begin();
         it != paths[lis[i]].end(); it++) {
      if ((*it) == lis[i - 1])
        hog = true;
    }
    if (!hog)
      res = 1;
  }
  for (int i = N - 1; i >= 0; i--) {
    cout << lis[i] << endl;
  }
  cout << res << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <functional>
#include <iostream>
#include <queue>
#include <set>
#include <utility>
using namespace std;

vector<short> ve[5010];
set<short> se[5010];
pair<int, int> ary[5010] = {};
bool used[5010] = {};

void func(short a) {
  vector<short>::iterator it;
  set<short>::iterator is;
  used[a] = true;
  for (it = ve[a].begin(); it != ve[a].end(); it++) {
    if (!used[*it]) {
      func(*it);
    }
    se[a].insert(*it);
    for (is = se[*it].begin(); is != se[*it].end(); is++) {
      se[a].insert(*is);
    }
  }
}

int main() {
  bool fla;
  int M, N, a, b;
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    cin >> a >> b;
    ve[a].push_back(b);
  }
  for (int i = 1; i <= N; i++) {
    if (!used[i])
      func(i);
  }
  set<int>::iterator it;
  for (int i = 1; i <= N; i++) {
    ary[i] = make_pair(se[i].size(), i);
  }
  sort(ary + 1, ary + N + 1, greater<pair<int, int>>());
  fla = false;
  for (int i = 1; i < N; i++) {
    cout << ary[i].second << endl;
    if (ary[i].first - ary[i + 1].first != 1)
      fla = true;
  }
  cout << ary[N].second << endl;
  if (!fla)
    cout << 0 << endl;
  else
    cout << 1 << endl;
  return 0;
}
Problem p00442 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ll long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 100000005
#define MAX 5001
using namespace std;

queue<int> s;
stack<int> a[MAX];
bool check[MAX][MAX];
int b[MAX];
int main() {
  int n, m, t1, t2;
  scanf("%d %d", &n, &m);
  fill(b, b + MAX, 0);
  rep(i, MAX) rep(j, MAX) check[i][j] = false;
  // fill((int*)check, (int*)(check+MAX), false);
  for (int i = 0; i < m; i++) {
    scanf("%d %d", &t1, &t2);
    check[t1][t2] = 1;
    a[t1].push(t2);
    b[t2]++;
  }

  int ans[MAX], c = 0;
  for (int i = 1; i <= n; i++)
    if (!b[i])
      s.push(i);
  while (!s.empty()) {
    int t = s.front();
    ans[c++] = s.front();
    while (!a[t].empty()) {
      if (--b[a[t].top()] == 0)
        s.push(a[t].top());
      a[t].pop();
    }
    s.pop();
  }

  int f = 0;
  for (int i = 0; i < n - 1; i++) {
    if (check[ans[i]][ans[i + 1]] != 1)
      f = 1;
  }
  for (int i = 0; i < n; i++)
    printf("%d\\n", ans[i]);
  printf("%d\\n", f);
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 100000005
#define MAX 5001
using namespace std;

queue<int> s;
stack<int> a[MAX];
bool check[MAX][MAX];
int b[MAX];
int main() {
  int n, m, t1, t2;
  scanf("%d %d", &n, &m);
  fill(b, b + MAX, 0);
  fill((int *)check, (int *)(check + MAX), 0);
  for (int i = 0; i < m; i++) {
    scanf("%d %d", &t1, &t2);
    check[t1][t2] = 1;
    a[t1].push(t2);
    b[t2]++;
  }

  int ans[MAX], c = 0;
  for (int i = 1; i <= n; i++)
    if (!b[i])
      s.push(i);
  while (!s.empty()) {
    int t = s.front();
    ans[c++] = s.front();
    while (!a[t].empty()) {
      if (--b[a[t].top()] == 0)
        s.push(a[t].top());
      a[t].pop();
    }
    s.pop();
  }

  int f = 0;
  for (int i = 0; i < n - 1; i++) {
    if (check[ans[i]][ans[i + 1]] != 1)
      f = 1;
  }
  for (int i = 0; i < n; i++)
    printf("%d\\n", ans[i]);
  printf("%d\\n", f);
  return 0;
}
Problem p00442 (C++) — buggy submission, verdict: Runtime Error

#include <queue>
#include <stdio.h>
#include <vector>
using namespace std;
#define PB push_back
const int N = 5e3 + 10;
int main() {
  int n, m, l, r, d[N], nxt;
  vector<int> graph[N];
  queue<int> q;
  bool y = false;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    d[i] = 0;
  while (m--) {
    scanf("%d%d", &l, &r);
    graph[l].PB(r);
    d[r]++;
  }
  for (int i = 1; i <= n; i++)
    if (d[i] == 0)
      q.push(i);
  while (!q.empty()) {
    nxt = q.front();
    q.pop();
    printf("%d\\n", nxt);
    y |= !q.empty();
    for (int i : graph[nxt]) {
      d[i]--;
      if (d[i] == 0)
        q.push(i);
    }
  }
  printf("%d\\n", y ? 1 : 0);
}


Error: 0

Fixed submission:

#include <queue>
#include <stdio.h>
#include <vector>
using namespace std;
#define PB push_back
const int N = 3e3 + 10;
int main() {
  int n, m, l, r, d[N], nxt;
  vector<int> graph[N];
  queue<int> q;
  bool y = false;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    d[i] = 0;
  while (m--) {
    scanf("%d%d", &l, &r);
    graph[l].PB(r);
    d[r]++;
  }
  for (int i = 1; i <= n; i++)
    if (d[i] == 0)
      q.push(i);
  while (!q.empty()) {
    nxt = q.front();
    q.pop();
    printf("%d\\n", nxt);
    y |= !q.empty();
    for (int i : graph[nxt]) {
      d[i]--;
      if (d[i] == 0)
        q.push(i);
    }
  }
  printf("%d\\n", y ? 1 : 0);
}
Problem p00442 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <cstring>
#include <vector>

using namespace std;

bool other;
bool vis[5000];
vector<int> win[5000], lose[5000], ord;
int depth[5000];

void dfs(int v) {
  vis[v] = true;
  for (int i = 0; i < win[v].size(); i++) {
    if (!vis[win[v][i]])
      dfs(win[v][i]);
  }
  ord.push_back(v);
}

int main() {
  int n, m;

  scanf("%d %d", &n, &m);

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

  int cnt = 0;
  memset(depth, 0, sizeof(depth));
  for (int i = 0; i < n; i++) {
    if (lose[i].size() == 0)
      dfs(i);
  }

  for (int i = n - 1; i >= 0; i--) {
    printf("%d\\n", ord[i] + 1);
    if (i) {
      bool ok = true;
      for (int j = 0; j < win[ord[i]].size(); j++)
        if (win[ord[i]][j] == ord[i - 1])
          ok = false;
      other |= ok;
    }
  }
  printf("%d\\n", other);

  return (0);
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <cstring>
#include <vector>

using namespace std;

bool other;
bool vis[5000];
vector<int> win[5000], lose[5000], ord;
int depth[5000];

void dfs(int v, int d) {
  depth[v] = max(d, depth[v]);
  for (int i = 0; i < win[v].size(); i++) {
    dfs(win[v][i], d + 1);
  }
  if (!vis[v]) {
    ord.push_back(v);
    vis[v] = true;
  }
}

int main() {
  int n, m;

  scanf("%d %d", &n, &m);

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

  int cnt = 0;
  memset(depth, 0, sizeof(depth));
  for (int i = 0; i < n; i++) {
    if (lose[i].size() == 0) {
      dfs(i, 0);
    }
  }

  for (int i = n - 1; i >= 0; i--) {
    printf("%d\\n", ord[i] + 1);
    if (depth[ord[i]] != n - 1 - i)
      other = true;
  }
  printf("%d\\n", other);

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

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

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)

typedef pair<int, int> pii;

int n, m;
vector<pii> edge;
int cnt[5000];
bool done[5000], used[100000];

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

  REP(i, m) {
    int a, b;
    scanf("%d %d", &a, &b);
    a--;
    b--;
    edge.push_back(pii(a, b));
  }

  vector<int> ans;
  bool flag = false;
  vector<int> next;

  REP(i, m) cnt[edge[i].second]++;
  REP(i, n) if (cnt[i] == 0) next.push_back(i);

  REP(i, n) {
    int tar = next[next.size() - 1];
    ans.push_back(tar + 1);
    done[tar] = true;
    next.erase(next.end() - 1);

    if (next.size() > 0)
      flag = true;

    REP(j, edge.size()) {
      if (!used[j] && edge[j].first == tar) {
        int to = edge[j].second;
        cnt[to]--;
        if (cnt[to] == 0 && !done[to])
          next.push_back(to);

        used[j] = true;
      }
    }
  }

  REP(i, ans.size()) printf("%d\\n", ans[i]);
  if (flag)
    puts("1");
  else
    puts("0");

  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)

typedef pair<int, int> pii;

int n, m;
vector<pii> edge;
int cnt[5000];
bool done[5000];

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

  REP(i, m) {
    int a, b;
    scanf("%d %d", &a, &b);
    a--;
    b--;
    edge.push_back(pii(a, b));
  }

  vector<int> ans;
  bool flag = false;
  vector<int> next;

  REP(i, m) cnt[edge[i].second]++;
  REP(i, n) if (cnt[i] == 0) next.push_back(i);

  REP(i, n) {
    int tar = next[next.size() - 1];
    ans.push_back(tar + 1);
    done[tar] = true;
    next.erase(next.end() - 1);

    if (next.size() > 0)
      flag = true;

    REP(j, edge.size()) {
      if (edge[j].first == tar) {
        int to = edge[j].second;
        cnt[to]--;
        if (cnt[to] == 0 && !done[to])
          next.push_back(to);

        edge.erase(edge.begin() + j);
        j--;
      }
    }
  }

  REP(i, ans.size()) printf("%d\\n", ans[i]);
  if (flag)
    puts("1");
  else
    puts("0");

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

#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int n, m;
queue<int> Q;
vector<int> gin[5001];
vector<int> gout[5001];
vector<int> ans;
int sta[5001];
int flg = 0;

void bfs() {

  int f = 0;
  for (int i = 1; i <= n; i++) {
    if (gin[i].size() == 0)
      Q.push(i), f++;
  }

  if (f >= 2)
    flg = 1;

  while (!Q.empty()) {
    int t = Q.front();
    Q.pop();
    ans.push_back(t);

    f = 0;
    for (int i = 0; i < gout[t].size(); i++) {
      int nx = gout[t][i];
      for (int j = 0; j < gin[nx].size(); j++) {
        if (gin[nx][j] == t)
          gin[nx].erase(gin[nx].begin() + j);
      }
      if (gin[nx].size() == 0) {
        Q.push(nx);
        f++;
      }
    }
    if (f >= 2)
      flg = 1;
  }
}

int main() {

  cin >> n >> m;

  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    gout[a].push_back(b);
    gin[b].push_back(a);
  }

  bfs();

  for (int i = 0; i < ans.size(); i++)
    cout << ans[i] << endl;
  if (flg == 1)
    cout << 1 << endl;
  else
    cout << 0 << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
struct po {
  int num, site;
};

int m, n;
po a[5001], b[5001];
vector<int> rel[5001];

void check(int e) {
  for (int i = 1; i <= e; i++) {

    for (int j = 0; j < rel[i].size(); j++) {
      int low = rel[i][j];
      if (a[i].site > a[low].site) {
        int t = a[i].site;
        a[i].site = a[low].site;
        a[low].site = t;
        check(i);
      }
    }
  }
}

int main() {

  cin >> n;
  cin >> m;

  for (int i = 0; i <= n; i++)
    a[i].num = a[i].site = i;

  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    rel[x].push_back(y);
  }

  for (int i = 1; i <= n; i++)
    sort(rel[i].begin(), rel[i].end());

  /*  for(int i=1;i<=n;i++){
    cout << i <<" > ";
    for(int j=0;j<rel[i].size();j++)
      cout << rel[i][j] <<" ";
    cout << endl;
    } */

  check(n);
  for (int i = 0; i <= n; i++) {
    b[i] = a[i];
    a[i].num = i;
    a[i].site = n - i + 1;
  }
  check(n);

  int flg = 0;
  for (int i = 1; i <= n; i++)
    if (a[i].site != b[i].site) {
      flg = 1;
      break;
    }

  // for(int i=1;i<=n;i++)cout <<"num="<< a[i].num <<" site="<<a[i].site <<
  // endl;

  for (int i = 1; i < n; i++)
    for (int j = n; j > i; j--)
      if (a[j].site < a[j - 1].site)
        swap(a[j], a[j - 1]);

  for (int i = 1; i <= n; i++)
    cout << a[i].num << endl;
  if (flg == 0)
    cout << 0 << endl;
  else
    cout << 1 << endl;

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

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef pair<int, int> P;
int lose[5005] = {0};
bool match[5005][5005];
int main() {
  int n;
  cin >> n;
  int m;
  cin >> m;
  int tf = 0;
  int no_1 = -1;
  for (int i = 0; i < 5005; i++) {
    for (int j = 0; j < 5005; j++)
      match[i][j] = false;
  }
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    lose[b]++;
    match[b][a] = true;
  }
  for (int i = 1; i <= n; i++) {
    if (lose[i] == 0) {
      if (no_1 != -1)
        tf = 1;
      no_1 = i;
    }
  }
  int ans[5005] = {0};
  bool used[5005];
  for (int i = 0; i < 5005; i++)
    used[i] = false;
  for (int i = 1; i <= n; i++) {
    ans[i] = no_1;
    used[no_1] = true;
    for (int j = 1; j <= n; j++) {
      if (match[j][no_1])
        lose[j]--;
    }
    no_1 = -1;
    for (int j = 1; j <= n; j++) {
      if (lose[j] == 0 && !used[j]) {
        if (no_1 != -1)
          tf = 1;
        no_1 = j;
      }
    }
  }
  for (int i = 1; i <= n; i++)
    cout << ans[i] << endl;
  cout << tf << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef pair<int, int> P;
int lose[1005] = {0};
bool match[5005][5005];
int main() {
  int n;
  cin >> n;
  int m;
  cin >> m;
  int tf = 0;
  int no_1 = -1;
  for (int i = 0; i < 5005; i++) {
    for (int j = 0; j < 5005; j++)
      match[i][j] = false;
  }
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    lose[b]++;
    match[b][a] = true;
  }
  for (int i = 1; i <= n; i++) {
    if (lose[i] == 0) {
      if (no_1 != -1)
        tf = 1;
      no_1 = i;
    }
  }
  int ans[5005] = {0};
  bool used[5005];
  for (int i = 0; i < 5005; i++)
    used[i] = false;
  for (int i = 1; i <= n; i++) {
    ans[i] = no_1;
    used[no_1] = true;
    for (int j = 1; j <= n; j++) {
      if (match[j][no_1])
        lose[j]--;
    }
    no_1 = -1;
    for (int j = 1; j <= n; j++) {
      if (lose[j] == 0 && !used[j]) {
        if (no_1 != -1)
          tf = 1;
        no_1 = j;
      }
    }
  }
  for (int i = 1; i <= n; i++)
    cout << ans[i] << endl;
  cout << tf << endl;
  return 0;
}
Problem p00442 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
int N, M;
int W[100000];
int L[100000];
bool board[5000][5000];
int WC[5000];
int LC[5000];
bool F[5000];
int main() {
  scanf("%d%d", &N, &M);
  for (int i = 0; i < M; i++) {
    scanf("%d%d", &W[i], &L[i]);
    W[i]--;
    L[i]--;
    board[W[i]][L[i]] = true;
    WC[W[i]]++;
    LC[L[i]]++;
  }
  bool ans = true;
  for (int i = 0; i < N; i++) {
    int now = -1;
    for (int j = 0; j < N; j++) {
      if (F[j])
        continue;
      if (LC[j] == 0) {
        if (now != -1)
          ans = false;
        else
          now = j;
      }
    }
    printf("%d\\n", now + 1);
    F[now] = true;
    for (int j = 0; j < N; j++) {
      if (board[now][j]) {
        LC[j]--;
      }
    }
  }
  if (ans)
    printf("0\\n");
  else
    printf("1\\n");
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
int N, M;
int W[100000];
int L[100000];
int board[5000][5000];
int WC[5000];
int LC[5000];
bool F[5000];
int main() {
  scanf("%d%d", &N, &M);
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      board[i][j] = -1;
    }
  }
  for (int i = 0; i < M; i++) {
    scanf("%d%d", &W[i], &L[i]);
    W[i]--;
    L[i]--;
    board[W[i]][L[i]] = 1;
    board[L[i]][W[i]] = 0;
    WC[W[i]]++;
    LC[L[i]]++;
  }
  bool ans = true;
  for (int i = 0; i < N; i++) {
    int now = -1;
    for (int j = 0; j < N; j++) {
      if (F[j])
        continue;
      if (LC[j] == 0) {
        if (now != -1)
          ans = false;
        else
          now = j;
      }
    }
    printf("%d\\n", now + 1);
    F[now] = true;
    for (int j = 0; j < N; j++) {
      if (board[now][j] == 1) {
        LC[j]--;
      }
    }
  }
  if (ans)
    printf("0\\n");
  else
    printf("1\\n");
  return 0;
}
Problem p00442 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>

using namespace std;

int vis[5005];
vector<int> G[5005];
int N, M;
vector<int> res;

void func(int v) {
  if (vis[v])
    return;
  vis[v] = 1;
  for (int i = 0; i < G[v].size(); i++)
    func(G[v][i]);
  res.push_back(v);
  return;
}

int main() {
  scanf("%d%d", &N, &M);
  for (int i = 0; i < M; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    a--;
    b--;
    G[b].push_back(a);
  }

  for (int i = 0; i < N; i++)
    func(i);
  int f = 0;
  for (int i = 1; i < N; i++)
    if (find(G[res[i]].begin(), G[res[i]].end(), res[i - 1]) == G[res[i]].end())
      f = 1;

  for (int i = 0; i < N; i++)
    printf("%d\\n", res[i] + 1);
  printf("%d\\n", f);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>

using namespace std;

int perm[32];
int w[32][32];
int N, M;
int cnt;

void solve() {
  //  for(int i=0;i<N;i++) printf("%d ", perm[i]+1); puts("");
  for (int i = 0; i < N; i++) {
    for (int j = i + 1; j < N; j++) {
      if (w[perm[j]][perm[i]])
        return;
    }
  }
  if (cnt == 0) {
    for (int i = 0; i < N; i++)
      printf("%d\\n", perm[i] + 1);
  }
  cnt++;
  return;
}

void dfs(int n, int bit) {
  if (n == N) {
    solve();
    return;
  }

  for (int i = 0; i < N; i++) {
    if ((bit >> i) & 1)
      continue;
    perm[n] = i;
    dfs(n + 1, bit | (1 << i));
  }
  return;
}

int main() {
  scanf("%d%d", &N, &M);
  cnt = 0;
  for (int i = 0; i < M; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    a--;
    b--;
    w[a][b] = 1;
  }
  dfs(0, 0);
  printf("%d\\n", cnt > 1 ? 1 : 0);
  return 0;
}
Problem p00442 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>

#define MAX_N 5000

using namespace std;

int N;
int E;
vector<int> in[MAX_N + 1];
vector<int> out[MAX_N + 1];
queue<int> S;
queue<int> L;

bool multi_flag;

bool in_exist(int v) {
  bool flag = false;
  if (in[v].empty())
    return false;
  for (int i = 0; i < in[v].size(); i++) {
    if (in[v][i] != -1)
      return true;
  }
  return false;
}

void solve() {
  for (int i = 1; i <= N; i++) {
    if (in_exist(i) == false)
      S.push(i);
  }

  while (!S.empty()) {
    int u = S.front();
    S.pop();
    L.push(u);
    int entered = 0;
    for (int i = 0; i < out[u].size(); i++) {
      int to = out[u][i];
      for (int i = 0; i < in[to].size(); i++) {
        if (in[to][i] == u)
          in[to][i] = -1;
      }

      if (in_exist(to) == false) {
        S.push(to);
        entered++;
      }
    }
    if (entered >= 2)
      multi_flag = true;
  }

  return;
}

int main() {

  scanf("%d", &N);
  scanf("%d", &E);
  // for(int i = 0;i <= MAX_N;i++){
  //	for(int j = 0;j <= MAX_N;j++){
  //		edge[i][j] = false;
  //	}
  // }
  int _u, _v;
  for (int i = 0; i < E; i++) {
    scanf("%d%d", &_u, &_v);
    in[_v].push_back(_u);
    out[_u].push_back(_v);
  }

  solve();

  while (!L.empty()) {
    int team = L.front();
    L.pop();
    printf("%d\\n", team);
  }
  if (multi_flag)
    puts("1");
  else
    puts("0");

  return 0;
}

Error: TLE

Fixed submission:

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

#define MAX_V 5000

using namespace std;

vector<int> G[MAX_V + 1];
bool visited[MAX_V + 1];
int ans[MAX_V];

stack<int> L;

int V, E;
bool single;

void init() {
  for (int i = 0; i <= MAX_V; i++)
    visited[i] = false;
  return;
}

void count(int to, int cnt) {
  // printf("###to = %d:cnt = %d\\n",to,cnt);
  if (cnt == V)
    single = true;
  for (int i = 0; i < G[to].size(); i++) {
    count(G[to][i], cnt + 1);
  }
  return;
}

void visit(int to) {
  if (!visited[to]) {
    visited[to] = true;
    for (int i = 0; i < G[to].size(); i++) {
      int _to = G[to][i];
      visit(_to);
    }
    L.push(to);
  }
  return;
}

void solve() {
  for (int i = 1; i <= V; i++) {
    visit(i);
  }
  for (int i = 0; i < V; i++) {
    int v = L.top();
    L.pop();
    ans[i] = v;
  }
  single = false;
  count(ans[0], 1);
  return;
}

int main() {

  scanf("%d%d", &V, &E);
  int _u, _v;
  for (int i = 0; i < E; i++) {
    scanf("%d%d", &_u, &_v);
    G[_u].push_back(_v);
  }

  solve();

  for (int i = 0; i < V; i++) {
    printf("%d\\n", ans[i]);
  }
  if (single)
    puts("0");
  else
    puts("1");

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

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

#define MAX_V 5000

using namespace std;

vector<int> G[MAX_V + 1];
bool visited[MAX_V + 1];

stack<int> L;
int ans[MAX_V];
int num;

int V, E;
bool single;

void init() {
  for (int i = 0; i <= MAX_V; i++)
    visited[i] = false;
  return;
}

void count(int to, int cnt) {
  // printf("###to = %d:cnt = %d\\n",to,cnt);
  if (cnt == V - 1)
    single = true;
  for (int i = 0; i < G[to].size(); i++) {
    if (G[to][i] == ans[cnt + 1])
      count(G[to][i], cnt + 1);
  }
  return;
}

void visit(int to) {
  // printf("###visited %d\\n",to);
  if (!visited[to]) {
    visited[to] = true;
    for (int i = 0; i < G[to].size(); i++) {
      int _to = G[to][i];
      visit(_to);
    }
    L.push(to);
    num++;
  }
  return;
}

void solve() {
  init();
  for (int i = 1; i <= V; i++) {
    visit(i);
  }
  single = false;
  for (int i = 0; i < V; i++) {
    ans[i] = L.top();
    L.pop();
  }
  count(ans[0], 0);
  return;
}

int main() {

  scanf("%d%d", &V, &E);
  int _u, _v;
  for (int i = 0; i < E; i++) {
    scanf("%d%d", &_u, &_v);
    G[_u].push_back(_v);
  }

  solve();

  for (int i = 0; i < V; i++) {
    printf("%d\\n", ans[i]);
  }
  if (single)
    puts("0");
  else
    puts("1");

  return 0;
}

Error: TLE

Fixed submission:

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

#define MAX_V 5000

using namespace std;

vector<int> G[MAX_V + 1];
bool visited[MAX_V + 1];

// stack<int > L;
int L[MAX_V];
int num;

int V, E;
bool single;

void init() {
  for (int i = 0; i <= MAX_V; i++)
    visited[i] = false;
  return;
}

void count(int to, int cnt) {
  // printf("###to = %d:cnt = %d\\n",to,cnt);
  if (cnt == V)
    single = true;
  for (int i = 0; i < G[to].size(); i++) {
    count(G[to][i], cnt + 1);
  }
  return;
}

void visit(int to) {
  // printf("###visited %d\\n",to);
  if (!visited[to]) {
    visited[to] = true;
    for (int i = 0; i < G[to].size(); i++) {
      int _to = G[to][i];
      visit(_to);
    }
    // L.push(to);
    L[num] = to;
    num++;
  }
  return;
}

void solve() {
  init();
  for (int i = 1; i <= V; i++) {
    visit(i);
  }
  single = false;
  // count(L.top(),1);
  count(L[V - 1], 1);
  return;
}

int main() {

  scanf("%d%d", &V, &E);
  int _u, _v;
  for (int i = 0; i < E; i++) {
    scanf("%d%d", &_u, &_v);
    G[_u].push_back(_v);
  }

  solve();

  /*for(int i = 0;i < V;i++){
          int v = L.top();L.pop();
          printf("%d\\n",v);
  }*/
  for (int i = V - 1; i >= 0; i--) {
    printf("%d\\n", L[i]);
  }
  if (single)
    puts("0");
  else
    puts("1");

  return 0;
}