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

#include <cctype>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>

#include <algorithm>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)

int main() {
  int N, Q;
  // 各テストケースについて
  while (cin >> N >> Q) {
    if (N == 0 && Q == 0) {
      break;
    }
    vector<vector<int>> members;
    int max_day = 0;
    // 各メンバーの情報について
    rep(n, N) {
      int m;
      cin >> m;
      vector<int> days;
      // 各日にちに対して
      rep(i, m) {
        int tmp;
        cin >> tmp;
        days.push_back(tmp);
        if (max_day < tmp) {
          max_day = tmp;
        }
      }
      members.push_back(days);
    }

    vector<int> attend;
    attend.resize(max_day + 1, 0);
    int index = 0;
    rep(i, max_day + 1) {
      rep(j, N) {
        rep(h, members[j].size()) {
          if (members[j][h] == i) {
            attend[i]++;
          }
          if (attend[i] > attend[index]) {
            index = i;
          }
        }
      }
    }
    // cout << "atend[]" << endl;
    // rep(i, attend.size() ) { cout << attend[i] << " "; }
    // cout << " " << endl;

    sort(attend.rbegin(), attend.rend());

    if (attend[0] < Q) {
      index = 0;
    }
    cout << index << endl;
  }
  return 0;
}

int min(int a, int b) {
  if (a < b) {
    return a;
  } else {
    return b;
  }
}

Error: 0

Fixed submission:

#include <cctype>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>

#include <algorithm>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)

typedef pair<int, int> P;

class Coordinates {
public:
  int x;
  int y;

  Coordinates operator+(Coordinates p) {
    Coordinates tmp;
    tmp.x = x + p.x;
    tmp.y = y + p.y;
    return tmp;
  }

  Coordinates() {
    x = 0;
    y = 0;
  };
  Coordinates(int a, int b) {
    x = a;
    y = b;
  }

  friend bool operator==(const Coordinates a, const Coordinates b) {
    return (a.x == b.x && a.y == b.y);
  }
  friend bool operator<(const Coordinates a, const Coordinates b) {
    return (a.x < b.x && a.y < b.y);
  }
  friend bool operator<=(const Coordinates a, const Coordinates b) {
    return (a.x <= b.x && a.y <= b.y);
  }
};

typedef Coordinates Coo;

Coo ld[4] = {Coo(0, -1), Coo(1, 0), Coo(0, 1), Coo(-1, 0)};
Coo rd[4] = {Coo(0, -1), Coo(-1, 0), Coo(0, 1), Coo(1, 0)};
int main() {
  int N, Q;
  // 各テストケースについて
  while (cin >> N >> Q) {
    if (N == 0 && Q == 0) {
      break;
    }
    vector<vector<int>> members;
    int max_day = 0;
    // 各メンバーの情報について
    rep(n, N) {
      int m;
      cin >> m;
      vector<int> days;
      rep(i, m) {
        int tmp;
        cin >> tmp;
        days.push_back(tmp);
        if (max_day < tmp) {
          max_day = tmp;
        }
      }
      members.push_back(days);
    }

    vector<int> attend;
    attend.resize(max_day, 0);
    int index = 0;
    rep(i, max_day) {
      rep(j, N) {
        rep(h, members[j].size()) {
          if (members[j][h] == i) {
            attend[i]++;
          }
          if (attend[i] > attend[index]) {
            index = i;
          }
        }
      }
    }
    // cout << "atend[]" << endl;
    // rep(i, attend.size() ) { cout << attend[i] << " "; }
    // cout << " " << endl;

    sort(attend.rbegin(), attend.rend());

    if (attend[0] < Q) {
      index = 0;
    }
    cout << index << endl;
  }
  return 0;
}

int min(int a, int b) {
  if (a < b) {
    return a;
  } else {
    return b;
  }
}