-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPossibleBipartition_27May.java
More file actions
40 lines (38 loc) · 1.19 KB
/
PossibleBipartition_27May.java
File metadata and controls
40 lines (38 loc) · 1.19 KB
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
40
class Solution {
public boolean possibleBipartition(int N, int[][] dislikes) {
if(dislikes==null || dislikes.length <=1){
return true;
}
List<List<Integer>> graph = new ArrayList<>(N+1);
for(int i=0;i<=N;i++) {
graph.add(new ArrayList<Integer>());
}
for(int[] d : dislikes) {
graph.get(d[0]).add(d[1]);
graph.get(d[1]).add(d[0]);
}
int[] color = new int[N+1];
for(int i=1;i<=N;i++) {
if(color[i]!=0) {
continue;
}
Queue<Integer> q = new LinkedList<>();
q.add(i);
color[i]=1;
while(!q.isEmpty()) {
Integer temp = q.poll();
System.out.println(temp);
for(Integer x : graph.get(temp)) {
if(color[x]==color[temp]) {
return false;
}
if(color[x]==0) {
color[x] = -color[temp];
q.add(x);
}
}
}
}
return true;
}
}