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

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
/*
N?????????????????????????????§????????????????????????(No)????????????????????°??????(Yes)???????????????CheckConsistentDice_N
?????????????????????????????°???????????§?????????????????????CheckConsistentLabel
?????????????????????????????????????????¢????????????????????????TopFront
*/
class DICE {
private:
  int Label[6];

public:
  void AddLabel() {
    for (int i = 0; i < 6; ++i) {
      std::cin >> Label[i];
    }
    return;
  }
  void RotateDirection(std::string);
  void Rotate(int, int, int, int);
  int GetLabel(int x) { return Label[x]; }
  bool TopBackFront(int, int, int);
  bool CheckConsistentLabel(DICE AnotherDice);
};

void DICE::RotateDirection(std::string s) {
  if (s == "E") {
    Rotate(0, 3, 5, 2);
  }
  if (s == "N") {
    Rotate(0, 1, 5, 4);
  }
  if (s == "S") {
    Rotate(0, 4, 5, 1);
  }
  if (s == "W") {
    Rotate(0, 2, 5, 3);
  }
  return;
}
void DICE::Rotate(int a, int b, int c, int d) {
  std::swap(Label[a], Label[b]);
  std::swap(Label[b], Label[c]);
  std::swap(Label[c], Label[d]);
  return;
}
bool DICE::TopBackFront(int top, int back, int front) {
  const int size_str = 7;
  std::string str[size_str] = {"N", "N", "N", "W", "N", "N", "N"};
  for (unsigned int i = 0; i < size_str; ++i) {
    if (front == Label[1]) {
      break;
    }
    RotateDirection(str[i]);
  }
  if (back != Label[4]) {
    return false;
  }

  while (top != Label[0]) {
    RotateDirection("W");
  }
  return true;
}
bool DICE::CheckConsistentLabel(DICE AnotherDice) {
  int NumberLabel[101] = {0};
  for (int i = 0; i < 6; ++i) {
    ++NumberLabel[Label[i]];
    --NumberLabel[AnotherDice.GetLabel(i)];
  }
  int i = 0;
  while (i <= 100) {
    if (NumberLabel[i] != 0) {
      return false;
    }
    ++i;
  }
  return true;
}
bool CheckConsistentDice_N(std::vector<DICE> &dice) {
  const int LastNumberDice = 5;
  for (unsigned int i = 0; i < dice.size(); ++i) {
    for (unsigned int j = i + 1; j < dice.size(); ++j) {

      if (dice[i].CheckConsistentLabel(dice[j]) &&
          dice[i].TopBackFront(dice[j].GetLabel(0), dice[j].GetLabel(4),
                               dice[j].GetLabel(1))) {
        for (int k = 0; k <= LastNumberDice; ++k) {
          if (dice[i].GetLabel(k) != dice[j].GetLabel(k)) {
            break;
          } else {
            if (k == LastNumberDice) {
              return false;
            }
          }
        }
      }
    }
  }
  return true;
}
int main(void) {
  int NumberOfDice;
  std::cin >> NumberOfDice;
  std::vector<DICE> dice(NumberOfDice);
  for (int i = 0; i < NumberOfDice; ++i) {
    dice[i].AddLabel();
  }
  if (CheckConsistentDice_N(dice)) {
    std::cout << "Yes" << std::endl;
  } else {
    std::cout << "No" << std::endl;
  }
  //	system("pause");
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
/*
N?????????????????????????????§????????????????????????(No)????????????????????°??????(Yes)???????????????CheckConsistentDice_N
?????????????????????????????°???????????§?????????????????????CheckConsistentLabel
?????????????????????????????????????????¢????????????????????????TopFront
*/
class DICE {
private:
  int Label[6];

public:
  void AddLabel() {
    for (int i = 0; i < 6; ++i) {
      std::cin >> Label[i];
    }
    return;
  }
  void RotateDirection(std::string);
  void Rotate(int, int, int, int);
  int GetLabel(int x) { return Label[x]; }
  bool TopBackFront(int, int, int);
  bool CheckConsistentLabel(DICE AnotherDice);
};

void DICE::RotateDirection(std::string s) {
  if (s == "E") {
    Rotate(0, 3, 5, 2);
  }
  if (s == "N") {
    Rotate(0, 1, 5, 4);
  }
  if (s == "S") {
    Rotate(0, 4, 5, 1);
  }
  if (s == "W") {
    Rotate(0, 2, 5, 3);
  }
  return;
}
void DICE::Rotate(int a, int b, int c, int d) {
  std::swap(Label[a], Label[b]);
  std::swap(Label[b], Label[c]);
  std::swap(Label[c], Label[d]);
  return;
}
bool DICE::TopBackFront(int top, int back, int front) {
  const int size_str = 7;
  std::string str[size_str] = {"N", "N", "N", "W", "N", "N", "N"};
  for (unsigned int i = 0; i < size_str; ++i) {
    if (front == Label[1]) {
      break;
    }
    RotateDirection(str[i]);
  }
  /*if (back != Label[5]) {
          return false;
  }
  */
  while (top != Label[0]) {
    RotateDirection("W");
  }
  return true;
}
bool DICE::CheckConsistentLabel(DICE AnotherDice) {
  int NumberLabel[101] = {0};
  for (int i = 0; i < 6; ++i) {
    ++NumberLabel[Label[i]];
    --NumberLabel[AnotherDice.GetLabel(i)];
  }
  int i = 0;
  while (i <= 100) {
    if (NumberLabel[i] != 0) {
      return false;
    }
    ++i;
  }
  return true;
}
bool CheckConsistentDice_N(std::vector<DICE> &dice) {
  const int LastNumberDice = 5;
  for (unsigned int i = 0; i < dice.size(); ++i) {
    for (unsigned int j = i + 1; j < dice.size(); ++j) {

      if (dice[i].CheckConsistentLabel(dice[j]) &&
          dice[i].TopBackFront(dice[j].GetLabel(0), dice[j].GetLabel(5),
                               dice[j].GetLabel(1))) {
        for (int k = 0; k <= LastNumberDice; ++k) {
          if (dice[i].GetLabel(k) != dice[j].GetLabel(k)) {
            break;
          } else {
            if (k == LastNumberDice) {
              return false;
            }
          }
        }
      }
    }
  }
  return true;
}
int main(void) {
  int NumberOfDice;
  std::cin >> NumberOfDice;
  std::vector<DICE> dice(NumberOfDice);
  for (int i = 0; i < NumberOfDice; ++i) {
    dice[i].AddLabel();
  }
  if (CheckConsistentDice_N(dice)) {
    std::cout << "Yes" << std::endl;
  } else {
    std::cout << "No" << std::endl;
  }
  // system("pause");
  return 0;
}