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

#include <iostream>
#include <string>
using namespace std;

class DICE {
private:
  const int dice_tem[5][6] = {
      {3, 2, 6, 1, 5, 4},
      {2, 6, 3, 4, 1, 5},
      {5, 1, 3, 4, 6, 2},
      {4, 2, 1, 6, 5, 3},
      {1, 2, 3, 4, 5, 6}}; //{0}:W {1}:N {2}:S {3}:E {4}:defalut
  const string dice_rl = "AWWWNWWWNWWWWNWWWSWWWSWWW";
  const int n = 6;
  int dice_i[6] = {0};
  int dice_j[6] = {0};
  int a = 4;

  void setup() {
    for (int i = 0; i < n; i++) {
      cin >> dice[i];
      dice_i[i] = dice[i];
    }
  }

public:
  int dice[6];
  int num = 0;
  const int dice_suffle_num = dice_rl.length();
  DICE() {}
  void roll_dice() {
    switch (dice_rl[num]) {
    case 78: // N
      a = 1;
      break;
    case 83: // S
      a = 2;
      break;
    case 87: // W
      a = 0;
      break;
    case 69: // E
      break;
      a = 3;
    default:
      a = 4;
      break;
    }

    for (int i = 0; i < n; i++)
      dice_j[i] = dice_i[dice_tem[a][i] - 1];
    for (int i = 0; i < n; i++)
      dice_i[i] = dice_j[i];
    num++;
    if (num > dice_suffle_num)
      num = 0;
  }
  int check(int i) { return dice_i[i]; }
  void set() { setup(); }
};

int main() {
  const int n = 6;
  bool flag = true;
  int num;
  cin >> num;
  const int con_num = num;
  DICE dice[100];
  for (int i = 0; i < num; i++)
    dice[i].set();
  for (int i = 0; i < num - 1; i++) {
    for (int j = i + 1; j < num; j++) {
      for (int k = 0; k < dice[0].dice_suffle_num; k++) {
        dice[j].roll_dice();
        for (int l = 0; l < n; l++) {
          if (dice[i].dice[l] != dice[j].check(l)) {
            flag = true;
            break;
          }
          flag = false;
        }
        if (flag == false)
          break;
      }
      if (flag == false)
        break;
    }
    if (flag == false)
      break;
  }

  if (flag == true) {
    cout << "Yes" << endl;
  } else if (flag == false) {
    cout << "No" << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>
using namespace std;

class DICE {
private:
  const int dice_tem[4][6] = {{3, 2, 6, 1, 5, 4},
                              {2, 6, 3, 4, 1, 5},
                              {5, 1, 3, 4, 6, 2},
                              {4, 2, 1, 6, 5, 3}};
  const int sort_num[6] = {1, 4, 2, 5, 3, 6};
  static int top;
  static int front;
  const int n = 6;
  int dice_i[6] = {0};
  int dice_j[6] = {0};
  int ee[6];
  int p;

  void setup() {
    for (int i = 0; i < n; i++) {
      cin >> dice[i];
      dice_i[i] = dice[i];
      ee[i] = dice[i];
    }
    sort(ee, ee + n);
    for (int i = 0, o = 0; i < n && o < 2; i++) {
      ee[o] = ee[i];
      if (ee[m] != ee[i])
        o++;
    }
    if (m == 0) {

      top = ee[0];
      front = ee[1];
    }
  }

  void dice_sort() {
    for (int d = 0; dice_i[0] != top; d++) {
      for (int i = 0; i < n; i++) {
        dice_j[i] = dice_i[dice_tem[d % 2][i] - 1];
      }
      for (int i = 0; i < n; i++)
        dice_i[i] = dice_j[i];
    }
    for (p = 0; p < 4; p++) {
      for (int i = 0; i < n; i++) {
        dice_j[i] = dice_i[dice_tem[p][i] - 1];
      }
      if (front == dice_j[0])
        break;
    }
    int a = p == 0 ? 3 : p == 1 ? 0 : p == 2 ? 2 : p == 3 ? 1 : -1;
    if (a == 0) {
      for (int i = 0; i < n; i++)
        sort_dice[i] = dice_i[i];
    } else {
      for (int q = 0; q < a; q++) {
        for (int i = 0; i < n; i++)
          sort_dice[i] = dice_i[sort_num[i] - 1];
        for (int i = 0; i < n; i++)
          dice_i[i] = sort_dice[i];
      }
    }
  }

public:
  int dice[6];
  int sort_dice[6];
  static int m;
  DICE() {
    setup();
    dice_sort();
    m++;
  }
};
int DICE::m = 0;
int DICE::top = 0;
int DICE::front = 0;
int main() {
  const int n = 6;
  bool flag = true;
  int num;
  cin >> num;
  DICE corr;

  for (int i = 0; i < num - 1 && flag == true; i++) {
    DICE che;
    int w = 0;
    for (w = 0; w < 6; w++) {
      if (corr.sort_dice[w] != che.sort_dice[w]) {
        break;
      }
    }
    if (w == n)
      flag = false;
  }

  if (flag == true) {
    cout << "Yes" << endl;
  } else if (flag == false) {
    cout << "No" << endl;
  }
  return 0;
}