Buggy python submission (verdict: Time Limit Exceeded):
import heapq
import os
import sys
from collections import defaultdict

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353


N, M = list(map(int, sys.stdin.buffer.readline().split()))
S = sys.stdin.buffer.readline().decode().rstrip()
AB = [list(map(int, sys.stdin.buffer.readline().split())) for _ in range(M)]

graph = [defaultdict(int) for _ in range(N)]
for a, b in AB:
    a -= 1
    b -= 1
    graph[a][b] += 1
    if a != b:
        graph[b][a] += 1

# AB 両方と隣接する頂点のみで部分グラフを作れるか
# どっちかとしか隣接しない頂点を取り除いていく
# removed = True
# while removed:
#     removed = False
#     for v in range(N):
#         if not graph[v]:
#             continue
#         a = 0
#         b = 0
#         for u in graph[v]:
#             if S[u] == 'A':
#                 a += 1
#             else:
#                 b += 1
#         if a == 0 or b == 0:
#             for u in graph[v]:
#                 graph[u].remove(v)
#             graph[v] = []
#             removed = True
# print(graph)


a_counts = [[0, v] for v in range(N)]
b_counts = [[0, v] for v in range(N)]
for v in range(N):
    for u, cnt in graph[v].items():
        if S[u] == 'A':
            a_counts[v][0] += cnt
        else:
            b_counts[v][0] += cnt
# shallow copy
a_heap = a_counts[:]
b_heap = b_counts[:]

cont = True
while cont:
    cont = False
    heapq.heapify(a_heap)
    heapq.heapify(b_heap)
    while a_heap and a_heap[0][0] == 0:
        _, v = heapq.heappop(a_heap)
        for u, cnt in list(graph[v].items()):
            if S[v] == 'A':
                a_counts[u][0] -= cnt
            else:
                b_counts[u][0] -= cnt
            del graph[u][v]
        graph[v] = {}
        cont = True
    while b_heap and b_heap[0][0] == 0:
        _, v = heapq.heappop(b_heap)
        for u, cnt in list(graph[v].items()):
            if S[v] == 'A':
                a_counts[u][0] -= cnt
            else:
                b_counts[u][0] -= cnt
            del graph[u][v]
        graph[v] = {}
        cont = True

ok = any(graph)
if ok:
    print('Yes')
else:
    print('No')


Fixed submission:
import os
import sys
from collections import defaultdict

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353


N, M = list(map(int, sys.stdin.buffer.readline().split()))
S = sys.stdin.buffer.readline().decode().rstrip()
AB = [list(map(int, sys.stdin.buffer.readline().split())) for _ in range(M)]

graph = [defaultdict(int) for _ in range(N)]
for a, b in AB:
    a -= 1
    b -= 1
    graph[a][b] += 1
    if a != b:
        graph[b][a] += 1

# AB 両方と隣接する頂点のみで部分グラフを作れるか
# どっちかとしか隣接しない頂点を取り除いていく
# removed = True
# while removed:
#     removed = False
#     for v in range(N):
#         if not graph[v]:
#             continue
#         a = 0
#         b = 0
#         for u in graph[v]:
#             if S[u] == 'A':
#                 a += 1
#             else:
#                 b += 1
#         if a == 0 or b == 0:
#             for u in graph[v]:
#                 graph[u].remove(v)
#             graph[v] = []
#             removed = True
# print(graph)


a_counts = [[0, v] for v in range(N)]
b_counts = [[0, v] for v in range(N)]
for v in range(N):
    for u, cnt in graph[v].items():
        if S[u] == 'A':
            a_counts[v][0] += cnt
        else:
            b_counts[v][0] += cnt
candidates = []
for c, v in a_counts + b_counts:
    if c == 0:
        candidates.append(v)

while candidates:
    v = candidates.pop()
    if not graph[v]:
        continue
    for u, cnt in list(graph[v].items()):
        if S[v] == 'A':
            a_counts[u][0] -= cnt
            if a_counts[u][0] == 0:
                candidates.append(u)
        else:
            b_counts[u][0] -= cnt
            if b_counts[u][0] == 0:
                candidates.append(u)
        del graph[u][v]
    graph[v] = {}

# print(graph)

ok = any(graph)
if ok:
    print('Yes')
else:
    print('No')

Buggy python submission (verdict: Time Limit Exceeded):
import heapq
import os
import sys
from collections import defaultdict

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353


N, M = list(map(int, sys.stdin.buffer.readline().split()))
S = sys.stdin.buffer.readline().decode().rstrip()
AB = [list(map(int, sys.stdin.buffer.readline().split())) for _ in range(M)]

graph = [defaultdict(int) for _ in range(N)]
for a, b in AB:
    a -= 1
    b -= 1
    graph[a][b] += 1
    if a != b:
        graph[b][a] += 1

