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

#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

#define DEBUG 0

using namespace std;

int max(int a, int b) { return a > b ? a : b; }

int main() {
  int n;
  while (cin >> n) {
    if (n == 0)
      break;

    vector<int> *person;
    map<int, vector<int>> dayToPerson;
    person = new vector<int>[n];

    int first = 0;
    int maxDay = 0;
    int ansFlag = 0;

    for (int i = 0; i < n; i++) {
      int tmp;
      cin >> tmp;
      for (int j = 0; j < tmp; j++) {
        int tmp2;
        cin >> tmp2;
        if (i == 0 && j == 0) {
          first = tmp2;
        }
        person[i].push_back(tmp2);
        dayToPerson[tmp2].push_back(i);
        maxDay = max(tmp2, maxDay);
      }
    }

    for (int day = 1; day <= 30; day++) {
      queue<int> personQueue;
      priority_queue<int> beforeDay;
      int *flag;
      flag = new int[n];
      for (int i = 0; i < n; i++) {
        flag[i] = 0;
      }

      int nowDay = day;
      if (DEBUG)
        cout << "NOWDAY:" << nowDay << endl;
      while (nowDay > 0) {
        for (int i = 0; i < dayToPerson[nowDay].size(); i++) {
          if (flag[dayToPerson[nowDay].at(i)] == 0) {
            personQueue.push(dayToPerson[nowDay].at(i));
            flag[dayToPerson[nowDay].at(i)] = 1;
          }
        }

        while (personQueue.size() != 0) {
          int nowPerson = personQueue.front();
          personQueue.pop();
          if (DEBUG)
            cout << "NOW Person:" << nowPerson << endl;

          for (int i = 0; i < person[nowPerson].size(); i++) {
            if (nowDay > person[nowPerson].at(i)) {

              if (DEBUG)
                cout << "beforeDay" << person[nowPerson].at(i) << "input"
                     << endl;
              beforeDay.push(person[nowPerson].at(i));
            }
          }
        }

        int changeFlag = 0;
        if (beforeDay.size() == 0) {
          break;
        }
        while (beforeDay.size() != 0 && nowDay == beforeDay.top()) {
          beforeDay.pop();
          if (beforeDay.size() == 0) {
            changeFlag = 1;
            break;
          }
        }
        if (changeFlag == 1) {
          break;
        }
        nowDay = beforeDay.top();
        beforeDay.pop();
      }
      ansFlag = 0;
      for (int i = 0; i < n; i++) {
        if (flag[i] == 0) {
          ansFlag = 1;
          break;
        }
      }
      if (ansFlag == 0) {
        cout << day << endl;
        ansFlag = 2;
        break;
      }
    }
    if (ansFlag != 2) {
      cout << -1 << endl;
    }
  }

  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

using namespace std;

int main() {
  int n;
  while (cin >> n) {
    if (n == 0)
      break;

    vector<int> *person;
    map<int, vector<int>> dayToPerson;
    person = new vector<int>[n];

    int first = 0;

    for (int i = 0; i < n; i++) {
      int tmp;
      cin >> tmp;
      for (int j = 0; j < tmp; j++) {
        int tmp2;
        cin >> tmp2;
        if (i == 0 && j == 0) {
          first = tmp2;
        }
        person[i].push_back(tmp2);
        dayToPerson[tmp2].push_back(i);
      }
    }

    //??\\?????¨????????¢????????\\??????
    map<int, vector<int>>::iterator it = dayToPerson.begin();
    int day = 1;
    while (it != dayToPerson.end()) {
      // cout<<day<<"day"<<" ";
      for (int i = 0; i < (*it).second.size(); i++) {
        //	cout<<(*it).second.at(i)<<" ";
      }
      day++;
      // cout<<endl;
      it++;
    }

    map<int, set<int>> personConnect;

    it = dayToPerson.begin();

    day = 1;
    while (it != dayToPerson.end()) {
      // cout<<day<<"Day"<<endl;
      for (int i = 0; i < (*it).second.size(); i++) {
        int now = (*it).second.at(i);
        // cout<<"Now:"<<now<<" ";
        for (int j = 0; j < (*it).second.size(); j++) {
          int next = (*it).second.at(j);
          // cout<<next<<" ";
          personConnect[now].insert(next);
        }
        // cout<<endl;
      }

      int *flag;
      flag = new int[n + 1];
      queue<int> q;
      q.push(first);
      flag[first] = 1;
      for (int i = 0; i < n + 1; i++) {
        flag[i] = 0;
      }
      int endFlag = 0;
      while (q.size() != 0) {
        int now = q.front();
        q.pop();
        // cout<<now<<"start ";

        set<int>::iterator sIt = personConnect[now].begin();
        while (sIt != personConnect[now].end()) {
          // cout<<(*sIt)<<" ";
          if (flag[(*sIt)] == 0) {
            q.push((*sIt));
            flag[(*sIt)]++;
          }
          sIt++;
        }
        // cout<<endl;

        int ansFlag = 0;
        for (int i = 0; i < n; i++) {
          if (flag[i] == 0) {
            ansFlag = 1;
            break;
          }
        }
        endFlag = 0;
        if (ansFlag == 0) {
          cout << day << endl;
          endFlag = 1;
          break;
        }
      }
      if (endFlag == 1) {
        break;
      }
      day++;
      it++;
      if (day >= 31) {
        cout << -1 << endl;
        break;
      }
    }
  }
  return 0;
}