class Graph:
def __init__(self,n):
self.n = n
self.v = [[] for i in range(n)]
def addEdge(self,a,b):
self.v[a].append(b)
def edges(self,a):
return self.v[a]
def __len__(self):
return self.n
def isBipartite(g):
marked = [False] * len(g)
color = [1] * len(g)
g.res = True
def bfs(s):
marked[s] = True
for e in g.edges(s):
if marked[e] == False:
color[e] = - color[s]
bfs(e)
elif color[e] == color[s]:
g.res = False
return
for i in range(len(g)):
if marked[i] == False:
bfs(i)
return (g.res and "Yes" or "No")
二分图
最新推荐文章于 2024-08-23 12:03:00 发布