Problem p03256 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define fr(i, n) for (int i = 0; i < n; i++)
#define pb push_back
#define frab(i, a, b) for (int i = a; i < b; i++)

using namespace std;

typedef long long ll;
typedef long double ld;

const ll MOD = 1e9 + 7;
const ld EPS = 1e-9;
const int N = 1e5 + 10;

vector<int> g[N];
char s[N];
int cnta[N], cntb[N];

void eraseA(int v, set<pair<int, int>> &white, int *cnta) {
  for (auto u : g[v]) {
    if (white.count({cnta[u], u})) {
      white.erase(white.find({cnta[u], u}));
      cnta[u]--;
      white.insert({cnta[u], u});
    }
  }
}

int main() {
  // freopen("a.in", "r", stdin);
  int n, m;
  scanf("%d %d", &n, &m);
  scanf("%s", &s);
  fr(i, m) {
    int v, u;
    scanf("%d %d", &v, &u);
    --v, --u;
    g[v].pb(u);
    g[u].pb(v);
    if (s[u] == 'A')
      cnta[v]++;
    else
      cntb[v]++;
    if (s[v] == 'A')
      cnta[u]++;
    else
      cntb[u]++;
  }
  set<pair<int, int>> white, black;
  fr(i, n) {
    white.insert({cnta[i], i});
    black.insert({cntb[i], i});
  }
  while (white.size() && black.size()) {
    auto itw = white.begin();
    auto itb = black.begin();
    // cout << itw->second << " " << itb->second << endl;
    if (itw->first && itb->first) {
      cout << "Yes";
      return 0;
    }
    if (!(itw->first)) {
      int v = itw->second;
      white.erase(itw);
      assert(black.count({cntb[v], v}));
      black.erase(black.find({cntb[v], v}));
      if (s[v] == 'A')
        eraseA(v, white, cnta);
      else
        eraseA(v, black, cntb);

    } else if (!(itb->first)) {
      int v = itb->second;
      black.erase(itb);
      assert(white.count({cnta[v], v}));
      white.erase(white.find({cnta[v], v}));
      if (s[v] == 'A')
        eraseA(v, white, cnta);
      else
        eraseA(v, black, cntb);
    }
  }
  cout << "No";
}


Fixed submission:
#include <bits/stdc++.h>
#define fr(i, n) for (int i = 0; i < n; i++)
#define pb push_back
#define frab(i, a, b) for (int i = a; i < b; i++)

using namespace std;

typedef long long ll;
typedef long double ld;

const ll MOD = 1e9 + 7;
const ld EPS = 1e-9;
const int N = 3e5 + 10;

vector<int> g[N];
char s[N];
int cnta[N], cntb[N];

void eraseA(int v, set<pair<int, int>> &white, int *cnta) {
  for (auto u : g[v]) {
    if (white.count({cnta[u], u})) {
      white.erase(white.find({cnta[u], u}));
      cnta[u]--;
      white.insert({cnta[u], u});
    }
  }
}

int main() {
  // freopen("a.in", "r", stdin);
  int n, m;
  scanf("%d %d", &n, &m);
  scanf("%s", &s);
  fr(i, m) {
    int v, u;
    scanf("%d %d", &v, &u);
    --v, --u;
    g[v].pb(u);
    g[u].pb(v);
    if (s[u] == 'A')
      cnta[v]++;
    else
      cntb[v]++;
    if (s[v] == 'A')
      cnta[u]++;
    else
      cntb[u]++;
  }
  set<pair<int, int>> white, black;
  fr(i, n) {
    white.insert({cnta[i], i});
    black.insert({cntb[i], i});
  }
  while (white.size() && black.size()) {
    auto itw = white.begin();
    auto itb = black.begin();
    // cout << itw->second << " " << itb->second << endl;
    if (itw->first && itb->first) {
      cout << "Yes";
      return 0;
    }
    if (!(itw->first)) {
      int v = itw->second;
      white.erase(itw);
      assert(black.count({cntb[v], v}));
      black.erase(black.find({cntb[v], v}));
      if (s[v] == 'A')
        eraseA(v, white, cnta);
      else
        eraseA(v, black, cntb);

    } else if (!(itb->first)) {
      int v = itb->second;
      black.erase(itb);
      assert(white.count({cnta[v], v}));
      white.erase(white.find({cnta[v], v}));
      if (s[v] == 'A')
        eraseA(v, white, cnta);
      else
        eraseA(v, black, cntb);
    }
  }
  cout << "No";
}