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

#include <cmath>
#include <iomanip>
#include <iostream>

using namespace std;

class dice {
public:
  int label[7];
  void turn(char c) {
    int temp;
    if (c == 'S') {
      temp = label[1];
      label[1] = label[5];
      label[5] = label[6];
      label[6] = label[2];
      label[2] = temp;
    } else if (c == 'E') {
      temp = label[1];
      label[1] = label[4];
      label[4] = label[6];
      label[6] = label[3];
      label[3] = temp;
    } else if (c == 'W') {
      temp = label[1];
      label[1] = label[3];
      label[3] = label[6];
      label[6] = label[4];
      label[4] = temp;
    } else if (c == 'N') {
      temp = label[1];
      label[1] = label[2];
      label[2] = label[6];
      label[6] = label[5];
      label[5] = temp;
    } else if (c == 'T') {
      temp = label[2];
      label[2] = label[3];
      label[3] = label[5];
      label[5] = label[4];
      label[4] = temp;
    }
  }
  void out(int k) { cout << label[k] << endl; }
};

int main() {
  int i, n, u, m, flag[101][102], j, k, ii;
  cin >> n;
  dice d[100];
  for (j = 0; j < 101; j++) {
    for (i = 0; i < 102; i++) {
      flag[j][i] = 0;
    }
  }

  for (j = 0; j < n; j++) {
    for (i = 1; i < 7; i++) {
      cin >> d[j].label[i];
      flag[j][d[j].label[i]] += 1;
      flag[j][101] += d[j].label[i];
    }

    for (k = 0; k < j; k++) {
      if (flag[j][101] == flag[k][101]) {
        for (ii = 1; ii < 7; ii++) {
          if (flag[j][d[j].label[ii]] != flag[k][d[j].label[ii]])
            break;
          if (ii == 6) {
            if (d[j].label[2] == d[k].label[1]) {
              d[j].turn('N');
            } else if (d[j].label[5] == d[k].label[1]) {
              d[j].turn('S');
            } else {
              while (d[j].label[1] != d[k].label[1]) {
                d[j].turn('W');
              }
            }
            int f = 0;
            while (d[j].label[2] != d[k].label[2]) {
              ii++;
              d[j].turn('T');
              if (ii == 10)
                break;
            }
            for (i = 3; i < 7; i++) {
              if (d[j].label[i] != d[k].label[i]) {
                f = 1;
                break;
              }
            }
            if (f == 0) {
              cout << "No" << endl;
              return 0;
            }
          }
        }
      }
    }
  }
  cout << "Yes" << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <cmath>
#include <iomanip>
#include <iostream>

using namespace std;

class dice {
public:
  int label[7];
  void turn(char c) {
    int temp;
    if (c == 'S') {
      temp = label[1];
      label[1] = label[5];
      label[5] = label[6];
      label[6] = label[2];
      label[2] = temp;
    } else if (c == 'E') {
      temp = label[1];
      label[1] = label[4];
      label[4] = label[6];
      label[6] = label[3];
      label[3] = temp;
    } else if (c == 'W') {
      temp = label[1];
      label[1] = label[3];
      label[3] = label[6];
      label[6] = label[4];
      label[4] = temp;
    } else if (c == 'N') {
      temp = label[1];
      label[1] = label[2];
      label[2] = label[6];
      label[6] = label[5];
      label[5] = temp;
    } else if (c == 'T') {
      temp = label[2];
      label[2] = label[3];
      label[3] = label[5];
      label[5] = label[4];
      label[4] = temp;
    }
  }
  void out(int k) { cout << label[k] << endl; }
};

int main() {
  int i, n, u, m, flag[101][102], j, k, ii;
  cin >> n;
  dice d[100];
  for (j = 0; j < 101; j++) {
    for (i = 0; i < 102; i++) {
      flag[j][i] = 0;
    }
  }

  for (j = 0; j < n; j++) {
    for (i = 1; i < 7; i++) {
      cin >> d[j].label[i];
      flag[j][d[j].label[i]] += 1;
      flag[j][101] += d[j].label[i];
    }

    for (k = 0; k < j; k++) {
      if (flag[j][101] == flag[k][101]) {
        for (ii = 1; ii < 7; ii++) {
          if (flag[j][d[j].label[ii]] != flag[k][d[j].label[ii]])
            break;
          if (ii == 6) {
            if (d[j].label[2] == d[k].label[1]) {
              d[j].turn('N');
            } else if (d[j].label[5] == d[k].label[1]) {
              d[j].turn('S');
            } else {
              while (d[j].label[1] != d[k].label[1]) {
                d[j].turn('W');
              }
            }
            while (d[j].label[2] != d[k].label[2]) {
              d[j].turn('T');
            }
            int f = 0;
            for (i = 3; i < 7; i++) {
              if (d[j].label[i] != d[k].label[i])
                f = 1;
              break;
            }
            if (f == 0) {
              cout << "No" << endl;
              return 0;
            }
          }
        }
      }
    }
  }
  cout << "Yes" << endl;
  return 0;
}