Problem p03256 (C++)

Buggy submission (status: Time Limit Exceeded):
/*FBI WARNING
Federal law provides severe civil and criminal penalties for
the unauthorzed reproduction, distribution, or exhibition of
copyrighted motion pictures (Title 17 united states code
sections 501 and 508). The federal bureau of investigation
investigates allegations of criminal copyright infringement
(Title 17, united states code sections 506).*/

#include <bits/stdc++.h>
using namespace std;
#define maxn 400010

template <typename tp> void read(tp &dig) {
  char ch = getchar();
  dig = 0;
  int flag = 0;
  while (!isdigit(ch)) {
    if (ch == '-')
      flag = 1;
    ch = getchar();
  }
  while (isdigit(ch))
    dig = dig * 10 + ch - '0', ch = getchar();
  if (flag)
    dig = -dig;
}

int fst[maxn], nxt[maxn << 1], to[maxn << 1];
int n, m, lnum, a[maxn], sum[maxn][2];
char s[maxn];
int vis[maxn];
queue<int> que;

inline void add_edge(int u, int v) {
  nxt[++lnum] = fst[u];
  fst[u] = lnum;
  to[lnum] = v;
  nxt[++lnum] = fst[v];
  fst[v] = lnum;
  to[lnum] = u;
}

int main() {

#ifndef ONLINE_JUDGE
  freopen("a.in", "r", stdin);
#endif

  read(n);
  read(m);
  scanf("%s", s + 1);
  for (int i = 1; i <= n; ++i)
    a[i] = (s[i] == 'A');
  for (int i = 1, x, y; i <= m; ++i) {
    read(x), read(y);
    add_edge(x, y);
    ++sum[x][a[y]];
    ++sum[y][a[x]];
  }
  for (int i = 1; i <= n; ++i)
    if (!sum[i][0] || !sum[i][1])
      que.push(i), vis[i] = 1;
  while (!que.empty()) {
    int now = que.front();
    que.pop();
    for (int j = fst[now]; j; j = nxt[j]) {
      int v = to[j];
      --sum[v][a[now]];
      if (vis[v])
        continue;
      if (!sum[v][0] || !sum[v][1])
        que.push(v), vis[v] = 1;
    }
  }
  int flag = 0;
  for (int i = 1; i <= n; ++i)
    if (!vis[i])
      flag = 1;
  if (flag)
    printf("Yes\n");
  else
    printf("No\n");
  return 0;
}


Fixed submission:
/*FBI WARNING
Federal law provides severe civil and criminal penalties for
the unauthorzed reproduction, distribution, or exhibition of
copyrighted motion pictures (Title 17 united states code
sections 501 and 508). The federal bureau of investigation
investigates allegations of criminal copyright infringement
(Title 17, united states code sections 506).*/

#include <bits/stdc++.h>
using namespace std;
#define maxn 400010

template <typename tp> void read(tp &dig) {
  char ch = getchar();
  dig = 0;
  int flag = 0;
  while (!isdigit(ch)) {
    if (ch == '-')
      flag = 1;
    ch = getchar();
  }
  while (isdigit(ch))
    dig = dig * 10 + ch - '0', ch = getchar();
  if (flag)
    dig = -dig;
}

int fst[maxn], nxt[maxn << 1], to[maxn << 1];
int n, m, lnum, a[maxn], sum[maxn][2];
char s[maxn];
int vis[maxn];
queue<int> que;

inline void add_edge(int u, int v) {
  nxt[++lnum] = fst[u];
  fst[u] = lnum;
  to[lnum] = v;
  nxt[++lnum] = fst[v];
  fst[v] = lnum;
  to[lnum] = u;
}

int main() {

  read(n);
  read(m);
  scanf("%s", s + 1);
  for (int i = 1; i <= n; ++i)
    a[i] = (s[i] == 'A');
  for (int i = 1, x, y; i <= m; ++i) {
    read(x), read(y);
    add_edge(x, y);
    ++sum[x][a[y]];
    ++sum[y][a[x]];
  }
  for (int i = 1; i <= n; ++i)
    if (!sum[i][0] || !sum[i][1])
      que.push(i), vis[i] = 1;
  while (!que.empty()) {
    int now = que.front();
    que.pop();
    for (int j = fst[now]; j; j = nxt[j]) {
      int v = to[j];
      --sum[v][a[now]];
      if (vis[v])
        continue;
      if (!sum[v][0] || !sum[v][1])
        que.push(v), vis[v] = 1;
    }
  }
  int flag = 0;
  for (int i = 1; i <= n; ++i)
    if (!vis[i])
      flag = 1;
  if (flag)
    printf("Yes\n");
  else
    printf("No\n");
  return 0;
}