-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmedium_785_IsGraphBipartite.java
More file actions
48 lines (47 loc) · 1.22 KB
/
medium_785_IsGraphBipartite.java
File metadata and controls
48 lines (47 loc) · 1.22 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
41
42
43
44
45
46
47
48
import java.util.LinkedList;
import java.util.Queue;
/**
* created by zl on 2019/5/14 21:26
*/
public class medium_785_IsGraphBipartite
{
int[] color ;
Queue<Integer> queue = new LinkedList<>();
public boolean isBipartite(int[][] graph)
{
color = new int[graph.length];
for (int i=0;i<graph.length;i++)
{
if (color[i]==0&&!bfs(i,graph))
{
return false;
}
}
return true;
}
boolean bfs(int s,int[][] graph)
{
color[s] = 1;
queue.add(s);//节点i
while (!queue.isEmpty())
{
int from = queue.poll();
for (int x:graph[from])
{
//如果相邻的点没有上色就给这个点上色
if (color[x]==0)
{
queue.add(x);
color[x] = -color[from];
}
//如果相邻的点颜色相同就返回false
if (color[x]==color[from])
{
return false;
}
}
}
//如果所有的点染过色,且相邻点点颜色都不一样,返回true
return true;
}
}