#34224: python解


s116113@elvs.chc.edu.tw (資處甲116113許銪升)

學校 : 國立二林高級工商職業學校
編號 : 213088
來源 : [118.232.28.227]
最後登入時間 :
2024-11-15 21:37:24
d768. 10004 - Bicoloring -- UVa10004 | From: [118.232.28.25] | 發表日期 : 2023-03-06 21:53

def dfs(node, color, graph, colors):
    colors[node] = color
    for neighbor in graph[node]:
        if colors[neighbor] == color:
            return False
        if colors[neighbor] == 0 and not dfs(neighbor, -color, graph, colors):
            return False
    return True

while True:
    n = int(input())
    if n == 0:
        break
    m = int(input())
    graph = {i: [] for i in range(n)}
    for _ in range(m):
        u, v = map(int, input().split())
        graph[u].append(v)
        graph[v].append(u)
    colors = [0] * n
    if dfs(0, 1, graph, colors):
        print("BICOLORABLE.")
    else:
        print("NOT BICOLORABLE.")

 
ZeroJudge Forum