-
Notifications
You must be signed in to change notification settings - Fork 5
/
PossibleBipartition.java
39 lines (38 loc) · 1.34 KB
/
PossibleBipartition.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*https://leetcode.com/problems/possible-bipartition/*/
class Solution {
public boolean possibleBipartition(int n, int[][] dislikes) {
ArrayList<ArrayList<Integer>> graph = new ArrayList<ArrayList<Integer>>();
int i, j, currNode;
for (i = 0; i < n; ++i) graph.add(new ArrayList<Integer>());
for (int[] dislike : dislikes)
{
graph.get(dislike[0]-1).add(dislike[1]-1);
graph.get(dislike[1]-1).add(dislike[0]-1);
}
Queue<Integer> queue = new LinkedList<Integer>();
int[] colors = new int[n];
boolean[] visited = new boolean[n];
Arrays.fill(colors,-1);
for (i = 0; i < n; ++i)
{
if (!visited[i])
{
queue.add(i);
colors[i] = 1;
while (!queue.isEmpty())
{
currNode = queue.poll();
if (visited[currNode]) continue;
visited[currNode] = true;
for (Integer adjNode : graph.get(currNode))
{
if (colors[currNode] == colors[adjNode]) return false;
colors[adjNode] = 1-colors[currNode];
queue.add(adjNode);
}
}
}
}
return true;
}
}