Problem p00140 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <vector>

using namespace std;

int main() {

  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int from, to;
    cin >> from >> to;
    // ¶ûüÉV~[V
    int angle = 1;
    int c = from;
    vector<int> ph;
    ph.push_back(c);
    while (1) {
      if (angle == 0 && c == 5) {
        c++;
      } else if (angle == 1 && c == 5) {
        c--;
      } else if (c >= 6 && c <= 9) {
        c++;
        if (c == 10) {
          c = 5;
          angle = 1;
        }
      } else if (angle == 0 && c >= 1 && c <= 4) {
        c++;
      } else if (angle == 1 && c >= 1 && c <= 4) {
        c--;
      } else if (c == 0) {
        c++;
        angle = 0;
      }
      ph.push_back(c);
      if (c == to)
        break;
    }
    c = from;
    angle = 0;
    vector<int> ph2;
    ph2.push_back(c);
    while (1) {
      if (angle == 0 && c == 5) {
        c++;
      } else if (angle == 1 && c == 5) {
        c--;
      } else if (c >= 6 && c <= 9) {
        c++;
        if (c == 10) {
          c = 5;
          angle = 1;
        }
      } else if (angle == 0 && c >= 1 && c <= 4) {
        c++;
      } else if (angle == 1 && c >= 1 && c <= 4) {
        c--;
      } else if (c == 0) {
        c++;
        angle = 0;
      }
      ph2.push_back(c);
      if (c == to)
        break;
    }
    if (ph.size() < ph2.size()) {
      for (int j = 0; j < ph.size(); j++) {
        cout << ph[j];
        if (j != ph.size() - 1) {
          cout << " ";
        } else
          cout << endl;
      }
    } else {
      for (int j = 0; j < ph2.size(); j++) {
        cout << ph2[j];
        if (j != ph2.size() - 1) {
          cout << " ";
        } else
          cout << endl;
      }
    }
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <vector>

using namespace std;

int main() {

  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int from, to;
    cin >> from >> to;
    // ¶ûüÉV~[V
    int angle = 1;
    int c = from;
    vector<int> ph;
    ph.push_back(c);
    while (1) {
      if (angle == 0 && c == 5) {
        c++;
      } else if (angle == 1 && c == 5) {
        c--;
      } else if (c >= 6 && c <= 9) {
        c++;
        if (c == 10) {
          c = 5;
          angle = 1;
        }
      } else if (angle == 0 && c >= 1 && c <= 4) {
        c++;
      } else if (angle == 1 && c >= 1 && c <= 4) {
        c--;
      } else if (c == 0) {
        c++;
      }
      ph.push_back(c);
      if (c == to)
        break;
    }
    c = from;
    angle = 0;
    vector<int> ph2;
    ph2.push_back(c);
    while (1) {
      if (angle == 0 && c == 5) {
        c++;
      } else if (angle == 1 && c == 5) {
        c--;
      } else if (c >= 6 && c <= 9) {
        c++;
        if (c == 10) {
          c = 5;
          angle = 1;
        }
      } else if (angle == 0 && c >= 1 && c <= 4) {
        c++;
      } else if (angle == 1 && c >= 1 && c <= 4) {
        c--;
      } else if (c == 0) {
        c++;
      }
      ph2.push_back(c);
      if (c == to)
        break;
    }
    if (ph.size() < ph2.size()) {
      for (int j = 0; j < ph.size(); j++) {
        cout << ph[j];
        if (j != ph.size() - 1) {
          cout << " ";
        } else
          cout << endl;
      }
    } else {
      for (int j = 0; j < ph2.size(); j++) {
        cout << ph2[j];
        if (j != ph2.size() - 1) {
          cout << " ";
        } else
          cout << endl;
      }
    }
  }

  return 0;
}