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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
template <class T> inline bool rd(T &ret) {
  char c;
  int sgn;
  if (c = getchar(), c == EOF)
    return 0;
  while (c != '-' && (c < '0' || c > '9'))
    c = getchar();
  sgn = (c == '-') ? -1 : 1;
  ret = (c == '-') ? 0 : (c - '0');
  while (c = getchar(), c >= '0' && c <= '9')
    ret = ret * 10 + (c - '0');
  ret *= sgn;
  return 1;
}
template <class T> inline void pt(T x) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  if (x > 9)
    pt(x / 10);
  putchar(x % 10 + '0');
}
using namespace std;
const int inf = (int)1e8;
const int N = 55;
typedef pair<int, int> pii;

int n, m, c;

deque<int> Q, q[15];
deque<int>::iterator it;
vector<int> G[105];
int id[105];
void input() {
  while (!Q.empty())
    Q.pop_front();
  for (int i = 1, num, j; i <= n; i++) {
    G[i].clear();
    rd(num);
    while (num--) {
      rd(j);
      G[i].push_back(j);
    }
    reverse(G[i].begin(), G[i].end());
    if (G[i].size())
      Q.push_back(i);
  }
  for (int i = 1; i <= m; i++)
    while (!q[i].empty())
      q[i].pop_front();
  for (int i = 1; i <= 100; i++)
    id[i] = m + 1;
}
int putbook(int book, int from) {
  for (int i = from; i <= m; i++) {
    if (q[i].size() < c) {
      q[i].push_back(book);
      id[book] = i;
      return i;
    }
  }
  id[book] = m + 1;
  return m + 1;
}
int takebook(int book) {
  if (id[book] == m + 1)
    return m + 1;
  for (it = q[id[book]].begin(); it != q[id[book]].end(); it++)
    if (*it == book) {
      q[id[book]].erase(it);
      break;
    }
  return id[book];
}
int main() {
  while (~scanf("%d %d %d", &m, &c, &n), n + c + m) {
    input();
    int ans = 0;
    while (!Q.empty()) {
      int stu = Q.front();
      Q.pop_front();
      int book = G[stu][G[stu].size() - 1];
      G[stu].erase(G[stu].end() - 1, G[stu].end());
      if (G[stu].size())
        Q.push_back(stu);
      ans += takebook(book);
      ans += putbook(book, 1);
      if (id[book] != 1) {
        int pre = q[1].front();
        q[1].pop_front();
        ans++;
        ans += putbook(pre, 2);
        ans += takebook(book);
        ans += putbook(book, 1);
      }
    }
    pt(ans);
    puts("");
  }
  return 0;
}

/*

9 9
*#**#**#*
*#**#**#*
*#**#**#*
*#**.**#*
*#*#.#*#*
*$##*##$*
*#*****#*
*.#.#.#.*
*********
3 3
*$*
*#*
*$*


*/

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
template <class T> inline bool rd(T &ret) {
  char c;
  int sgn;
  if (c = getchar(), c == EOF)
    return 0;
  while (c != '-' && (c < '0' || c > '9'))
    c = getchar();
  sgn = (c == '-') ? -1 : 1;
  ret = (c == '-') ? 0 : (c - '0');
  while (c = getchar(), c >= '0' && c <= '9')
    ret = ret * 10 + (c - '0');
  ret *= sgn;
  return 1;
}
template <class T> inline void pt(T x) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  if (x > 9)
    pt(x / 10);
  putchar(x % 10 + '0');
}
using namespace std;
const int inf = (int)1e8;
const int N = 55;
typedef pair<int, int> pii;

int n, m, c;

deque<int> Q, q[55];
deque<int>::iterator it;
vector<int> G[N];
int id[105];
void input() {
  while (!Q.empty())
    Q.pop_front();
  for (int i = 1, num, j; i <= n; i++) {
    G[i].clear();
    rd(num);
    while (num--) {
      rd(j);
      G[i].push_back(j);
    }
    reverse(G[i].begin(), G[i].end());
    Q.push_back(i);
  }
  for (int i = 1; i <= m + 1; i++)
    while (!q[i].empty())
      q[i].pop_front();
  for (int i = 1; i <= 100; i++)
    id[i] = m + 1;
}
int putbook(int book, int from) {
  for (int i = from; i <= m; i++) {
    if (q[i].size() < c) {
      q[i].push_back(book);
      id[book] = i;
      return i;
    }
  }
  id[book] = m + 1;
  return m + 1;
}
int takebook(int book) {
  if (id[book] == m + 1)
    return m + 1;
  for (it = q[id[book]].begin(); it != q[id[book]].end(); it++)
    if (*it == book) {
      q[id[book]].erase(it);
      break;
    }
  return id[book];
}
int main() {
  while (~scanf("%d %d %d", &m, &c, &n), n + c + m) {
    input();
    int ans = 0;
    while (!Q.empty()) {
      int stu = Q.front();
      Q.pop_front();
      int book = G[stu][G[stu].size() - 1];
      G[stu].erase(G[stu].end() - 1, G[stu].end());
      if (G[stu].size())
        Q.push_back(stu);
      ans += takebook(book);
      ans += putbook(book, 1);
      if (id[book] != 1) {
        int pre = q[1].front();
        q[1].pop_front();
        ans++;
        ans += putbook(pre, 2);
        ans += takebook(book);
        ans += putbook(book, 1);
      }
    }
    pt(ans);
    puts("");
  }
  return 0;
}

/*

9 9
*#**#**#*
*#**#**#*
*#**#**#*
*#**.**#*
*#*#.#*#*
*$##*##$*
*#*****#*
*.#.#.#.*
*********
3 3
*$*
*#*
*$*


*/