Here is the full solution:
function possibleBipartition(n, dislikes):
graph := array of n + 1 empty lists
for [a, b] in dislikes:
graph[a].append(b)
graph[b].append(a)
color := array of size n + 1, all -1
for i from 1 to n:
if color[i] = -1 then
if not bfs(i, graph, color) then
return false
return true
This runs in time and uses space.