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

#include <bits/stdc++.h>

#define int long long int
#define rep(a, b, c) for (int a = b; a < c; a++)
#define repm(a, b, c) for (int a = (b - 1); a >= c; a--)
#define pb push_back
#define str string
#define sf(a) scanfs("%d", &a)
#define pb push_back
#define mp make_pair
#define Fi first
#define Se second
#define ALL(v) (v).begin(), (v).end()

using namespace std;

const int INF = 1e18 + 9;
const int Mod = 1e9 + 7;
inline int replac(str s) {
  double ans = 0;
  rep(i, 0, s.length()) { ans += (s[i] - '0') * pow(10, s.length() - i - 1); }
  return (int)ans;
}
inline string numstr(int m) {
  str s = "";
  while (m > 0) {
    char c;
    int a = m / 10;
    if (a > 0)
      a = m % (a * 10);
    else
      a = m;
    c = (char)('0' + a);
    s += c;
    m /= 10;
  }
  str st = "";
  for (int i = s.size() - 1; i >= 0; i--) {
    st += s[i];
  }
  return st;
}
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pii> vii;
vi data[5005];
vi res;
bool used[5005];
vector<pii> a;
void f(int num) {
  if (used[num] == true)
    return;
  used[num] = true;
  for (int v : data[num]) {
    f(v);
  }
  res.pb(num);
}
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  rep(i, 0, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    data[b].pb(a);
    // cout << b << " <- " << a << endl;
  }
  // a>b;
  memset(used, false, sizeof(used));
  rep(i, 0, n) f(i);
  int ff = false;
  rep(i, 0, n - 1) {
    bool u = false;
    int ue = res[i];
    int shi = res[i + 1];
    for (int v = 0; v < data[shi].size(); v++) {
      if (data[shi][v] == ue) {
        u = true;
        break;
      }
    }
    if (u == false) {
      //  cout<< endl << ue <<  " " <<i << " " << shi << endl;
      ff = true;
      break;
    }
  }
  rep(i, 0, n) { cout << res[i] + 1 << endl; }
  cout << ff << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define int long long int
#define rep(a, b, c) for (int a = b; a < c; a++)
#define repm(a, b, c) for (int a = (b - 1); a >= c; a--)
#define pb push_back
#define str string
#define sf(a) scanfs("%d", &a)
#define pb push_back
#define mp make_pair
#define Fi first
#define Se second
#define ALL(v) (v).begin(), (v).end()

using namespace std;

const int INF = 1e18 + 9;
const int Mod = 1e9 + 7;
inline int replac(str s) {
  double ans = 0;
  rep(i, 0, s.length()) { ans += (s[i] - '0') * pow(10, s.length() - i - 1); }
  return (int)ans;
}
inline string numstr(int m) {
  str s = "";
  while (m > 0) {
    char c;
    int a = m / 10;
    if (a > 0)
      a = m % (a * 10);
    else
      a = m;
    c = (char)('0' + a);
    s += c;
    m /= 10;
  }
  str st = "";
  for (int i = s.size() - 1; i >= 0; i--) {
    st += s[i];
  }
  return st;
}
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pii> vii;
vi data[5005];
int an[5005];
bool used[5005];
vector<pii> a;
void f(int num) {
  // if(used[num]==true)return;
  // used[num]=true;
  for (int v : data[num]) {
    an[v] = max(an[v], an[num] + 1);
    f(v);
  }
}
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  rep(i, 0, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    data[a].pb(b);
  }
  memset(an, 0, sizeof(an));
  memset(used, false, sizeof(used));
  rep(i, 0, n) f(i);
  rep(i, 0, n) { a.pb(mp(an[i], i)); }
  sort(ALL(a));
  bool f = false;
  int mae = -1;
  rep(i, 0, n) {
    if (a[i].first == mae) {
      f = true;
    }
    mae = a[i].first;
    cout << a[i].second + 1 << endl;
  }
  cout << f << endl;
  return 0;
}