Problem p00701 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#define N 110
using namespace std;

int main() {
  int data[N][N];
  int size[N];

  while (1) {
    int m;
    cin >> m;
    if (m == 0)
      break;
    for (int i = 0; i <= m; i++) {
      size[i] = 0;
    }
    for (int i = 1; i <= m; i++) {
      data[i][size[i]] = i;
      size[i]++;
    }
    while (1) {
      /*for(int i=0;i<=m;i++){
        cout << size[i] <<  ' ';
        }
      cout << endl;
      */
      int a, b;
      cin >> a >> b;
      if (a == 0 && b == 0)
        break;

      if (a == b)
        continue;

      int tmp1[2], tmp2[2];
      for (int i = 0; i <= m; i++) {
        for (int j = 0; j < size[i]; j++) {
          if (data[i][j] == a) {
            tmp1[0] = i;
            tmp1[1] = j;
          }
          if (data[i][j] == b) {
            tmp2[0] = i;
            tmp2[1] = j;
          }
        }
      }
      if (b == 0) {
        if (tmp1[1] == 0)
          continue;
        else {
          int tmpsize = size[tmp1[0]] - tmp1[1];
          for (int i = tmp1[1]; i < size[tmp1[0]]; i++) {
            int tmp;
            tmp = data[tmp1[0]][i];
            data[tmp][size[tmp]] = tmp;
            size[tmp]++;
          }
          for (int i = 0; i < tmpsize; i++) {
            size[tmp1[0]]--;
          }
        }
      } else if (tmp1[0] == tmp2[0]) {
        if (tmp1[1] > tmp2[1])
          continue;
        else {
          int count = 0;
          for (int i = tmp1[1] + 1; i < size[tmp1[0]]; i++) {
            int tmp;
            tmp = data[tmp1[0]][i];
            data[tmp][size[tmp]] = tmp;
            size[tmp]++;
            count++;
          }
          size[tmp1[0]] -= count;
          data[b][size[b]] = a;
          size[b]++;
          size[tmp1[0]]--;
        }
      } else {
        int count = 1;
        data[tmp2[0]][size[tmp2[0]]] = data[tmp1[0]][tmp1[1]];
        size[tmp2[0]]++;
        for (int i = tmp1[1] + 1; i < size[tmp1[0]]; i++) {
          int tmp = data[tmp1[0]][i];
          data[tmp][size[tmp]] = tmp;
          size[tmp]++;
          count++;
        }
        for (int i = 0; i < count; i++) {
          size[tmp1[0]]--;
        }
      }
    }

    sort(size, size + m + 1);

    for (int i = 0; i <= m; i++) {
      if (size[i] == 0)
        continue;
      cout << size[i] << endl;
    }
    cout << "end" << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
#define N 101
using namespace std;

int main() {
  int data[N][N];
  int size[N];

  while (1) {
    int m;
    cin >> m;
    if (m == 0)
      break;
    for (int i = 0; i <= m; i++) {
      size[i] = 0;
    }
    for (int i = 1; i <= m; i++) {
      data[i][size[i]] = i;
      size[i]++;
    }
    while (1) {
      int a, b;
      cin >> a >> b;
      if (a == 0 && b == 0)
        break;

      if (a == b)
        continue;

      int tmp1[2], tmp2[2];
      for (int i = 0; i <= m; i++) {
        for (int j = 0; j < size[i]; j++) {
          if (data[i][j] == a) {
            tmp1[0] = i;
            tmp1[1] = j;
          }
          if (data[i][j] == b) {
            tmp2[0] = i;
            tmp2[1] = j;
          }
        }
      }
      if (b == 0) {
        if (tmp1[1] == 0 && size[tmp1[0]] == 1)
          continue;
        else {
          int tmpsize = size[tmp1[0]] - tmp1[1];
          for (int i = tmp1[1]; i < size[tmp1[0]]; i++) {
            int tmp;
            tmp = data[tmp1[0]][i];
            data[tmp][size[tmp]] = tmp;
            size[tmp]++;
          }
          for (int i = 0; i < tmpsize; i++) {
            size[tmp1[0]]--;
          }
        }
      } else if (tmp1[0] == tmp2[0]) {
        if (tmp1[1] > tmp2[1])
          continue;
        else {
          int tmpsize = size[tmp1[0]];
          for (int i = size[tmp1[0]] - 1; i > 0; i--) {
            int tmp;
            tmp = data[tmp1[0]][i];
            data[tmp][size[tmp]] = tmp;
            size[tmp]++;
          }
          for (int i = 1; i < tmpsize; i++) {
            size[tmp1[0]]--;
          }
        }
      } else {
        int count = 1;
        data[tmp2[0]][size[tmp2[0]]] = data[tmp1[0]][tmp1[1]];
        size[tmp2[0]]++;
        for (int i = tmp1[1] + 1; i < size[tmp1[0]]; i++) {
          int tmp = data[tmp1[0]][i];
          data[tmp][size[tmp]] = tmp;
          size[tmp]++;
          count++;
        }
        for (int i = 0; i < count; i++) {
          size[tmp1[0]]--;
        }
      }
    }

    sort(size, size + m + 1);

    for (int i = 0; i <= m; i++) {
      if (size[i] == 0)
        continue;
      cout << size[i] << endl;
    }
    cout << "end" << endl;
  }
  return 0;
}