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

#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;
  DICE i = DICE();
  DICE j = DICE();
  for (int w = 0; w < n; w++) {
    if (i.sort_dice[w] != j.sort_dice[w])
      flag = false;
  }
  if (flag == true) {
    cout << "Yes" << endl;
  } else if (flag == false) {
    cout << "No" << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#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};
  int top = 1001;
  int front = 0;
  const int n = 6;
  int dice_i[6] = {0};
  int dice_j[6] = {0};
  int p;

  void 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 (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 ? 1 : p == 1 ? 0 : p == 2 ? 2 : p == 3 ? 3 : -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];
    }
  }

public:
  int dice[6];
  int sort_dice[6];

  DICE() {
    for (int i = 0; i < n; i++) {
      cin >> dice[i];
      dice_i[i] = dice[i];
      if (top > dice[i]) {
        front = top;
        top = dice[i];
      }
    }
    sort();
  }
};

int main() {
  const int n = 6;
  int dice_1[n];
  int dice_2[n];
  bool flag = true;
  DICE i = DICE();
  DICE j = DICE();
  for (int w = 0; w < n; w++) {
    if (i.sort_dice[w] != j.sort_dice[w])
      flag = false;
  }
  if (flag == true) {
    cout << "Yes" << endl;
  } else if (flag == false) {
    cout << "No" << endl;
  }
  return 0;
}