Problem p03443 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
int ans[202020];
void no() {
  printf("No\n");
  exit(0);
}
int zzz[202020];
int ai[202020];
int dd[202020];
void check(int num, string s) {
  fill(zzz, zzz + num * 2, -1);
  for (int i = 0; i < num * 2; i++) {
    if (zzz[ans[i]] == -1)
      zzz[ans[i]] = i;
    else
      ai[i] = zzz[ans[i]], ai[zzz[ans[i]]] = i;
  }
  int now = 0;
  for (;;) {
    if (now == num * 2)
      break;
    now = ai[now];
    dd[now] = 1;
    now++;
  }
  for (int i = 1; i < s.size() - 1; i++)
    if ((s[i] == '0') != (dd[i - 1] == 0))
      abort();
}
typedef pair<int, int> pii;
int main() {
  int num;
  scanf("%d", &num);
  string s;
  cin >> s;
  s = "1" + s + "1";
  vector<int> v;
  for (int i = 0; i < num * 2; i++)
    if (s[i] == '1' && s[i + 1] == '1')
      v.push_back(i);
  if (v.size() % 2 == 1)
    no();
  if (v.size() % 4 == 0) {
    int pt = 0;
    for (int i = 0; i < v.size() / 2; i++)
      ans[v[i]] = ans[v[i + v.size() / 2]] = pt++;
    vector<int> a, b, c;
    for (int i = 0; i < num * 2; i++) {
      if (s[i] == '1' && s[i + 1] == '0')
        a.push_back(i);
      if (s[i] == '0' && s[i + 1] == '1')
        b.push_back(i);
      if (s[i] == '0' && s[i + 1] == '0')
        c.push_back(i);
    }
    for (int i = 0; i < a.size(); i++)
      ans[a[i]] = ans[b[i]] = pt++;
    for (int i = 0; i < c.size() / 2; i++)
      ans[c[i * 2]] = ans[c[i * 2 + 1]] = pt++;
    check(num, s);
    printf("Yes\n");
    for (int i = 0; i < num * 2; i++)
      printf("%d ", ans[i] + 1);
    printf("\n");
  } else {
    int pt = 0;
    for (int i = 0; i < v.size() / 2; i++)
      ans[v[i]] = ans[v[i + v.size() / 2]] = pt++;
    vector<int> a, b, c;
    for (int i = 0; i < num * 2; i++) {
      if (s[i] == '1' && s[i + 1] == '0')
        a.push_back(i);
      if (s[i] == '0' && s[i + 1] == '1')
        b.push_back(i);
      if (s[i] == '0' && s[i + 1] == '0')
        c.push_back(i);
    }
    for (int i = 0; i < a.size(); i++)
      ans[a[i]] = ans[b[i]] = pt++;
    for (int i = 0; i < c.size() / 2; i++)
      ans[c[i * 2]] = ans[c[i * 2 + 1]] = pt++;

    int now = 0, mode = -1;
    int lb = s.size() - 1, ub = s.size() - 1;
    for (int i = 0; i < s.size(); i++) {
      if (s[i] == '1')
        now++;
      else {
        if (mode == -1)
          mode = 0;
        else if (now % 2 == 0 && now > 0) {
          lb = i - now, ub = i;
          break;
        }
        now = 0;
      }
    }
    if (ub == s.size() - 1) {
      now = 0, mode = -1;
      vector<pii> zz;
      for (int i = 0; i <= num * 4; i++) {
        if (s[i % (num * 2)] == '1')
          now++;
        else {
          if (mode == -1)
            mode = 0;
          else if (now > 2) {
            zz.push_back(make_pair(i - now, i));
          }
          now = 0;
        }
      }
      if (zz.size() <= 2)
        no();
      swap(ans[zz[0].first], ans[zz[0].first + 1]);
      lb = zz[1].first, ub = zz[1].second % (num * 2);
    }
    int t;
    for (int i = 0; i < num * 2; i++)
      if (ub - 1 != i && ans[ub - 1] == ans[i])
        t = i;
    swap(ans[lb - 1], ans[t]);
    check(num, s);
    printf("Yes\n");
    for (int i = 0; i < num * 2; i++)
      printf("%d ", ans[i] + 1);
    printf("\n");
  }
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
int ans[202020];
void no() {
  printf("No\n");
  exit(0);
}
int zzz[202020];
int ai[202020];
int dd[202020];
void check(int num, string s) {
  fill(zzz, zzz + num * 2, -1);
  for (int i = 0; i < num * 2; i++) {
    if (zzz[ans[i]] == -1)
      zzz[ans[i]] = i;
    else
      ai[i] = zzz[ans[i]], ai[zzz[ans[i]]] = i;
  }
  int now = 0;
  for (;;) {
    if (now == num * 2)
      break;
    now = ai[now];
    dd[now] = 1;
    now++;
  }
  for (int i = 1; i < s.size() - 1; i++)
    if ((s[i] == '0') != (dd[i - 1] == 0))
      abort();
}
typedef pair<int, int> pii;
int main() {
  int num;
  scanf("%d", &num);
  string s;
  cin >> s;
  s = "1" + s + "1";
  vector<int> v;
  for (int i = 0; i < num * 2; i++)
    if (s[i] == '1' && s[i + 1] == '1')
      v.push_back(i);
  if (v.size() % 2 == 1)
    no();
  if (v.size() % 4 == 0) {
    int pt = 0;
    for (int i = 0; i < v.size() / 2; i++)
      ans[v[i]] = ans[v[i + v.size() / 2]] = pt++;
    vector<int> a, b, c;
    for (int i = 0; i < num * 2; i++) {
      if (s[i] == '1' && s[i + 1] == '0')
        a.push_back(i);
      if (s[i] == '0' && s[i + 1] == '1')
        b.push_back(i);
      if (s[i] == '0' && s[i + 1] == '0')
        c.push_back(i);
    }
    for (int i = 0; i < a.size(); i++)
      ans[a[i]] = ans[b[i]] = pt++;
    for (int i = 0; i < c.size() / 2; i++)
      ans[c[i * 2]] = ans[c[i * 2 + 1]] = pt++;
    check(num, s);
    printf("Yes\n");
    for (int i = 0; i < num * 2; i++)
      printf("%d ", ans[i] + 1);
    printf("\n");
  } else {
    int pt = 0;
    for (int i = 0; i < v.size() / 2; i++)
      ans[v[i]] = ans[v[i + v.size() / 2]] = pt++;
    vector<int> a, b, c;
    for (int i = 0; i < num * 2; i++) {
      if (s[i] == '1' && s[i + 1] == '0')
        a.push_back(i);
      if (s[i] == '0' && s[i + 1] == '1')
        b.push_back(i);
      if (s[i] == '0' && s[i + 1] == '0')
        c.push_back(i);
    }
    for (int i = 0; i < a.size(); i++)
      ans[a[i]] = ans[b[i]] = pt++;
    for (int i = 0; i < c.size() / 2; i++)
      ans[c[i * 2]] = ans[c[i * 2 + 1]] = pt++;

    int now = 0, mode = -1;
    int lb = s.size() - 1, ub = s.size() - 1;
    for (int i = 0; i < s.size(); i++) {
      if (s[i] == '1')
        now++;
      else {
        if (mode == -1)
          mode = 0;
        else if (now % 2 == 0 && now > 0) {
          lb = i - now, ub = i;
          break;
        }
        now = 0;
      }
    }
    if (ub == s.size() - 1) {
      now = 0, mode = -1;
      vector<pii> zz;
      for (int i = 0; i <= num * 4; i++) {
        if (s[i % (num * 2)] == '1')
          now++;
        else {
          if (mode == -1)
            mode = 0;
          else if (now > 2) {
            zz.push_back(make_pair(i - now, i));
          }
          now = 0;
        }
      }
      if (zz.size() <= 2)
        no();
      swap(ans[zz[0].second - 2], ans[zz[1].first % (num * 2)]);
      lb = zz[1].first, ub = zz[1].second % (num * 2);
    }
    int t;
    for (int i = 0; i < num * 2; i++)
      if (ub - 1 != i && ans[ub - 1] == ans[i])
        t = i;
    swap(ans[lb - 1], ans[t]);
    check(num, s);
    printf("Yes\n");
    for (int i = 0; i < num * 2; i++)
      printf("%d ", ans[i] + 1);
    printf("\n");
  }
}