# AB 両方と隣接する頂点のみで部分グラフを作れるか
# どっちかとしか隣接しない頂点を取り除いていく
# removed = True
# while removed:
#     removed = False
#     for v in range(N):
#         if not graph[v]:
#             continue
#         a = 0
#         b = 0
#         for u in graph[v]:
#             if S[u] == 'A':
#                 a += 1
#             else:
#                 b += 1
#         if a == 0 or b == 0:
#             for u in graph[v]:
#                 graph[u].remove(v)
#             graph[v] = []
#             removed = True
# print(graph)


a_counts = [[0, v] for v in range(N)]
b_counts = [[0, v] for v in range(N)]
for v in range(N):
    for u, cnt in graph[v].items():
        if S[u] == 'A':
            a_counts[v][0] += cnt
        else:
            b_counts[v][0] += cnt
# shallow copy
a_heap = a_counts.copy()
b_heap = b_counts.copy()

cont = True
while cont:
    cont = False
    heapq.heapify(a_heap)
    heapq.heapify(b_heap)
    while a_heap and a_heap[0][0] == 0:
        _, v = heapq.heappop(a_heap)
        for u, cnt in list(graph[v].items()):
            if S[v] == 'A':
                a_counts[u][0] -= cnt
            else:
                b_counts[u][0] -= cnt
            del graph[u][v]
        graph[v] = {}
        cont = True
    while b_heap and b_heap[0][0] == 0:
        _, v = heapq.heappop(b_heap)
        for u, cnt in list(graph[v].items()):
            if S[v] == 'A':
                a_counts[u][0] -= cnt
            else:
                b_counts[u][0] -= cnt
            del graph[u][v]
        graph[v] = {}
        cont = True

ok = any(graph)
if ok:
    print('Yes')
else:
    print('No')


Fixed submission:
import os
import sys
from collections import defaultdict

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353


N, M = list(map(int, sys.stdin.buffer.readline().split()))
S = sys.stdin.buffer.readline().decode().rstrip()
AB = [list(map(int, sys.stdin.buffer.readline().split())) for _ in range(M)]

graph = [defaultdict(int) for _ in range(N)]
for a, b in AB:
    a -= 1
    b -= 1
    graph[a][b] += 1
    if a != b:
        graph[b][a] += 1

# AB 両方と隣接する頂点のみで部分グラフを作れるか
# どっちかとしか隣接しない頂点を取り除いていく
# removed = True
# while removed:
#     removed = False
#     for v in range(N):
#         if not graph[v]:
#             continue
#         a = 0
#         b = 0
#         for u in graph[v]:
#             if S[u] == 'A':
#                 a += 1
#             else:
#                 b += 1
#         if a == 0 or b == 0:
#             for u in graph[v]:
#                 graph[u].remove(v)
#             graph[v] = []
#             removed = True
# print(graph)


a_counts = [[0, v] for v in range(N)]
b_counts = [[0, v] for v in range(N)]
for v in range(N):
    for u, cnt in graph[v].items():
        if S[u] == 'A':
            a_counts[v][0] += cnt
        else:
            b_counts[v][0] += cnt
candidates = []
for c, v in a_counts + b_counts:
    if c == 0:
        candidates.append(v)

while candidates:
    v = candidates.pop()
    if not graph[v]:
        continue
    for u, cnt in list(graph[v].items()):
        if S[v] == 'A':
            a_counts[u][0] -= cnt
            if a_counts[u][0] == 0:
                candidates.append(u)
        else:
            b_counts[u][0] -= cnt
            if b_counts[u][0] == 0:
                candidates.append(u)
        del graph[u][v]
    graph[v] = {}

# print(graph)

ok = any(graph)
if ok:
    print('Yes')
else:
    print('No')

Buggy python submission (verdict: Time Limit Exceeded):
import os
import sys

import heapq

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353


N, M = list(map(int, sys.stdin.buffer.readline().split()))
S = sys.stdin.buffer.readline().decode().rstrip()
AB = [list(map(int, sys.stdin.buffer.readline().split())) for _ in range(M)]

graph = [[] for _ in range(N)]
for a, b in AB:
    a -= 1
    b -= 1
    graph[a].append(b)
    if a != b:
        graph[b].append(a)

# AB 両方と隣接する頂点のみで部分グラフを作れるか
# どっちかとしか隣接しない頂点を取り除いていく
# removed = True
# while removed:
#     removed = False
#     for v in range(N):
#         if not graph[v]:
#             continue
#         a = 0
#         b = 0
#         for u in graph[v]:
#             if S[u] == 'A':
#                 a += 1
#             else:
#                 b += 1
#         if a == 0 or b == 0:
#             for u in graph[v]:
#                 graph[u].remove(v)
#             graph[v] = []
#             removed = True
# print(graph)


