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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define MOD 1000000007
#define ADD(X, Y) ((X) = ((X) + (Y) % MOD) % MOD)
typedef long long i64;
typedef vector<int> ivec;
typedef vector<string> svec;
typedef pair<int, int> pi;
int dd[] = {0, 1, 0, -1, 0};

int main() {
  // string s = "";
  while (true) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    ivec A[31];
    //??\\?????????1?§?????????????????????????1?§???????
    for (int i = 1; i <= n; i++) //????????????
    {
      int f;
      cin >> f;
      for (int j = 0; j < f; j++) {
        int k;
        cin >> k;
        A[k].push_back(i);
      }
    }
    if (n == 1) {
      for (int i = 1; i < 31; i++) {
        if (A[i].size() > 0) {
          cout << i << endl;
          // s += to_string(i) + "\\n";
          break;
        }
      }
      continue;
    }

    set<int> ds[100]; //????????°
    for (int i = 1; i <= n; i++) {
      ds[i].insert(i);
    }
    int ok = 0;

    for (int k = 1; k < 31; k++) //??\\?????????
    {
      set<int> today;
      for (auto i : A[k]) //????????????
      {
        for (auto j : ds[i]) {
          today.insert(j);
        }
      }
      if (today.size() == n) {
        cout << k << endl;
        // s += to_string(i) + "\\n";
        ok = 1;
        break;
      }
      for (auto i : A[k]) {
        ds[i] = today;
      }
    }
    if (!ok) {
      cout << -1 << endl;
      // s += to_string(-1) + "\\n";
    }
  }

  // cout << s;

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define MOD 1000000007
#define ADD(X, Y) ((X) = ((X) + (Y) % MOD) % MOD)
typedef long long i64;
typedef vector<int> ivec;
typedef vector<string> svec;
typedef pair<int, int> pi;
int dd[] = {0, 1, 0, -1, 0};

int main() {
  // string s = "";
  while (true) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    ivec A[31];
    for (int i = 0; i < n; i++) {
      int f;
      cin >> f;
      for (int j = 0; j < f; j++) {
        int k;
        cin >> k;
        A[k].push_back(i);
      }
    }
    if (n == 1) {
      for (int i = 0; i < 31; i++) {
        if (A[i].size() > 0) {
          cout << i << endl;
          // s += to_string(i) + "\\n";
          break;
        }
      }
      continue;
    }

    set<int> ds[31];
    for (int i = 0; i < 31; i++) {
      ds[i].insert(i);
    }
    int ok = 0;
    for (int i = 1; i < 31; i++) {
      set<int> today;
      for (auto j : A[i]) {
        for (auto k : ds[j]) {
          today.insert(k);
        }
      }
      if (today.size() == n) {
        cout << i << endl;
        // s += to_string(i) + "\\n";
        ok = 1;
        break;
      }
      for (auto j : A[i]) {
        ds[j] = today;
      }
    }
    if (!ok) {
      cout << -1 << endl;
      // s += to_string(-1) + "\\n";
    }
  }

  // cout << s;

  return 0;
}