Buggy python submission (verdict: Time Limit Exceeded):
I=input
N,M=map(int,I().split())
X,G,d,C=list(map(int,I().split())),[set()for _ in range(N)],[0]*M,[0]*N
for i in range(M):
    u,v=sorted(map(int,I().split()),key=lambda x:[X[x-1],-x])
    G[v-1].add(u-1)
    d[i]=str(X[v-1])
for x, u in sorted((x,i)for i, x in enumerate(X)):
    if C[u]:continue
    if not G[u]:break
    C[u],S=-1 if sum(C[c]+1 for c in G[u])else 1,[u]
    while S:
        q = S.pop()
        for c in G[q]:
            if C[c]:continue
            S+=[c]
            C[c]=-C[q]
print("".join(" BW"[i] for i in C)+"\n"+"\n".join(d)if C[u]else -1)

Fixed submission:
from collections import defaultdict
import sys
input = sys.stdin.readline
N, M = map(int, input().split())
bxs = list(map(int, input().split()))
xs = bxs[:]
dd = defaultdict(set)
for i, x in enumerate(xs):
    dd[x].add(i+1)
xs = list(set(xs))
xs.sort()

#for x, i in xs:
G = [set() for _ in range(N+1)]
G2 = [set() for _ in range(N+1)]
es = []
for i in range(M):
    u, v = map(int, input().split())
    if bxs[u-1] == bxs[v-1]:
        G[v].add(u)
        G[u].add(v)
    elif bxs[u-1] > bxs[v-1]:
        G[u].add(v)
    else:
        G[v].add(u)
    e = (min(u,v), max(u,v))
    es.append(e)

r = True
d = dict()
cc = [-1 for _ in range(N+1)]
for i in range(N):
    if len(G[i+1]) == 0:
        print(-1)
        sys.exit()
for x in xs:
    for u in dd[x]:
        if cc[u] != -1:
            continue
        for c in G[u]:
            if cc[c] == -1:
                cc[u] = 1
                break
            else:
                cc[u] = 1-cc[c]
        stack = [u]
        while stack:
            q = stack.pop()
            for c in G[q]:
                e = (min(q,c), max(q,c))
                if e in d:
                    continue
                d[e] = x
                if cc[c] != -1:
                    continue
                stack.append(c)
                cc[c] = 1 - cc[q]
for i in range(1, N+1):
    if cc[i] == -1:
        print(-1)
        sys.exit()
for i in range(1, N+1):
    print("B"if cc[i] else "W", end="")
print()
for i in range(M):
    print(d[es[i]])

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
X = list(map(int, input().split()))
G = [set() for _ in range(N)]
for i in range(M):
    u, v = sorted(map(int, input().split()),key=lambda x:[X[x-1],-x])
    G[v-1].add((u-1,i))
X,d,C = sorted([(x,i) for i, x in enumerate(X)]),[0]*M,[0]*N
for x, u in X:
    if C[u]:continue
    if not G[u]:break
    C[u]=1
    for c, _ in G[u]:
        if C[c]:
            C[u] = -C[c]
    S = [u]
    while S:
        q = S.pop()
        for c, i in G[q]:
            d[i] = str(x)
            if C[c]:continue
            S.append(c)
            C[c] = -C[q]
print("".join(" BW"[i] for i in C)+"\n"+"\n".join(d) if C[u] else -1)

Fixed submission:
from collections import defaultdict
import sys
input = sys.stdin.readline
N, M = map(int, input().split())
bxs = list(map(int, input().split()))
xs = bxs[:]
dd = defaultdict(set)
for i, x in enumerate(xs):
    dd[x].add(i+1)
xs = list(set(xs))
xs.sort()

#for x, i in xs:
G = [set() for _ in range(N+1)]
G2 = [set() for _ in range(N+1)]
es = []
for i in range(M):
    u, v = map(int, input().split())
    if bxs[u-1] == bxs[v-1]:
        G[v].add(u)
        G[u].add(v)
    elif bxs[u-1] > bxs[v-1]:
        G[u].add(v)
    else:
        G[v].add(u)
    e = (min(u,v), max(u,v))
    es.append(e)

r = True
d = dict()
cc = [-1 for _ in range(N+1)]
for i in range(N):
    if len(G[i+1]) == 0:
        print(-1)
        sys.exit()
for x in xs:
    for u in dd[x]:
        if cc[u] != -1:
            continue
        for c in G[u]:
            if cc[c] == -1:
                cc[u] = 1
                break
            else:
                cc[u] = 1-cc[c]
        stack = [u]
        while stack:
            q = stack.pop()
            for c in G[q]:
                e = (min(q,c), max(q,c))
                if e in d:
                    continue
                d[e] = x
                if cc[c] != -1:
                    continue
                stack.append(c)
                cc[c] = 1 - cc[q]
