There was an error while loading. Please reload this page.
1 parent 6a0242b commit b3f7405Copy full SHA for b3f7405
1 file changed
leetcode3/염혜정/785. Is Graph Bipartite?.java
@@ -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