AcWing 860. 染色法判定二分图
原题链接
简单
作者:
皓首不倦
,
2020-08-10 13:00:37
,
所有人可见
,
阅读 333
import sys
sys.setrecursionlimit(999999999)
from collections import deque
n, m = map(int, input().split())
link = {}
init_node = -1
for i in range(m):
a, b = map(int, input().split())
init_node = a
if a not in link:
link[a]= []
if b not in link:
link[b] = []
link[a].append(b)
link[b].append(a)
color = [-1] * (100007)
def bfs(init_node):
que = deque()
que.append(init_node)
color[init_node] = 1
while len(que) > 0:
cur = que.popleft()
for ne in link[cur]:
if color[ne] == -1:
for next in link[ne]:
if color[next] == 1-color[cur]:
return False
color[ne] = 1-color[cur]
que.append(ne)
return True
print('Yes' if bfs(init_node) else 'No')