for i in range(1, N+1):
    if cc[i] == -1:
        print(-1)
        sys.exit()
for i in range(1, N+1):
    print("B"if cc[i] else "W", end="")
print()
for i in range(M):
    print(d[es[i]])

Buggy python submission (verdict: Time Limit Exceeded):
import sys
N, M = map(int, input().split())
xs = list(map(int, input().split()))
G = [set() for _ in range(N+1)]
es = []
for i in range(M):
    u, v = sorted(map(int, input().split()))
    if xs[u-1] == xs[v-1]:
        G[v].add(u)
        G[u].add(v)
    elif xs[u-1] > xs[v-1]:
        G[u].add(v)
    else:
        G[v].add(u)
    es.append((u,v))
xs,d,cc = sorted([(x,i+1) for i, x in enumerate(xs)]),{},{}
for i in range(N):
    if not G[i+1]:
        print(-1)
        sys.exit()
for x, u in xs:
    if u in cc:
        continue
    cc[u] = 1
    for c in G[u]:
        if c in cc:
            cc[u] = 1-cc[c]
    stack = [u]
    while stack:
        q = stack.pop()
        for c in G[q]:
            e = tuple(sorted([q,c]))
            d[e] = x
            if c in cc:
                continue
            stack.append(c)
            cc[c] = 1-cc[q]
print("".join("BW"[cc[i]] for i in range(1,N+1)))
for i in range(M):
    print(d[es[i]])


Fixed submission:
from collections import defaultdict
import sys
input = sys.stdin.readline
N, M = map(int, input().split())
bxs = list(map(int, input().split()))
xs = bxs[:]
dd = defaultdict(set)
for i, x in enumerate(xs):
    dd[x].add(i+1)
xs = list(set(xs))
xs.sort()

#for x, i in xs:
G = [set() for _ in range(N+1)]
G2 = [set() for _ in range(N+1)]
es = []
for i in range(M):
    u, v = map(int, input().split())
    if bxs[u-1] == bxs[v-1]:
        G[v].add(u)
        G[u].add(v)
    elif bxs[u-1] > bxs[v-1]:
        G[u].add(v)
    else:
        G[v].add(u)
    e = (min(u,v), max(u,v))
    es.append(e)

r = True
d = dict()
cc = [-1 for _ in range(N+1)]
for i in range(N):
    if len(G[i+1]) == 0:
        print(-1)
        sys.exit()
for x in xs:
    for u in dd[x]:
        if cc[u] != -1:
            continue
        for c in G[u]:
            if cc[c] == -1:
                cc[u] = 1
                break
            else:
                cc[u] = 1-cc[c]
        stack = [u]
        while stack:
            q = stack.pop()
            for c in G[q]:
                e = (min(q,c), max(q,c))
                if e in d:
                    continue
                d[e] = x
                if cc[c] != -1:
                    continue
                stack.append(c)
                cc[c] = 1 - cc[q]
for i in range(1, N+1):
    if cc[i] == -1:
        print(-1)
        sys.exit()
for i in range(1, N+1):
    print("B"if cc[i] else "W", end="")
print()
for i in range(M):
    print(d[es[i]])

Buggy python submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
N, M = map(int, input().split())
xs = list(map(int, input().split()))

G = [set() for _ in range(N+1)]
es = []
for i in range(M):
    u, v = map(int, input().split())
    if xs[u-1] == xs[v-1]:
        G[v].add(u)
        G[u].add(v)
    elif xs[u-1] > xs[v-1]:
        G[u].add(v)
    else:
        G[v].add(u)
    e = (min(u,v), max(u,v))
    es.append(e)

xs = sorted([(x,i+1) for i, x in enumerate(xs)])
d = dict()
cc = dict()
for i in range(N):
    if not (G[i+1] or G2[i+1]):
        print(-1)
        sys.exit()
for x, u in xs:
    if u in cc:
        continue
    cc[u] = 1
    for c in G[u]:
        if c in cc:
            cc[u] = 1-cc[c]
    stack = [u]
    while stack:
        q = stack.pop()
        for c in G[q]:
            e = (min(q,c), max(q,c))
            d[e] = x
            if c in cc:
                continue
            stack.append(c)
            cc[c] = 1 - cc[q]
if len(cc) == N:
    print(-1)
else:
    print("".join("BW"[cc[i]] for i in range(1,N+1)))
    for i in range(M):
        print(d[es[i]])



