Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit22485db

Browse files
authored
Update 0785-is-graph-bipartite.kt
1 parentd5742cb commit22485db

File tree

1 file changed

+90
-32
lines changed

1 file changed

+90
-32
lines changed

‎kotlin/0785-is-graph-bipartite.kt

Lines changed: 90 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,8 @@
1-
packagekotlin
2-
3-
importjava.util.*
4-
1+
/*
2+
* BFS
3+
*/
54
classSolution {
6-
funisBipartiteDfs(graph:Array<IntArray>):Boolean {
7-
if (graph.isEmpty())returntrue
8-
9-
val nodeColorMap= mutableMapOf<Int,Color>()
10-
fundfs(node:Int,defaultColor:Color):Boolean {
11-
if (node!in nodeColorMap.keys) nodeColorMap[node]= defaultColor
12-
for (adjacentNodein graph[node]) {
13-
val isEdgeVisited= nodeColorMap[adjacentNode]!=null
14-
if (isEdgeVisited) {
15-
val colorOfCurrentNode= nodeColorMap.getValue(node)
16-
val colorOfAdjacentNode= nodeColorMap.getValue(adjacentNode)
17-
if (colorOfAdjacentNode== colorOfCurrentNode)returnfalse
18-
elsecontinue
19-
}
20-
if (!dfs(adjacentNode, defaultColor.inverse))returnfalse
21-
}
22-
returntrue
23-
}
24-
25-
for (nodein graph.indices) {
26-
if (nodein nodeColorMap.keys)continue
27-
if (!dfs(node,Color.RED))returnfalse
28-
}
29-
returntrue
30-
}
31-
32-
funisBipartiteBfs(graph:Array<IntArray>):Boolean {
5+
funisBipartite(graph:Array<IntArray>):Boolean {
336
if (graph.isEmpty())returntrue
347
val nodeColorMap= mutableMapOf<Int,Color>()
358

@@ -46,7 +19,8 @@ class Solution {
4619
if (adjacentNodein nodeColorMap.keys) {
4720
val colorOfAdjacentNode= nodeColorMap.getValue(adjacentNode)
4821
if (colorOfAdjacentNode== colorOfCurrentNode)returnfalse
49-
continue
22+
continue
23+
5024
}
5125
nodeColorMap[adjacentNode]= colorOfCurrentNode.inverse
5226
queue.add(adjacentNode)
@@ -70,3 +44,87 @@ class Solution {
7044
Color.GREEN->Color.RED
7145
}
7246
}
47+
48+
/*
49+
* DFS
50+
*/
51+
classSolution {
52+
funisBipartite(graph:Array<IntArray>):Boolean {
53+
if (graph.isEmpty())returntrue
54+
55+
val nodeColorMap= mutableMapOf<Int,Color>()
56+
fundfs(node:Int,defaultColor:Color):Boolean {
57+
if (node!in nodeColorMap.keys) nodeColorMap[node]= defaultColor
58+
for (adjacentNodein graph[node]) {
59+
val isEdgeVisited= nodeColorMap[adjacentNode]!=null
60+
if (isEdgeVisited) {
61+
val colorOfCurrentNode= nodeColorMap.getValue(node)
62+
val colorOfAdjacentNode= nodeColorMap.getValue(adjacentNode)
63+
if (colorOfAdjacentNode== colorOfCurrentNode)returnfalse
64+
elsecontinue
65+
}
66+
if (!dfs(adjacentNode, defaultColor.inverse))returnfalse
67+
}
68+
returntrue
69+
}
70+
71+
for (nodein graph.indices) {
72+
if (nodein nodeColorMap.keys)continue
73+
if (!dfs(node,Color.RED))returnfalse
74+
}
75+
returntrue
76+
}
77+
78+
privateenumclassColor {RED,GREEN }
79+
80+
privatevalColor.inverse
81+
get()=when (this) {
82+
Color.RED->Color.GREEN
83+
Color.GREEN->Color.RED
84+
}
85+
}
86+
87+
/*
88+
* Union Find
89+
*/
90+
classSolution {
91+
92+
classDSU(valn:Int) {
93+
94+
val parent=IntArray(n) { it }
95+
val rank=IntArray(n) {1 }
96+
97+
funfind(x:Int):Int {
98+
if (x!= parent[x])
99+
parent[x]= find(parent[x])
100+
return parent[x]
101+
}
102+
103+
fununion(x:Int,y:Int) {
104+
val px= find(x)
105+
val py= find(y)
106+
107+
if (rank[px]>= rank[py]) {
108+
parent[py]= px
109+
rank[px]+= rank[py]
110+
}else {
111+
parent[px]= py
112+
rank[py]+= rank[px]
113+
}
114+
}
115+
}
116+
117+
funisBipartite(graph:Array<IntArray>):Boolean {
118+
val dsu=DSU(graph.size)
119+
120+
for (iin0 until graph.size) {
121+
for (jin graph[i]) {
122+
if (dsu.find(i)== dsu.find(j))
123+
returnfalse
124+
dsu.union(graph[i][0], j)
125+
}
126+
}
127+
128+
returntrue
129+
}
130+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp