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

#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
using namespace std;

int m, n;
int k[101];
int data[101][101][2];
int s[101][101], cond[101][101], t[101][101];
int now[101];
int next[101];
bool used[101];

int main(void) {
  scanf("%d %d", &m, &n);
  for (int i = 0; i <= 100; i++) {
    for (int j = 0; j <= 100; j++) {
      data[i][j][0] = 0;
      data[i][j][1] = 101;
    }
  }
  for (int i = 0; i < m; i++) {
    scanf("%d", &k[i]);
    for (int j = 0; j < k[i]; j++) {
      char d;
      scanf("%d %c%*c %d", &s[i][j], &d, &t[i][j]);
      if (d == '<')
        cond[i][j] = 0;
      else
        cond[i][j] = 1;
    }
  }
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < k[i]; j++) {
      if (cond[i][j] == 0)
        data[i][s[i][j]][1] = min(t[i][j], data[i][s[i][j]][1]);
      if (cond[i][j] == 1)
        data[i][s[i][j]][0] = max(t[i][j], data[i][s[i][j]][0]);
      if (data[i][s[i][j]][1] < data[i][s[i][j]][0]) {
        printf("No\\n");
        return 0;
      }
    }
  }
  memset(used, false, sizeof(used));
  for (int i = 1; i <= n; i++)
    now[i] = 100;
  int i;
  for (i = 0; i < m; i++) {
    int nec = -1;
    int j;
    for (j = 0; j < m; j++) {
      if (used[j])
        continue;
      for (int k = 1; k <= n; k++) {
        if (now[k] < data[j][k][0]) {
          printf("No\\n");
          return 0;
        }
        if (now[k] > data[j][k][1])
          next[k] = data[j][k][1];
        else
          next[k] = now[k];
      }
      bool flag = true;
      for (int k = 0; k < m; k++) {
        if (used[k])
          continue;
        for (int l = 1; l <= n; l++) {
          if (next[l] < data[k][l][0]) {
            flag = false;
            break;
          }
        }
      }
      if (flag) {
        nec = j;
        break;
      }
    }
    if (nec == -1)
      break;
    used[nec] = true;
    for (int j = 1; j <= n; j++)
      now[j] = next[j];
  }
  printf("%s\\n", i == m ? "Yes" : "No");
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
using namespace std;

int m, n;
int k[101];
int data[101][101][2];
int s[101][101], cond[101][101], t[101][101];
vector<int> G[101];
vector<int> rG[101];
vector<int> vs;
bool used[101];
int cmp[101];
int now[101][101];

void add_edge(int f, int t) {
  G[f].push_back(t);
  G[t].push_back(f);
}

void dfs(int v) {
  used[v] = true;
  for (int i = 0; i < G[v].size(); i++) {
    if (!used[G[v][i]])
      dfs(G[v][i]);
  }
  vs.push_back(v);
}

void rdfs(int v, int k) {
  used[v] = true;
  cmp[v] = k;
  for (int i = 0; i < rG[v].size(); i++) {
    if (!used[rG[v][i]])
      rdfs(rG[v][i], k);
  }
}

void cdfs(int v) {
  used[v] = true;
  for (int i = 0; i < G[v].size(); i++) {
    if (!used[G[v][i]] && cmp[v] + 1 >= cmp[G[v][i]])
      cdfs(G[v][i]);
  }
}

bool scc() {
  memset(used, 0, sizeof(used));
  for (int i = 0; i < m; i++) {
    if (!used[i])
      dfs(i);
  }
  memset(used, 0, sizeof(used));
  int k = 0;
  for (int i = vs.size() - 1; i >= 0; i--) {
    if (!used[vs[i]])
      rdfs(vs[i], k++);
  }
  memset(used, 0, sizeof(used));
  cdfs(vs[vs.size() - 1]);
  for (int i = 0; i < m; i++) {
    if (!used[i])
      return false;
  }
  return true;
}

bool ddfs(int v, int depth) {
  if (v >= 0)
    used[v] = true;
  if (depth == m)
    return true;
  if (v == -1) {
    for (int i = 0; i < m; i++) {
      if (used[i])
        continue;
      int k;
      for (k = 1; k <= n; k++) {
        if (now[depth][k] > data[i][k][1])
          break;
        if (now[depth][k] < data[i][k][0])
          now[depth + 1][k] = data[i][k][0];
        else
          now[depth + 1][k] = now[depth][k];
      }
      if (k == n + 1) {
        if (ddfs(i, depth + 1))
          return true;
      }
    }
  } else {
    for (int i = 0; i < G[v].size(); i++) {
      if (used[G[v][i]])
        continue;
      int k;
      for (k = 1; k <= n; k++) {
        if (now[depth][k] > data[G[v][i]][k][1])
          break;
        if (now[depth][k] < data[G[v][i]][k][0])
          now[depth + 1][k] = data[G[v][i]][k][0];
        else
          now[depth + 1][k] = now[depth][k];
      }
      if (k == n + 1) {
        if (ddfs(G[v][i], depth + 1))
          return true;
      }
    }
  }
  if (v >= 0)
    used[v] = false;
  return false;
}

int main(void) {
  scanf("%d %d", &m, &n);
  for (int i = 0; i <= 100; i++) {
    for (int j = 0; j <= 100; j++) {
      data[i][j][0] = 0;
      data[i][j][1] = 101;
    }
  }
  for (int i = 0; i < m; i++) {
    scanf("%d", &k[i]);
    for (int j = 0; j < k[i]; j++) {
      char d;
      scanf("%d %c%*c %d", &s[i][j], &d, &t[i][j]);
      if (d == '<')
        cond[i][j] = 0;
      else
        cond[i][j] = 1;
    }
  }
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < k[i]; j++) {
      if (cond[i][j] == 0)
        data[i][s[i][j]][1] = min(t[i][j], data[i][s[i][j]][1]);
      if (cond[i][j] == 1)
        data[i][s[i][j]][0] = max(t[i][j], data[i][s[i][j]][0]);
      if (data[i][s[i][j]][1] < data[i][s[i][j]][0]) {
        printf("No\\n");
        return 0;
      }
    }
  }
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < m; j++) {
      if (i == j)
        continue;
      int k;
      for (k = 1; k <= n; k++) {
        if (data[i][k][0] > data[j][k][1])
          break;
      }
      if (k == n + 1) {
        add_edge(i, j);
      }
    }
  }
  printf("%s\\n", ddfs(-1, 0) ? "Yes" : "No");
  return 0;
}