Fixed submission:
from collections import defaultdict
import sys
input = sys.stdin.readline
N, M = map(int, input().split())
bxs = list(map(int, input().split()))
xs = bxs[:]
dd = defaultdict(set)
for i, x in enumerate(xs):
    dd[x].add(i+1)
xs = list(set(xs))
xs.sort()

#for x, i in xs:
G = [set() for _ in range(N+1)]
G2 = [set() for _ in range(N+1)]
es = []
for i in range(M):
    u, v = map(int, input().split())
    if bxs[u-1] == bxs[v-1]:
        G[v].add(u)
        G[u].add(v)
    elif bxs[u-1] > bxs[v-1]:
        G[u].add(v)
    else:
        G[v].add(u)
    e = (min(u,v), max(u,v))
    es.append(e)

r = True
d = dict()
cc = [-1 for _ in range(N+1)]
for i in range(N):
    if len(G[i+1]) == 0:
        print(-1)
        sys.exit()
for x in xs:
    for u in dd[x]:
        if cc[u] != -1:
            continue
        for c in G[u]:
            if cc[c] == -1:
                cc[u] = 1
                break
            else:
                cc[u] = 1-cc[c]
        stack = [u]
        while stack:
            q = stack.pop()
            for c in G[q]:
                e = (min(q,c), max(q,c))
                if e in d:
                    continue
                d[e] = x
                if cc[c] != -1:
                    continue
                stack.append(c)
                cc[c] = 1 - cc[q]
for i in range(1, N+1):
    if cc[i] == -1:
        print(-1)
        sys.exit()
for i in range(1, N+1):
    print("B"if cc[i] else "W", end="")
print()
for i in range(M):
    print(d[es[i]])

Buggy python submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
N, M = map(int, input().split())
xs = list(map(int, input().split()))

G = [set() for _ in range(N+1)]
es = []
for i in range(M):
    u, v = map(int, input().split())
    if xs[u-1] == xs[v-1]:
        G[v].add(u)
        G[u].add(v)
    elif xs[u-1] > xs[v-1]:
        G[u].add(v)
    else:
        G[v].add(u)
    e = (min(u,v), max(u,v))
    es.append(e)

xs = sorted([(x,i+1) for i, x in enumerate(xs)])
d = dict()
cc = dict()
for i in range(N):
    if not (G[i+1] or G2[i+1]):
        print(-1)
        sys.exit()
for x, u in xs:
    if u in cc:
        continue
    cc[u] = 1
    for c in G[u]:
        if c in cc:
            cc[u] = 1-cc[c]
    stack = [u]
    while stack:
        q = stack.pop()
        for c in G[q]:
            e = (min(q,c), max(q,c))
            d[e] = x
            if c in cc:
                continue
            stack.append(c)
            cc[c] = 1 - cc[q]
print("".join("BW"[cc[i]] for i in range(1,N+1)))
for i in range(M):
    print(d[es[i]])



Fixed submission:
from collections import defaultdict
import sys
input = sys.stdin.readline
N, M = map(int, input().split())
bxs = list(map(int, input().split()))
xs = bxs[:]
dd = defaultdict(set)
for i, x in enumerate(xs):
    dd[x].add(i+1)
xs = list(set(xs))
xs.sort()

#for x, i in xs:
G = [set() for _ in range(N+1)]
G2 = [set() for _ in range(N+1)]
es = []
for i in range(M):
    u, v = map(int, input().split())
    if bxs[u-1] == bxs[v-1]:
        G[v].add(u)
        G[u].add(v)
    elif bxs[u-1] > bxs[v-1]:
        G[u].add(v)
    else:
        G[v].add(u)
    e = (min(u,v), max(u,v))
    es.append(e)

r = True
d = dict()
cc = [-1 for _ in range(N+1)]
for i in range(N):
    if len(G[i+1]) == 0:
        print(-1)
        sys.exit()
for x in xs:
    for u in dd[x]:
        if cc[u] != -1:
            continue
        for c in G[u]:
            if cc[c] == -1:
                cc[u] = 1
                break
            else:
                cc[u] = 1-cc[c]
        stack = [u]
        while stack:
            q = stack.pop()
            for c in G[q]:
                e = (min(q,c), max(q,c))
                if e in d:
                    continue
                d[e] = x
                if cc[c] != -1:
                    continue
                stack.append(c)
                cc[c] = 1 - cc[q]
for i in range(1, N+1):
    if cc[i] == -1:
        print(-1)
        sys.exit()
for i in range(1, N+1):
    print("B"if cc[i] else "W", end="")
print()
for i in range(M):
    print(d[es[i]])