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


// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1124

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <numeric>
#include <vector>

#define ALL(v) (v).begin(), (v).end()
#define REP(i, p, n) for (int i = p; i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define dump(a) (cerr << #a << "=" << (a) << endl)
#define DUMP(list)                                                             \\
  cout << "{ ";                                                                \\
  for (auto nth : list) {                                                      \\
    cout << nth << " ";                                                        \\
  }                                                                            \\
  cout << "}" << endl;

using namespace std;

int main() {
  int N, Q, M;

  while (cin >> N >> Q) {
    if (N == 0 && Q == 0) {
      break;
    }

    vector<int> date;
    rep(nth, N) {
      cin >> M;

      int tmp;
      rep(i, M) {
        cin >> tmp;
        date.push_back(tmp);
      }
    }

    if (date.size() == 0) {
      date.push_back(0);
    }

    int MAX = *max_element(ALL(date));
    int count = 0;
    bool flag = false;
    for (int m = N; m >= Q; --m) {
      REP(num, 1, MAX + 1) {
        count = 0;
        rep(i, date.size()) {
          if (date[i] == num) {
            count++;
          }
          if (count == m) {
            cout << num << endl;
            flag = true;
            goto end;
          }
        }
      }
    }
  end:
    if (!flag) {
      cout << 0 << endl;
    }
  }
  return 0;
}

Error: 0

Fixed submission:


// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1124

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <numeric>
#include <vector>

#define ALL(v) (v).begin(), (v).end()
#define REP(i, p, n) for (int i = p; i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define dump(a) (cerr << #a << "=" << (a) << endl)
#define DUMP(list)                                                             \\
  cout << "{ ";                                                                \\
  for (auto nth : list) {                                                      \\
    cout << nth << " ";                                                        \\
  }                                                                            \\
  cout << "}" << endl;

using namespace std;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, Q, M;

  while (cin >> N >> Q) {
    if (N == 0 && Q == 0) {
      break;
    }

    vector<int> date;
    rep(nth, N) {
      cin >> M;

      int tmp;
      rep(i, M) {
        cin >> tmp;
        date.push_back(tmp);
      }
    }

    int MAX = *max_element(ALL(date));
    int count = 0;
    bool flag = false;
    for (int m = N; m >= Q; --m) {
      REP(num, 1, MAX + 1) {
        count = 0;
        rep(i, date.size()) {
          if (date[i] == num) {
            count++;
          }
          if (count == m) {
            cout << num << endl;
            flag = true;
            goto end;
          }
        }
      }
    }
  end:
    if (!flag) {
      cout << 0 << endl;
    }
  }
  return 0;
}