結果

問題 No.1436 Rgaph
ユーザー qwewe
提出日時 2025-05-14 12:56:12
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,436 bytes
コンパイル時間 171 ms
コンパイル使用メモリ 82,596 KB
実行使用メモリ 65,612 KB
最終ジャッジ日時 2025-05-14 12:58:10
合計ジャッジ時間 5,485 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 19 WA * 9
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from collections import deque

def main():
    input = sys.stdin.read().split()
    idx = 0
    N = int(input[idx])
    idx += 1
    M = int(input[idx])
    idx += 1
    edges = []
    adj = [[] for _ in range(N+1)]  # 1-based
    for _ in range(M):
        a = int(input[idx])
        idx += 1
        b = int(input[idx])
        idx += 1
        edges.append((a, b))
        adj[a].append(b)
        adj[b].append(a)  # for undirected bipartition check

    # Check if the underlying undirected graph is bipartite
    color = [-1] * (N + 1)
    is_bipartite = True
    for u in range(1, N+1):
        if color[u] == -1:
            queue = deque()
            queue.append(u)
            color[u] = 0
            while queue:
                v = queue.popleft()
                for nei in adj[v]:
                    if color[nei] == -1:
                        color[nei] = color[v] ^ 1
                        queue.append(nei)
                    elif color[nei] == color[v]:
                        is_bipartite = False
                        break
                if not is_bipartite:
                    break
            if not is_bipartite:
                break
    if is_bipartite:
        print(-1)
        return

    # If not bipartite, output alternating R and G
    res = []
    for i in range(M):
        res.append('R' if i % 2 == 0 else 'G')
    print(''.join(res))

if __name__ == '__main__':
    main()
0