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

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
const ld eps = 1e-9;

//// < "d:\\d_download\\visual studio
///2015\\projects\\programing_contest_c++\\debug\\a.txt" > "d:\\d_download\\visual
///studio 2015\\projects\\programing_contest_c++\\debug\\b.txt"

int main() {
  while (1) {
    int M;
    cin >> M;
    if (!M)
      break;
    vector<vector<int>> piles(M);
    for (int i = 0; i < piles.size(); ++i) {
      piles[i] = vector<int>(1, i + 1);
    }
    while (1) {
      int a, b;
      cin >> a >> b;
      if (!a)
        break;
      pair<int, int> ap;
      for (int i = 0; i < piles.size(); ++i) {
        auto pile(piles[i]);
        auto it = find(pile.begin(), pile.end(), a);
        if (it != pile.end()) {
          ap = make_pair(i, it - pile.begin());
        }
      }
      if (!b) {
        if (ap.second == 0)
          continue;
        auto &apile(piles[ap.first]);
        while (apile.back() != a) {
          int n = apile.back();

          apile.pop_back();
          if (n != a) {

            piles.push_back(vector<int>(1, n));
          }
        }
        apile.pop_back();
        piles.push_back(vector<int>(1, a));
      } else {
        pair<int, int> bp;

        for (int i = 0; i < piles.size(); ++i) {
          auto pile(piles[i]);
          auto jt = find(pile.begin(), pile.end(), b);
          if (jt != pile.end()) {
            bp = make_pair(i, jt - pile.begin());
          }
        }

        if (ap.first == bp.first) {
          if (ap.second >= bp.second) {
            continue;
          } else {
            auto &apile(piles[ap.first]);
            while (apile.back() != a) {
              int n = apile.back();

              apile.pop_back();
              if (n != b) {

                piles.push_back(vector<int>(1, n));
              }
            }
            apile.pop_back();
            piles.push_back(vector<int>{b, a});
          }
        } else {
          auto &apile(piles[ap.first]);
          auto &bpile(piles[bp.first]);
          while (apile.back() != a) {
            int n = apile.back();

            apile.pop_back();
            if (n != a) {

              piles.push_back(vector<int>(1, n));
            }
          }
          apile.pop_back();
          bpile.push_back(a);
        }
      }

      {

        vector<vector<int>> nextpiles;
        for (auto &pile : piles) {
          if (!pile.empty())
            nextpiles.push_back(pile);
        }
        piles = nextpiles;
      }
    }
    vector<int> nums;
    for (auto pile : piles) {
      nums.push_back(pile.size());
    }
    sort(nums.begin(), nums.end());
    for (auto n : nums)
      cout << n << endl;
    cout << "end" << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
const ld eps = 1e-9;

//// < "d:\\d_download\\visual studio
///2015\\projects\\programing_contest_c++\\debug\\a.txt" > "d:\\d_download\\visual
///studio 2015\\projects\\programing_contest_c++\\debug\\b.txt"

int main() {
  while (1) {
    int M;
    cin >> M;
    if (!M)
      break;
    vector<vector<int>> piles(M);
    for (int i = 0; i < piles.size(); ++i) {
      piles[i] = vector<int>(1, i + 1);
    }
    while (1) {
      int a, b;
      cin >> a >> b;
      if (!a)
        break;
      pair<int, int> ap;
      for (int i = 0; i < piles.size(); ++i) {
        auto pile(piles[i]);
        auto it = find(pile.begin(), pile.end(), a);
        if (it != pile.end()) {
          ap = make_pair(i, it - pile.begin());
        }
      }
      if (!b) {
        auto &apile(piles[ap.first]);
        while (apile.back() != a) {
          int n = apile.back();

          apile.pop_back();
          if (n != a) {

            piles.push_back(vector<int>(1, n));
          }
        }
        apile.pop_back();
        piles.push_back(vector<int>(1, a));
      } else {
        pair<int, int> bp;

        for (int i = 0; i < piles.size(); ++i) {
          auto pile(piles[i]);
          auto jt = find(pile.begin(), pile.end(), b);
          if (jt != pile.end()) {
            bp = make_pair(i, jt - pile.begin());
          }
        }

        if (ap.first == bp.first) {
          if (ap.second >= bp.second) {
            continue;
          } else {
            auto &apile(piles[ap.first]);
            while (apile.back() != a) {
              int n = apile.back();

              piles.pop_back();
              if (n != b) {

                piles.push_back(vector<int>(1, n));
              }
            }
            apile.pop_back();
            apile.push_back(b);
            apile.push_back(a);
          }
        } else {
          auto &apile(piles[ap.first]);
          auto &bpile(piles[bp.first]);
          while (apile.back() != a) {
            int n = apile.back();

            apile.pop_back();
            if (n != a) {

              piles.push_back(vector<int>(1, n));
            }
          }
          apile.pop_back();
          bpile.push_back(a);
        }
      }

      {

        vector<vector<int>> nextpiles;
        for (auto &pile : piles) {
          if (!pile.empty())
            nextpiles.push_back(pile);
        }
        piles = nextpiles;
      }
    }
    vector<int> nums;
    for (auto pile : piles) {
      nums.push_back(pile.size());
    }
    sort(nums.begin(), nums.end());
    for (auto n : nums)
      cout << n << endl;
    cout << "end" << endl;
  }
  return 0;
}