a_counts = [[0, v] for v in range(N)]
b_counts = [[0, v] for v in range(N)]
for v in range(N):
    for u in graph[v]:
        if S[u] == 'A':
            a_counts[v][0] += 1
        else:
            b_counts[v][0] += 1
# shallow copy
a_heap = a_counts.copy()
b_heap = b_counts.copy()

removed = [False] * N
cont = True
while cont:
    cont = False
    heapq.heapify(a_heap)
    heapq.heapify(b_heap)
    while a_heap and a_heap[0][0] == 0:
        _, v = heapq.heappop(a_heap)
        for u in graph[v]:
            if removed[u]:
                continue
            if S[v] == 'A':
                a_counts[u][0] -= 1
            else:
                b_counts[u][0] -= 1
        cont = True
        removed[v] = True
    while b_heap and b_heap[0][0] == 0:
        _, v = heapq.heappop(b_heap)
        for u in graph[v]:
            if removed[u]:
                continue
            if S[v] == 'A':
                a_counts[u][0] -= 1
            else:
                b_counts[u][0] -= 1
        cont = True
        removed[v] = True

ok = not all(removed)
if ok:
    print('Yes')
else:
    print('No')


Fixed submission:
import os
import sys
from collections import defaultdict

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353


N, M = list(map(int, sys.stdin.buffer.readline().split()))
S = sys.stdin.buffer.readline().decode().rstrip()
AB = [list(map(int, sys.stdin.buffer.readline().split())) for _ in range(M)]

graph = [defaultdict(int) for _ in range(N)]
for a, b in AB:
    a -= 1
    b -= 1
    graph[a][b] += 1
    if a != b:
        graph[b][a] += 1

# AB 両方と隣接する頂点のみで部分グラフを作れるか
# どっちかとしか隣接しない頂点を取り除いていく
# removed = True
# while removed:
#     removed = False
#     for v in range(N):
#         if not graph[v]:
#             continue
#         a = 0
#         b = 0
#         for u in graph[v]:
#             if S[u] == 'A':
#                 a += 1
#             else:
#                 b += 1
#         if a == 0 or b == 0:
#             for u in graph[v]:
#                 graph[u].remove(v)
#             graph[v] = []
#             removed = True
# print(graph)


a_counts = [[0, v] for v in range(N)]
b_counts = [[0, v] for v in range(N)]
for v in range(N):
    for u, cnt in graph[v].items():
        if S[u] == 'A':
            a_counts[v][0] += cnt
        else:
            b_counts[v][0] += cnt
candidates = []
for c, v in a_counts + b_counts:
    if c == 0:
        candidates.append(v)

while candidates:
    v = candidates.pop()
    if not graph[v]:
        continue
    for u, cnt in list(graph[v].items()):
        if S[v] == 'A':
            a_counts[u][0] -= cnt
            if a_counts[u][0] == 0:
                candidates.append(u)
        else:
            b_counts[u][0] -= cnt
            if b_counts[u][0] == 0:
                candidates.append(u)
        del graph[u][v]
    graph[v] = {}

# print(graph)

ok = any(graph)
if ok:
    print('Yes')
else:
    print('No')

Buggy python submission (verdict: Time Limit Exceeded):
import os
import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353


N, M = list(map(int, sys.stdin.buffer.readline().split()))
S = sys.stdin.buffer.readline().decode().rstrip()
AB = [list(map(int, sys.stdin.buffer.readline().split())) for _ in range(M)]

graph = [set() for _ in range(N)]
for a, b in AB:
    a -= 1
    b -= 1
    graph[a].add(b)
    graph[b].add(a)


# @debug
def dfs(v, pc=None, seen=None):
    seen = seen or set()

    if v in seen:
        return True
    seen.add(v)

    if pc is None or pc == S[v]:
        ok = False
        for u in graph[v]:
            if S[u] != S[v]:
                if dfs(u, S[v], seen):
                    ok = True
                    break
        if not ok:
            seen.remove(v)
            return False

    if pc is None or pc != S[v]:
        ok = False
        for u in graph[v]:
            if S[u] == S[v]:
                if dfs(u, S[v], seen):
                    ok = True
                    break
        if not ok:
            seen.remove(v)
            return False
    return True


ok = False
for v in range(N):
    if dfs(v):
        ok = True
        break
if ok:
    print('Yes')
else:
    print('No')


Fixed submission:
import os
import sys
from collections import defaultdict

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353


N, M = list(map(int, sys.stdin.buffer.readline().split()))
S = sys.stdin.buffer.readline().decode().rstrip()
AB = [list(map(int, sys.stdin.buffer.readline().split())) for _ in range(M)]

