Problem p03996 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define F first
#define S second
#define SS stringstream
#define sqr(x) ((x) * (x))
#define m0(x) memset(x, 0, sizeof(x))
#define m1(x) memset(x, 63, sizeof(x))
#define CC(x) cout << (x) << endl
#define pw(x) (1ll << (x))
#define buli(x) __builtin_popcountll(x)
#define forn(i, n) for (int i = 0; (i) < (n); ++i)
#define M 1000000007
#define N 211111

#define TASK "1"

using namespace std;

int n, m, q;
vector<int> need[N], ans[N];

int used[N];
int t[N];
int was[N];

vector<int> dop(vector<int> a) {
  for (int i = 1; i <= m; i++)
    used[i] = 0;
  for (int i = 0; i < a.size(); i++)
    used[a[i]] = 1;
  for (int i = 1; i <= m; i++)
    if (!used[i])
      a.pb(i);
  return a;
}

int main() {
#ifdef home
  freopen(TASK ".in", "r", stdin);
  freopen(TASK ".out", "w", stdout);
#endif
  ios::sync_with_stdio(false);
  cin >> n >> m >> q;
  for (int i = 0; i < q; i++) {
    cin >> t[i];
  }
  for (int i = 1; i < n; i++)
    need[0].pb(i);

  for (int i = q - 1; i >= 0; i--) {
    int g = t[i];

    if (was[g] == 0) {
      ans[0].pb(g);
      was[g] = 1;

      for (int h : need[0])
        need[g].pb(h);
      need[0].clear();

      continue;
    }
    /*		if (need[g].size() == 0) {
                            cout << "No\n";
                            return 0;
                    }*/

    int t = need[g].back();
    need[g].pop_back();

    ans[t].pb(g);

    if (ans[t].size() < m) {
      if (ans[0].size() == ans[t].size())
        need[0].pb(t);
      else
        need[ans[0][ans[t].size()]].pb(t);
    }
  }
  //	for (int i = 0; i < n; i++) {
  //		for (int j = 0; j < ans[i].size(); j++) cout << ans[i][j] << "
  //"; 		cout << endl;
  //	}
  int x = -1;
  for (int i = 0; i < n; i++)
    if (x == -1 || ans[i].size() < ans[x].size())
      x = i;
  if (dop(ans[0]) != dop(ans[x]))
    cout << "No\n";
  else
    cout << "Yes\n" << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define F first
#define S second
#define SS stringstream
#define sqr(x) ((x) * (x))
#define m0(x) memset(x, 0, sizeof(x))
#define m1(x) memset(x, 63, sizeof(x))
#define CC(x) cout << (x) << endl
#define pw(x) (1ll << (x))
#define buli(x) __builtin_popcountll(x)
#define forn(i, n) for (int i = 0; (i) < (n); ++i)
#define M 1000000007
#define N 211111

#define TASK "1"

using namespace std;

int n, m, q;
vector<int> need[N], ans[N];

int used[N];
int t[N];
int was[N];

vector<int> dop(vector<int> a) {
  for (int i = 1; i <= m; i++)
    used[i] = 0;
  for (int i = 0; i < a.size(); i++)
    used[a[i]] = 1;
  for (int i = 1; i <= m; i++)
    if (!used[i])
      a.pb(i);
  return a;
}

int main() {
#ifdef home
  freopen(TASK ".in", "r", stdin);
  freopen(TASK ".out", "w", stdout);
#endif
  ios::sync_with_stdio(false);
  cin >> n >> m >> q;
  for (int i = 0; i < q; i++) {
    cin >> t[i];
  }
  for (int i = 1; i < n; i++)
    need[0].pb(i);

  for (int i = q - 1; i >= 0; i--) {
    int g = t[i];

    if (was[g] == 0) {
      ans[0].pb(g);
      was[g] = 1;

      for (int h : need[0])
        need[g].pb(h);
      need[0].clear();

      continue;
    }
    if (need[g].size() == 0) {
      continue;
    }

    int t = need[g].back();
    need[g].pop_back();

    ans[t].pb(g);

    if (ans[t].size() < m) {
      if (ans[0].size() == ans[t].size())
        need[0].pb(t);
      else
        need[ans[0][ans[t].size()]].pb(t);
    }
  }
  //	for (int i = 0; i < n; i++) {
  //		for (int j = 0; j < ans[i].size(); j++) cout << ans[i][j] << "
  //"; 		cout << endl;
  //	}
  int x = -1;
  for (int i = 0; i < n; i++)
    if (x == -1 || ans[i].size() < ans[x].size())
      x = i;
  if (dop(ans[0]) != dop(ans[x]))
    cout << "No\n";
  else
    cout << "Yes\n" << endl;

  return 0;
}