Skip to content

Commit 7ccb69a

Browse files
committed
Add solution 0785
1 parent 5f4219c commit 7ccb69a

File tree

6 files changed

+284
-44
lines changed

6 files changed

+284
-44
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package leetcode
2+
3+
// DFS 染色,1 是红色,0 是绿色,-1 是未染色
4+
func isBipartite(graph [][]int) bool {
5+
colors := make([]int, len(graph))
6+
for i := range colors {
7+
colors[i] = -1
8+
}
9+
for i := range graph {
10+
if !dfs(i, graph, colors, -1) {
11+
return false
12+
}
13+
}
14+
return true
15+
}
16+
17+
func dfs(n int, graph [][]int, colors []int, parentCol int) bool {
18+
if colors[n] == -1 {
19+
if parentCol == 1 {
20+
colors[n] = 0
21+
} else {
22+
colors[n] = 1
23+
}
24+
} else if colors[n] == parentCol {
25+
return false
26+
} else if colors[n] != parentCol {
27+
return true
28+
}
29+
for _, c := range graph[n] {
30+
if !dfs(c, graph, colors, colors[n]) {
31+
return false
32+
}
33+
}
34+
return true
35+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question785 struct {
9+
para785
10+
ans785
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para785 struct {
16+
graph [][]int
17+
}
18+
19+
// ans 是答案
20+
// one 代表第一个答案
21+
type ans785 struct {
22+
one bool
23+
}
24+
25+
func Test_Problem785(t *testing.T) {
26+
27+
qs := []question785{
28+
29+
{
30+
para785{[][]int{{1, 3}, {0, 2}, {1, 3}, {0, 2}}},
31+
ans785{true},
32+
},
33+
34+
{
35+
para785{[][]int{{1, 2, 3}, {0, 2}, {0, 1, 3}, {0, 2}}},
36+
ans785{false},
37+
},
38+
39+
{
40+
para785{[][]int{{1, 2, 3}, {0, 2, 3}, {0, 1, 3}, {0, 1, 2}}},
41+
ans785{false},
42+
},
43+
}
44+
45+
fmt.Printf("------------------------Leetcode Problem 785------------------------\n")
46+
47+
for _, q := range qs {
48+
_, p := q.ans785, q.para785
49+
fmt.Printf("【input】:%v 【output】:%v\n", p, isBipartite(p.graph))
50+
}
51+
fmt.Printf("\n\n\n")
52+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# [785. Is Graph Bipartite?](https://leetcode.com/problems/is-graph-bipartite/)
2+
3+
4+
## 题目
5+
6+
Given an undirected `graph`, return `true` if and only if it is bipartite.
7+
8+
Recall that a graph is *bipartite* if we can split it's set of nodes into two independent subsets A and B such that every edge in the graph has one node in A and another node in B.
9+
10+
The graph is given in the following form: `graph[i]` is a list of indexes `j` for which the edge between nodes `i` and `j` exists. Each node is an integer between `0` and `graph.length - 1`. There are no self edges or parallel edges: `graph[i]` does not contain `i`, and it doesn't contain any element twice.
11+
12+
13+
Example 1:Input: [[1,3], [0,2], [1,3], [0,2]]
14+
Output: true
15+
Explanation:
16+
The graph looks like this:
17+
0----1
18+
| |
19+
| |
20+
3----2
21+
We can divide the vertices into two groups: {0, 2} and {1, 3}.
22+
23+
24+
Example 2:Input: [[1,2,3], [0,2], [0,1,3], [0,2]]
25+
Output: false
26+
Explanation:
27+
The graph looks like this:
28+
0----1
29+
| \ |
30+
| \ |
31+
3----2
32+
We cannot find a way to divide the set of nodes into two independent subsets.
33+
34+
35+
**Note:**
36+
37+
- `graph` will have length in range `[1, 100]`.
38+
- `graph[i]` will contain integers in range `[0, graph.length - 1]`.
39+
- `graph[i]` will not contain `i` or duplicate values.
40+
- The graph is undirected: if any element `j` is in `graph[i]`, then `i` will be in `graph[j]`.
41+
42+
## 题目大意
43+
44+
给定一个无向图 graph,当这个图为二分图时返回 true。
45+
46+
graph 将会以邻接表方式给出,graph[i] 表示图中与节点i相连的所有节点。每个节点都是一个在 0 到 graph.length-1 之间的整数。这图中没有自环和平行边: graph[i] 中不存在 i,并且 graph[i] 中没有重复的值。
47+
48+
注意:
49+
50+
- graph 的长度范围为 [1, 100]
51+
- graph[i] 中的元素的范围为 [0, graph.length - 1]
52+
- graph[i] 不会包含 i 或者有重复的值。
53+
- 图是无向的: 如果 j 在 graph[i] 里边, 那么 i 也会在 graph[j] 里边。
54+
55+
## 解题思路
56+
57+
- 判断一个无向图是否是二分图。二分图的定义:如果我们能将一个图的节点集合分割成两个独立的子集 A 和 B,并使图中的每一条边的两个节点一个来自 A 集合,一个来自 B 集合,我们就将这个图称为二分图。
58+
- 这一题可以用 BFS、DFS、并查集来解答。这里是 DFS 实现。任选一个节点开始,把它染成红色,然后对整个图 DFS 遍历,把与它相连的节点并且未被染色的,都染成绿色。颜色不同的节点代表不同的集合。这时候还可能遇到第 2 种情况,与它相连的节点已经有颜色了,并且这个颜色和前一个节点的颜色相同,这就说明了该无向图不是二分图。可以直接 return false。如此遍历到所有节点都染色了,如果能染色成功,说明该无向图是二分图,返回 true。
59+
60+
## 代码
61+
62+
```go
63+
package leetcode
64+
65+
// DFS 染色,1 是红色,0 是绿色,-1 是未染色
66+
func isBipartite(graph [][]int) bool {
67+
colors := make([]int, len(graph))
68+
for i := range colors {
69+
colors[i] = -1
70+
}
71+
for i := range graph {
72+
if !dfs(i, graph, colors, -1) {
73+
return false
74+
}
75+
}
76+
return true
77+
}
78+
79+
func dfs(n int, graph [][]int, colors []int, parentCol int) bool {
80+
if colors[n] == -1 {
81+
if parentCol == 1 {
82+
colors[n] = 0
83+
} else {
84+
colors[n] = 1
85+
}
86+
} else if colors[n] == parentCol {
87+
return false
88+
} else if colors[n] != parentCol {
89+
return true
90+
}
91+
for _, c := range graph[n] {
92+
if !dfs(c, graph, colors, colors[n]) {
93+
return false
94+
}
95+
}
96+
return true
97+
}
98+
```

leetcode/785.Is Graph Bipartite?

Lines changed: 0 additions & 44 deletions
This file was deleted.
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# [785. Is Graph Bipartite?](https://leetcode.com/problems/is-graph-bipartite/)
2+
3+
4+
## 题目
5+
6+
Given an undirected `graph`, return `true` if and only if it is bipartite.
7+
8+
Recall that a graph is *bipartite* if we can split it's set of nodes into two independent subsets A and B such that every edge in the graph has one node in A and another node in B.
9+
10+
The graph is given in the following form: `graph[i]` is a list of indexes `j` for which the edge between nodes `i` and `j` exists. Each node is an integer between `0` and `graph.length - 1`. There are no self edges or parallel edges: `graph[i]` does not contain `i`, and it doesn't contain any element twice.
11+
12+
13+
Example 1:Input: [[1,3], [0,2], [1,3], [0,2]]
14+
Output: true
15+
Explanation:
16+
The graph looks like this:
17+
0----1
18+
| |
19+
| |
20+
3----2
21+
We can divide the vertices into two groups: {0, 2} and {1, 3}.
22+
23+
24+
Example 2:Input: [[1,2,3], [0,2], [0,1,3], [0,2]]
25+
Output: false
26+
Explanation:
27+
The graph looks like this:
28+
0----1
29+
| \ |
30+
| \ |
31+
3----2
32+
We cannot find a way to divide the set of nodes into two independent subsets.
33+
34+
35+
**Note:**
36+
37+
- `graph` will have length in range `[1, 100]`.
38+
- `graph[i]` will contain integers in range `[0, graph.length - 1]`.
39+
- `graph[i]` will not contain `i` or duplicate values.
40+
- The graph is undirected: if any element `j` is in `graph[i]`, then `i` will be in `graph[j]`.
41+
42+
## 题目大意
43+
44+
给定一个无向图 graph,当这个图为二分图时返回 true。
45+
46+
graph 将会以邻接表方式给出,graph[i] 表示图中与节点i相连的所有节点。每个节点都是一个在 0 到 graph.length-1 之间的整数。这图中没有自环和平行边: graph[i] 中不存在 i,并且 graph[i] 中没有重复的值。
47+
48+
注意:
49+
50+
- graph 的长度范围为 [1, 100]
51+
- graph[i] 中的元素的范围为 [0, graph.length - 1]
52+
- graph[i] 不会包含 i 或者有重复的值。
53+
- 图是无向的: 如果 j 在 graph[i] 里边, 那么 i 也会在 graph[j] 里边。
54+
55+
## 解题思路
56+
57+
- 判断一个无向图是否是二分图。二分图的定义:如果我们能将一个图的节点集合分割成两个独立的子集 A 和 B,并使图中的每一条边的两个节点一个来自 A 集合,一个来自 B 集合,我们就将这个图称为二分图。
58+
- 这一题可以用 BFS、DFS、并查集来解答。这里是 DFS 实现。任选一个节点开始,把它染成红色,然后对整个图 DFS 遍历,把与它相连的节点并且未被染色的,都染成绿色。颜色不同的节点代表不同的集合。这时候还可能遇到第 2 种情况,与它相连的节点已经有颜色了,并且这个颜色和前一个节点的颜色相同,这就说明了该无向图不是二分图。可以直接 return false。如此遍历到所有节点都染色了,如果能染色成功,说明该无向图是二分图,返回 true。
59+
60+
## 代码
61+
62+
```go
63+
package leetcode
64+
65+
// DFS 染色,1 是红色,0 是绿色,-1 是未染色
66+
func isBipartite(graph [][]int) bool {
67+
colors := make([]int, len(graph))
68+
for i := range colors {
69+
colors[i] = -1
70+
}
71+
for i := range graph {
72+
if !dfs(i, graph, colors, -1) {
73+
return false
74+
}
75+
}
76+
return true
77+
}
78+
79+
func dfs(n int, graph [][]int, colors []int, parentCol int) bool {
80+
if colors[n] == -1 {
81+
if parentCol == 1 {
82+
colors[n] = 0
83+
} else {
84+
colors[n] = 1
85+
}
86+
} else if colors[n] == parentCol {
87+
return false
88+
} else if colors[n] != parentCol {
89+
return true
90+
}
91+
for _, c := range graph[n] {
92+
if !dfs(c, graph, colors, colors[n]) {
93+
return false
94+
}
95+
}
96+
return true
97+
}
98+
```

website/content/menu/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,7 @@ headless: true
379379
- [0778.Swim-in-Rising-Water]({{< relref "/ChapterFour/0778.Swim-in-Rising-Water.md" >}})
380380
- [0781.Rabbits-in-Forest]({{< relref "/ChapterFour/0781.Rabbits-in-Forest.md" >}})
381381
- [0784.Letter-Case-Permutation]({{< relref "/ChapterFour/0784.Letter-Case-Permutation.md" >}})
382+
- [0785.Is-Graph-Bipartite]({{< relref "/ChapterFour/0785.Is-Graph-Bipartite.md" >}})
382383
- [0786.K-th-Smallest-Prime-Fraction]({{< relref "/ChapterFour/0786.K-th-Smallest-Prime-Fraction.md" >}})
383384
- [0793.Preimage-Size-of-Factorial-Zeroes-Function]({{< relref "/ChapterFour/0793.Preimage-Size-of-Factorial-Zeroes-Function.md" >}})
384385
- [0802.Find-Eventual-Safe-States]({{< relref "/ChapterFour/0802.Find-Eventual-Safe-States.md" >}})

0 commit comments

Comments
 (0)