Problem p03256 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <assert.h>
#include <cstring>
#include <iterator>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
#pragma warning(disable : 4996)

typedef long long ll;
#define MIN(a, b) ((a) > (b) ? (b) : (a))
#define MAX(a, b) ((a) < (b) ? (b) : (a))
#define LINF 9223300000000000000
#define INF 2140000000
#define MOD 1000000007
using namespace std;

char str[100005];

int main(int argc, char *argv[]) {
  long n, m;
  scanf("%ld%ld", &n, &m);
  scanf("%s", str);

  vector<vector<long>> z(n);

  long i, j;
  for (i = 0; i < m; i++) {
    long a, b;
    scanf("%ld%ld", &a, &b);
    z[a - 1].push_back(b - 1);
    z[b - 1].push_back(a - 1);
  }

  queue<long> que;
  for (i = 0; i < n; i++) {
    long a = 0, b = 0;
    for (j = 0; j < z[i].size(); j++) {
      if (str[z[i][j]] == 'A')
        a++;
      else
        b++;
    }
    if (a == 0 || b == 0) {
      que.push(i);
    }
  }

  vector<long> flag(n, 0);
  while (!que.empty()) {
    long i = que.front();
    que.pop();

    if (flag[i])
      continue;

    long a = 0, b = 0;
    for (j = 0; j < z[i].size(); j++) {
      if (!flag[z[i][j]]) {
        if (str[z[i][j]] == 'A')
          a++;
        else
          b++;
      }
    }
    if (a == 0 || b == 0) {
      flag[i] = 1;
      for (j = 0; j < z[i].size(); j++) {
        if (!flag[z[i][j]]) {
          que.push(z[i][j]);
        }
      }
    }
  }

  for (i = 0; i < n; i++) {
    if (flag[i] == 0) {
      printf("Yes\n");
      return 0;
    }
  }
  printf("No\n");

  return 0;
}


Fixed submission:
#include <algorithm>
#include <assert.h>
#include <cstring>
#include <iterator>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
#pragma warning(disable : 4996)

typedef long long ll;
#define MIN(a, b) ((a) > (b) ? (b) : (a))
#define MAX(a, b) ((a) < (b) ? (b) : (a))
#define LINF 9223300000000000000
#define INF 2140000000
#define MOD 1000000007
using namespace std;

char str[200005];

int main(int argc, char *argv[]) {
  long n, m;
  scanf("%ld%ld", &n, &m);
  scanf("%s", str);

  vector<vector<long>> z(n);

  long i, j;
  for (i = 0; i < m; i++) {
    long a, b;
    scanf("%ld%ld", &a, &b);
    z[a - 1].push_back(b - 1);
    z[b - 1].push_back(a - 1);
  }

  queue<long> que;
  for (i = 0; i < n; i++) {
    long a = 0, b = 0;
    for (j = 0; j < z[i].size(); j++) {
      if (str[z[i][j]] == 'A')
        a++;
      else
        b++;
    }
    if (a == 0 || b == 0) {
      que.push(i);
    }
  }

  vector<long> flag(n, 0);
  while (!que.empty()) {
    long i = que.front();
    que.pop();

    if (flag[i])
      continue;

    long a = 0, b = 0;
    for (j = 0; j < z[i].size(); j++) {
      if (!flag[z[i][j]]) {
        if (str[z[i][j]] == 'A')
          a++;
        else
          b++;
      }
    }
    if (a == 0 || b == 0) {
      flag[i] = 1;
      for (j = 0; j < z[i].size(); j++) {
        if (!flag[z[i][j]]) {
          que.push(z[i][j]);
        }
      }
    }
  }

  for (i = 0; i < n; i++) {
    if (flag[i] == 0) {
      printf("Yes\n");
      return 0;
    }
  }
  printf("No\n");

  return 0;
}