Skip to content

Commit b3f7405

Browse files
authored
Create 785. Is Graph Bipartite?.java
1 parent 6a0242b commit b3f7405

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// 인접한 노드는 다른 색
2+
3+
class Solution {
4+
public boolean isBipartite(int[][] graph) {
5+
int[] color = new int[graph.length];
6+
7+
for (int i = 0; i<graph.length; i++) {
8+
if (color[i] != 0) continue;
9+
10+
Queue<Integer> queue = new LinkedList<>();
11+
queue.offer(i);
12+
color[i] = 1;
13+
14+
while (!queue.isEmpty()) {
15+
int node = queue.poll();
16+
for (int next : graph[node]) {
17+
if (color[next] == color[node]) return false;
18+
else if (color[next] == 0) {
19+
color[next] = -color[node];
20+
queue.offer(next);
21+
}
22+
}
23+
}
24+
}
25+
return true;
26+
}
27+
}

0 commit comments

Comments
 (0)