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

#include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

struct before_main {
  before_main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
  }
} before_main;

#define REP(i, a, b) for (int i = a; i < (int)b; i++)
#define rep(i, n) REP(i, 0, n)
#define all(c) (c).begin(), (c).end()
#define zero(a) memset(a, 0, sizeof a)
#define minus(a) memset(a, -1, sizeof a)
#define watch(a)                                                               \\
  { cout << #a << " = " << a << endl; }
template <class T1, class T2> inline bool minimize(T1 &a, T2 b) {
  return b < a && (a = b, 1);
}
template <class T1, class T2> inline bool maximize(T1 &a, T2 b) {
  return a < b && (a = b, 1);
}

typedef long long ll;
int const inf = 1 << 29;

bool isDAG(vector<vector<int>> &G) {
  int N = G.size();
  vector<bool> vis(N);

  std::function<bool(int, unordered_set<int> &)> dfs =
      [&](int curr, unordered_set<int> &st) {
        for (auto &&e : G[curr]) {
          if (st.count(e))
            return false;
          if (vis[e])
            continue;
          st.insert(e);
          vis[e] = 1;
          if (!dfs(e, st))
            return false;
          st.erase(e);
        }
        return true;
      };

  rep(e, N) {
    vis[e] = 1;
    unordered_set<int> st = {e};
    if (!dfs(e, st))
      return false;
  }
  return true;
}

int main() {

  int M, N;
  cin >> M >> N;

  // テ・ツ青?」ツつケテ」ツつュテ」ツδォテヲツッツ偲」ツ?ォテ」ツつウテ」ツδ榲」ツδウテ」ツδ嘉」ツ?ョテ・ツ個コテゥツ鳴禿」ツつ津ヲツ個?」ツ?、
  vector<vector<pair<int, int>>> cmd_sect(N,
                                          vector<pair<int, int>>(M, {0, inf}));

  rep(cmd_idx, M) {
    int K;
    cin >> K;
    rep(_, K) {
      int skill;
      cin >> skill;
      skill--;
      string op;
      cin >> op;
      int threshold;
      cin >> threshold;
      if (op == ">=") {
        cmd_sect[skill][cmd_idx].first =
            max(cmd_sect[skill][cmd_idx].first, threshold);
      } else {
        cmd_sect[skill][cmd_idx].second =
            min(cmd_sect[skill][cmd_idx].second, threshold);
      }
      if (cmd_sect[skill][cmd_idx].first > cmd_sect[skill][cmd_idx].second) {
        cout << "No" << endl;
        return 0;
      }
    }
  }

  vector<vector<int>> G(M);

  rep(i, N) {
    rep(a, M) rep(b, M) {
      if (a == b)
        continue;
      if (cmd_sect[i][a].second < cmd_sect[i][b].first)
        G[a].push_back(b);
    }
  }

  rep(i, M) {
    sort(all(G[i]));
    G[i].erase(unique(all(G[i])), G[i].end());
  }

  cout << (isDAG(G) ? "Yes" : "No") << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

struct before_main {
  before_main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
  }
} before_main;

#define REP(i, a, b) for (int i = a; i < (int)b; i++)
#define rep(i, n) REP(i, 0, n)
#define all(c) (c).begin(), (c).end()
#define zero(a) memset(a, 0, sizeof a)
#define minus(a) memset(a, -1, sizeof a)
#define watch(a)                                                               \\
  { cout << #a << " = " << a << endl; }
template <class T1, class T2> inline bool minimize(T1 &a, T2 b) {
  return b < a && (a = b, 1);
}
template <class T1, class T2> inline bool maximize(T1 &a, T2 b) {
  return a < b && (a = b, 1);
}

typedef long long ll;
int const inf = 1 << 29;

bool isDAG(vector<vector<int>> &G) {
  int N = G.size();
  vector<bool> vis(N);

  std::function<bool(int)> dfs = [&](int curr) {
    for (auto &&e : G[curr]) {
      if (vis[e])
        return false;
      vis[e] = 1;
      if (!dfs(e))
        return false;
      vis[e] = 0;
    }
    return true;
  };

  vector<int> deg(N);

  rep(i, N) for (auto &&e : G[i]) deg[e]++;

  vector<int> starts;

  rep(i, N) if (deg[i] == 0) starts.push_back(i);

  if (starts.empty())
    return false;

  for (auto &&e : starts) {
    vis[e] = 1;
    if (!dfs(e))
      return false;
    vis[e] = 0;
  }
  return true;
}

int main() {

  int M, N;
  cin >> M >> N;

  // テ・ツ青?」ツつケテ」ツつュテ」ツδォテヲツッツ偲」ツ?ォテ」ツつウテ」ツδ榲」ツδウテ」ツδ嘉」ツ?ョテ・ツ個コテゥツ鳴禿」ツつ津ヲツ個?」ツ?、
  vector<vector<pair<int, int>>> cmd_sect(N,
                                          vector<pair<int, int>>(M, {0, inf}));

  rep(cmd_idx, M) {
    int K;
    cin >> K;
    rep(_, K) {
      int skill;
      cin >> skill;
      skill--;
      string op;
      cin >> op;
      int threshold;
      cin >> threshold;
      if (op == ">=") {
        cmd_sect[skill][cmd_idx].first =
            max(cmd_sect[skill][cmd_idx].first, threshold);
      } else {
        cmd_sect[skill][cmd_idx].second =
            min(cmd_sect[skill][cmd_idx].second, threshold);
      }
      if (cmd_sect[skill][cmd_idx].first > cmd_sect[skill][cmd_idx].second) {
        cout << "No" << endl;
        return 0;
      }
    }
  }

  vector<vector<int>> G(M);

  rep(i, N) {
    rep(a, M) rep(b, M) {
      if (a == b)
        continue;
      if (cmd_sect[i][a].second < cmd_sect[i][b].first)
        G[a].push_back(b);
    }
  }

  rep(i, M) {
    sort(all(G[i]));
    G[i].erase(unique(all(G[i])), G[i].end());
  }

  cout << (isDAG(G) ? "Yes" : "No") << endl;

  return 0;
}