Problem p02370 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfenv>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
// #include <bits/stdc++.h>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
long long MOD = 1000000000 + 7; // 998244353;// ;
constexpr long long INF = numeric_limits<LL>::max() / 2;
const double PI = acos(-1);
#define fir first
#define sec second
#define thi third
#define debug(x) cerr << #x << ": " << x << '\\n'
typedef pair<LL, LL> Pll;
typedef pair<LL, pair<LL, LL>> Ppll;
typedef pair<LL, pair<LL, bitset<100001>>> Pbll;
typedef pair<LL, pair<LL, vector<LL>>> Pvll;
typedef pair<LL, LL> Vec2;
struct Tll {
  LL first, second, third;
};
struct Fll {
  LL first, second, third, fourd;
};
typedef pair<LL, Tll> Ptll;
#define rep(i, rept) for (LL i = 0; i < rept; i++)
#define Rrep(i, mf) for (LL i = mf - 1; i >= 0; i--)
void YN(bool f) {
  if (f)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}
void yn(bool f) {
  if (f)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}
struct Edge {
  LL to, cost;
};
struct edge {
  LL from, to, cost;
};
vector<vector<LL>> g;
vector<edge> edges;
vector<LL> v;
map<LL, LL> ma;
set<Pll> st;
LL h, w, n, m, k, t, s, p, q, last, cnt, sum, ans, dp[210000], a[210000],
    b[210000];
string str, ss;
bool f;
char c;
vector<LL> tsort(vector<vector<LL>> &g) {
  LL n = g.size();
  vector<LL> ret;
  vector<bool> used(n);
  function<void(LL)> dfs = [&](LL cur) {
    used[cur] = 1;
    rep(i, g[cur].size()) {
      if (!used[g[cur][i]])
        dfs(g[cur][i]);
    }
    ret.push_back(cur);
  };
  rep(i, n) {
    if (!used[i])
      dfs(i);
  }
  reverse(ret.begin(), ret.end());
  return ret;
}
int main() {
  cin >> n >> m;
  g.resize(n);
  rep(i, m) {
    LL x, y;
    cin >> x >> y;
    g[x].push_back(y);
  }
  vector<LL> ts = tsort(g);
  rep(i, n) { cout << ts[i] << endl; }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfenv>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
// #include <bits/stdc++.h>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
long long MOD = 1000000000 + 7;
constexpr long long INF = std::numeric_limits<LL>::max();
const double PI = acos(-1);
#define fir first
#define sec second
#define thi third
#define debug(x) cerr << #x << ": " << x << '\\n'
typedef pair<LL, LL> Pll;
typedef pair<LL, pair<LL, LL>> Ppll;
typedef pair<LL, pair<LL, bitset<100001>>> Pbll;
typedef pair<LL, pair<LL, vector<LL>>> Pvll;
typedef pair<LL, LL> Vec2;
struct Tll {
  LL first, second, third;
};
typedef pair<LL, Tll> Ptll;
#define rep(i, rept) for (LL i = 0; i < rept; i++)
#define Mfor(i, mf) for (LL i = mf - 1; i >= 0; i--)
LL h, w, n, m, k, t, s, q, last, cnt, sum[2000000], ans, dp[5000][5000],
    a[2000000], b[2000000];
string str;
bool f;
char c;
int di[4][2] = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
struct Edge {
  LL from, cost;
};
vector<Edge> vec[1000000];
vector<Pll> v;
map<string, LL> ma;
set<LL> st;
void YN(bool f) {
  if (f)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}
void yn(bool f) {
  if (f)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}
vector<int> topological(vector<vector<bool>> E) {
  int n = (int)E.size();
  vector<bool> V(n);
  vector<int> R;
  for (int i = 0; i < n; i++) {
    int c = -1;
    for (int j = 0; j < n && c == -1; j++)
      if (!V[j]) {
        bool f = true;
        for (int k = 0; k < n && f; k++)
          if (!V[k] && E[k][j])
            f = false;
        if (f)
          c = j;
      }
    if (c == -1)
      return vector<int>();
    V[c] = true;
    R.push_back(c);
  }
  return R;
}

int main() {
  cin >> n >> m;
  vector<vector<bool>> v(n, vector<bool>(n));
  rep(i, m) {
    LL x, y;
    cin >> x >> y;
    v[x][y] = 1;
  }
  vector<int> an = topological(v);
  rep(i, an.size()) cout << an[i] << endl;
  return 0;
}