graph = [defaultdict(int) for _ in range(N)]
for a, b in AB:
    a -= 1
    b -= 1
    graph[a][b] += 1
    if a != b:
        graph[b][a] += 1

# AB 両方と隣接する頂点のみで部分グラフを作れるか
# どっちかとしか隣接しない頂点を取り除いていく
# removed = True
# while removed:
#     removed = False
#     for v in range(N):
#         if not graph[v]:
#             continue
#         a = 0
#         b = 0
#         for u in graph[v]:
#             if S[u] == 'A':
#                 a += 1
#             else:
#                 b += 1
#         if a == 0 or b == 0:
#             for u in graph[v]:
#                 graph[u].remove(v)
#             graph[v] = []
#             removed = True
# print(graph)


a_counts = [[0, v] for v in range(N)]
b_counts = [[0, v] for v in range(N)]
for v in range(N):
    for u, cnt in graph[v].items():
        if S[u] == 'A':
            a_counts[v][0] += cnt
        else:
            b_counts[v][0] += cnt
candidates = []
for c, v in a_counts + b_counts:
    if c == 0:
        candidates.append(v)

while candidates:
    v = candidates.pop()
    if not graph[v]:
        continue
    for u, cnt in list(graph[v].items()):
        if S[v] == 'A':
            a_counts[u][0] -= cnt
            if a_counts[u][0] == 0:
                candidates.append(u)
        else:
            b_counts[u][0] -= cnt
            if b_counts[u][0] == 0:
                candidates.append(u)
        del graph[u][v]
    graph[v] = {}

# print(graph)

ok = any(graph)
if ok:
    print('Yes')
else:
    print('No')

Buggy python submission (verdict: Time Limit Exceeded):
import os
import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353


N, M = list(map(int, sys.stdin.buffer.readline().split()))
S = sys.stdin.buffer.readline().decode().rstrip()
AB = [list(map(int, sys.stdin.buffer.readline().split())) for _ in range(M)]

graph = [set() for _ in range(N)]
for a, b in AB:
    a -= 1
    b -= 1
    graph[a].add(b)
    graph[b].add(a)



# @debug
def dfs(v, pc=None, seen=None):
    seen = seen or set()

    if v in seen:
        return True
    seen.add(v)

    if pc is None or pc == S[v]:
        ok = False
        for u in graph[v]:
            if S[u] != S[v]:
                ok |= dfs(u, S[v], seen)
        if not ok:
            seen.remove(v)
            return False

    if pc is None or pc != S[v]:
        ok = False
        for u in graph[v]:
            if S[u] == S[v]:
                ok |= dfs(u, S[v], seen)
        if not ok:
            seen.remove(v)
            return False
    return True


ok = False
for v in range(N):
    ok |= dfs(v)
if ok:
    print('Yes')
else:
    print('No')


Fixed submission:
import os
import sys
from collections import defaultdict

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353


N, M = list(map(int, sys.stdin.buffer.readline().split()))
S = sys.stdin.buffer.readline().decode().rstrip()
AB = [list(map(int, sys.stdin.buffer.readline().split())) for _ in range(M)]

graph = [defaultdict(int) for _ in range(N)]
for a, b in AB:
    a -= 1
    b -= 1
    graph[a][b] += 1
    if a != b:
        graph[b][a] += 1

# AB 両方と隣接する頂点のみで部分グラフを作れるか
# どっちかとしか隣接しない頂点を取り除いていく
# removed = True
# while removed:
#     removed = False
#     for v in range(N):
#         if not graph[v]:
#             continue
#         a = 0
#         b = 0
#         for u in graph[v]:
#             if S[u] == 'A':
#                 a += 1
#             else:
#                 b += 1
#         if a == 0 or b == 0:
#             for u in graph[v]:
#                 graph[u].remove(v)
#             graph[v] = []
#             removed = True
# print(graph)


a_counts = [[0, v] for v in range(N)]
b_counts = [[0, v] for v in range(N)]
for v in range(N):
    for u, cnt in graph[v].items():
        if S[u] == 'A':
            a_counts[v][0] += cnt
        else:
            b_counts[v][0] += cnt
candidates = []
for c, v in a_counts + b_counts:
    if c == 0:
        candidates.append(v)

while candidates:
    v = candidates.pop()
    if not graph[v]:
        continue
    for u, cnt in list(graph[v].items()):
        if S[v] == 'A':
            a_counts[u][0] -= cnt
            if a_counts[u][0] == 0:
                candidates.append(u)
        else:
            b_counts[u][0] -= cnt
            if b_counts[u][0] == 0:
                candidates.append(u)
        del graph[u][v]
    graph[v] = {}

# print(graph)

ok = any(graph)
if ok:
    print('Yes')
else:
    print('No')