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

#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <utility>
#include <vector>

using namespace std;

typedef long long Long;
#define whole(xs) xs.begin(), xs.end()

template <class T> ostream &operator<<(ostream &os, const vector<T> &vs) {
  os << vs[0];
  for (int i = 1; i < vs.size(); i++) {
    os << ' ' << vs[i];
  }
  return os;
}

const int MAX_N = 5001;

int N, M;
vector<int> G[MAX_N];
void input() {
  cin >> N >> M;
  for (int i = 0; i < N; i++) {
    G[i].clear();
  }
  for (int i = 0; i < M; i++) {
    int x, y;
    cin >> x >> y;
    G[x].push_back(y);
  }
  for (int i = 1; i <= N; i++) {
    G[0].push_back(i);
  }
}

void dfs(vector<int> &result, vector<bool> &used, int v) {
  if (used[v])
    return;
  used[v] = true;
  for (int i = 0; i < G[v].size(); i++) {
    int u = G[v][i];
    if (used[u])
      continue;
    dfs(result, used, u);
  }
  result.push_back(v);
}
void tsort(vector<int> &res) {
  vector<bool> used(N, false);
  dfs(res, used, 0);
  reverse(whole(res));
}

int dfs1(int v, vector<int> &cache) {
  if (G[v].empty())
    return v;
  if (cache[v] >= -1)
    return cache[v];
  int x = dfs1(G[v][0], cache);
  for (int i = 1; i < G[v].size(); i++) {
    int u = G[v][i];
    if (x == dfs1(u, cache))
      continue;
    return cache[v] = -1;
  }
  return cache[v] = x;
}

void solve() {
  vector<int> res;
  tsort(res);
  for (int i = 1; i <= N; i++) {
    cout << res[i] << endl;
  }
  vector<int> cache(N + 1, -2);
  cout << (dfs1(0, cache) == -1) << endl;
}

int main() {
  input();
  solve();
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <utility>
#include <vector>

using namespace std;

typedef long long Long;
#define whole(xs) xs.begin(), xs.end()

template <class T> ostream &operator<<(ostream &os, const vector<T> &vs) {
  os << vs[0];
  for (int i = 1; i < vs.size(); i++) {
    os << ' ' << vs[i];
  }
  return os;
}

const int MAX_N = 5001;

int N, M;
vector<int> G[MAX_N];
void input() {
  cin >> N >> M;
  for (int i = 0; i < N; i++) {
    G[i].clear();
  }
  for (int i = 0; i < M; i++) {
    int x, y;
    cin >> x >> y;
    G[x].push_back(y);
  }
  for (int i = 1; i <= N; i++) {
    G[0].push_back(i);
  }
}

void dfs(vector<int> &result, vector<bool> &used, int v) {
  if (used[v])
    return;
  used[v] = true;
  for (int i = 0; i < G[v].size(); i++) {
    int u = G[v][i];
    dfs(result, used, u);
  }
  result.push_back(v);
}
void tsort(vector<int> &res) {
  vector<bool> used(N, false);
  for (int i = 0; i < N + 1; i++) {
    dfs(res, used, i);
  }
  reverse(whole(res));
}

int dfs1(int v) {
  if (G[v].empty())
    return v;
  int x = dfs1(G[v][0]);
  for (int i = 1; i < G[v].size(); i++) {
    int u = G[v][i];
    if (x == dfs1(u))
      continue;
    return -1;
  }
  return x;
}

void solve() {
  vector<int> res;
  tsort(res);
  for (int i = 1; i <= N; i++) {
    cout << res[i] << endl;
  }
  cout << (dfs1(0) == -1) << endl;
}

int main() {
  input();
  solve();
  return 0;
}